blob: 41c660c0c10ce105ea40963a87f2b2d1af32a902 [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/*
11 * eval.c: User defined function support
12 */
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 Moolenaara9b579f2016-07-17 18:29:19 +020025
26/* From user function to hashitem and back. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027#define UF2HIKEY(fp) ((fp)->uf_name)
Bram Moolenaar98aefe72018-12-13 22:20:09 +010028#define HIKEY2UF(p) ((ufunc_T *)((p) - offsetof(ufunc_T, uf_name)))
Bram Moolenaara9b579f2016-07-17 18:29:19 +020029#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
30
31#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
32#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
33
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034/*
35 * All user-defined functions are found in this hashtable.
36 */
37static hashtab_T func_hashtab;
38
39/* Used by get_func_tv() */
40static garray_T funcargs = GA_EMPTY;
41
Bram Moolenaar209b8e32019-03-14 13:43:24 +010042// pointer to funccal for currently active function
43static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020044
Bram Moolenaar209b8e32019-03-14 13:43:24 +010045// Pointer to list of previously used funccal, still around because some
46// item in it is still being used.
47static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020048
49static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
50static char *e_funcdict = N_("E717: Dictionary entry already exists");
51static char *e_funcref = N_("E718: Funcref required");
52static char *e_nofunc = N_("E130: Unknown function: %s");
53
54#ifdef FEAT_PROFILE
55static void func_do_profile(ufunc_T *fp);
56static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
57static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
58static int
59# ifdef __BORLANDC__
60 _RTLENTRYF
61# endif
62 prof_total_cmp(const void *s1, const void *s2);
63static int
64# ifdef __BORLANDC__
65 _RTLENTRYF
66# endif
67 prof_self_cmp(const void *s1, const void *s2);
68#endif
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020069static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020070
71 void
72func_init()
73{
74 hash_init(&func_hashtab);
75}
76
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020077/*
78 * Get function arguments.
79 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +020080 static int
81get_function_args(
82 char_u **argp,
83 char_u endchar,
84 garray_T *newargs,
85 int *varargs,
86 int skip)
87{
88 int mustend = FALSE;
89 char_u *arg = *argp;
90 char_u *p = arg;
91 int c;
92 int i;
93
94 if (newargs != NULL)
95 ga_init2(newargs, (int)sizeof(char_u *), 3);
96
97 if (varargs != NULL)
98 *varargs = FALSE;
99
100 /*
101 * Isolate the arguments: "arg1, arg2, ...)"
102 */
103 while (*p != endchar)
104 {
105 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
106 {
107 if (varargs != NULL)
108 *varargs = TRUE;
109 p += 3;
110 mustend = TRUE;
111 }
112 else
113 {
114 arg = p;
115 while (ASCII_ISALNUM(*p) || *p == '_')
116 ++p;
117 if (arg == p || isdigit(*arg)
118 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
119 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
120 {
121 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100122 semsg(_("E125: Illegal argument: %s"), arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200123 break;
124 }
125 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200126 goto err_ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200127 if (newargs != NULL)
128 {
129 c = *p;
130 *p = NUL;
131 arg = vim_strsave(arg);
132 if (arg == NULL)
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200133 {
134 *p = c;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200135 goto err_ret;
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200136 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200137
138 /* Check for duplicate argument name. */
139 for (i = 0; i < newargs->ga_len; ++i)
140 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
141 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100142 semsg(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200143 vim_free(arg);
144 goto err_ret;
145 }
146 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
147 newargs->ga_len++;
148
149 *p = c;
150 }
151 if (*p == ',')
152 ++p;
153 else
154 mustend = TRUE;
155 }
156 p = skipwhite(p);
157 if (mustend && *p != endchar)
158 {
159 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100160 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200161 break;
162 }
163 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200164 if (*p != endchar)
165 goto err_ret;
166 ++p; /* skip "endchar" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200167
168 *argp = p;
169 return OK;
170
171err_ret:
172 if (newargs != NULL)
173 ga_clear_strings(newargs);
174 return FAIL;
175}
176
177/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200178 * Register function "fp" as using "current_funccal" as its scope.
179 */
180 static int
181register_closure(ufunc_T *fp)
182{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200183 if (fp->uf_scoped == current_funccal)
184 /* no change */
185 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200186 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200187 fp->uf_scoped = current_funccal;
188 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200189
Bram Moolenaar58016442016-07-31 18:30:22 +0200190 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
191 return FAIL;
192 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
193 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200194 return OK;
195}
196
197/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200198 * Parse a lambda expression and get a Funcref from "*arg".
199 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
200 */
201 int
202get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
203{
204 garray_T newargs;
205 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200206 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200207 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100208 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200209 int varargs;
210 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200211 char_u *start = skipwhite(*arg + 1);
212 char_u *s, *e;
213 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200214 int *old_eval_lavars = eval_lavars_used;
215 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200216
217 ga_init(&newargs);
218 ga_init(&newlines);
219
220 /* First, check if this is a lambda expression. "->" must exist. */
221 ret = get_function_args(&start, '-', NULL, NULL, TRUE);
222 if (ret == FAIL || *start != '>')
223 return NOTDONE;
224
225 /* Parse the arguments again. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200226 if (evaluate)
227 pnewargs = &newargs;
228 else
229 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 *arg = skipwhite(*arg + 1);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200231 ret = get_function_args(arg, '-', pnewargs, &varargs, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200232 if (ret == FAIL || **arg != '>')
233 goto errret;
234
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +0200235 /* Set up a flag for checking local variables and arguments. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200236 if (evaluate)
237 eval_lavars_used = &eval_lavars;
238
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200239 /* Get the start and the end of the expression. */
240 *arg = skipwhite(*arg + 1);
241 s = *arg;
242 ret = skip_expr(arg);
243 if (ret == FAIL)
244 goto errret;
245 e = *arg;
246 *arg = skipwhite(*arg);
247 if (**arg != '}')
248 goto errret;
249 ++*arg;
250
251 if (evaluate)
252 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200253 int len, flags = 0;
254 char_u *p;
255 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200256
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200257 sprintf((char*)name, "<lambda>%d", ++lambda_no);
258
Bram Moolenaar58016442016-07-31 18:30:22 +0200259 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200260 if (fp == NULL)
261 goto errret;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200262 pt = (partial_T *)alloc_clear((unsigned)sizeof(partial_T));
263 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200264 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200265
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200266 ga_init2(&newlines, (int)sizeof(char_u *), 1);
267 if (ga_grow(&newlines, 1) == FAIL)
268 goto errret;
269
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200270 /* Add "return " before the expression. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200271 len = 7 + e - s + 1;
272 p = (char_u *)alloc(len);
273 if (p == NULL)
274 goto errret;
275 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
276 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200277 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200278
279 fp->uf_refcount = 1;
280 STRCPY(fp->uf_name, name);
281 hash_add(&func_hashtab, UF2HIKEY(fp));
282 fp->uf_args = newargs;
283 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200284 if (current_funccal != NULL && eval_lavars)
285 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200286 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200287 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200288 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200289 }
290 else
291 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292
293#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200294 if (prof_def_func())
295 func_do_profile(fp);
296#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200297 if (sandbox)
298 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200299 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200300 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200301 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200302 fp->uf_script_ctx = current_sctx;
303 fp->uf_script_ctx.sc_lnum += sourcing_lnum - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200304
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200305 pt->pt_func = fp;
306 pt->pt_refcount = 1;
307 rettv->vval.v_partial = pt;
308 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200309 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200310
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200311 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200312 return OK;
313
314errret:
315 ga_clear_strings(&newargs);
316 ga_clear_strings(&newlines);
317 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100318 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200319 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200320 return FAIL;
321}
322
323/*
324 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
325 * name it contains, otherwise return "name".
326 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
327 * "partialp".
328 */
329 char_u *
330deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
331{
332 dictitem_T *v;
333 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200334 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200335
336 if (partialp != NULL)
337 *partialp = NULL;
338
339 cc = name[*lenp];
340 name[*lenp] = NUL;
341 v = find_var(name, NULL, no_autoload);
342 name[*lenp] = cc;
343 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
344 {
345 if (v->di_tv.vval.v_string == NULL)
346 {
347 *lenp = 0;
348 return (char_u *)""; /* just in case */
349 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200350 s = v->di_tv.vval.v_string;
351 *lenp = (int)STRLEN(s);
352 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200353 }
354
355 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
356 {
357 partial_T *pt = v->di_tv.vval.v_partial;
358
359 if (pt == NULL)
360 {
361 *lenp = 0;
362 return (char_u *)""; /* just in case */
363 }
364 if (partialp != NULL)
365 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200366 s = partial_name(pt);
367 *lenp = (int)STRLEN(s);
368 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 }
370
371 return name;
372}
373
374/*
375 * Give an error message with a function name. Handle <SNR> things.
376 * "ermsg" is to be passed without translation, use N_() instead of _().
377 */
378 static void
379emsg_funcname(char *ermsg, char_u *name)
380{
381 char_u *p;
382
383 if (*name == K_SPECIAL)
384 p = concat_str((char_u *)"<SNR>", name + 3);
385 else
386 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100387 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200388 if (p != name)
389 vim_free(p);
390}
391
392/*
393 * Allocate a variable for the result of a function.
394 * Return OK or FAIL.
395 */
396 int
397get_func_tv(
398 char_u *name, /* name of the function */
399 int len, /* length of "name" */
400 typval_T *rettv,
401 char_u **arg, /* argument, pointing to the '(' */
402 linenr_T firstline, /* first line of range */
403 linenr_T lastline, /* last line of range */
404 int *doesrange, /* return: function handled range */
405 int evaluate,
406 partial_T *partial, /* for extra arguments */
407 dict_T *selfdict) /* Dictionary for "self" */
408{
409 char_u *argp;
410 int ret = OK;
411 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
412 int argcount = 0; /* number of arguments found */
413
414 /*
415 * Get the arguments.
416 */
417 argp = *arg;
418 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
419 {
420 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
421 if (*argp == ')' || *argp == ',' || *argp == NUL)
422 break;
423 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
424 {
425 ret = FAIL;
426 break;
427 }
428 ++argcount;
429 if (*argp != ',')
430 break;
431 }
432 if (*argp == ')')
433 ++argp;
434 else
435 ret = FAIL;
436
437 if (ret == OK)
438 {
439 int i = 0;
440
441 if (get_vim_var_nr(VV_TESTING))
442 {
443 /* Prepare for calling test_garbagecollect_now(), need to know
444 * what variables are used on the call stack. */
445 if (funcargs.ga_itemsize == 0)
446 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
447 for (i = 0; i < argcount; ++i)
448 if (ga_grow(&funcargs, 1) == OK)
449 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
450 &argvars[i];
451 }
452
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200453 ret = call_func(name, len, rettv, argcount, argvars, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200454 firstline, lastline, doesrange, evaluate, partial, selfdict);
455
456 funcargs.ga_len -= i;
457 }
458 else if (!aborting())
459 {
460 if (argcount == MAX_FUNC_ARGS)
461 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
462 else
463 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
464 }
465
466 while (--argcount >= 0)
467 clear_tv(&argvars[argcount]);
468
469 *arg = skipwhite(argp);
470 return ret;
471}
472
473#define FLEN_FIXED 40
474
475/*
476 * Return TRUE if "p" starts with "<SID>" or "s:".
477 * Only works if eval_fname_script() returned non-zero for "p"!
478 */
479 static int
480eval_fname_sid(char_u *p)
481{
482 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
483}
484
485/*
486 * In a script change <SID>name() and s:name() to K_SNR 123_name().
487 * Change <SNR>123_name() to K_SNR 123_name().
488 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
489 * (slow).
490 */
491 static char_u *
492fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
493{
494 int llen;
495 char_u *fname;
496 int i;
497
498 llen = eval_fname_script(name);
499 if (llen > 0)
500 {
501 fname_buf[0] = K_SPECIAL;
502 fname_buf[1] = KS_EXTRA;
503 fname_buf[2] = (int)KE_SNR;
504 i = 3;
505 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
506 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200507 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200508 *error = ERROR_SCRIPT;
509 else
510 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200511 sprintf((char *)fname_buf + 3, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200512 i = (int)STRLEN(fname_buf);
513 }
514 }
515 if (i + STRLEN(name + llen) < FLEN_FIXED)
516 {
517 STRCPY(fname_buf + i, name + llen);
518 fname = fname_buf;
519 }
520 else
521 {
522 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
523 if (fname == NULL)
524 *error = ERROR_OTHER;
525 else
526 {
527 *tofree = fname;
528 mch_memmove(fname, fname_buf, (size_t)i);
529 STRCPY(fname + i, name + llen);
530 }
531 }
532 }
533 else
534 fname = name;
535 return fname;
536}
537
538/*
539 * Find a function by name, return pointer to it in ufuncs.
540 * Return NULL for unknown function.
541 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200542 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200543find_func(char_u *name)
544{
545 hashitem_T *hi;
546
547 hi = hash_find(&func_hashtab, name);
548 if (!HASHITEM_EMPTY(hi))
549 return HI2UF(hi);
550 return NULL;
551}
552
553/*
554 * Copy the function name of "fp" to buffer "buf".
555 * "buf" must be able to hold the function name plus three bytes.
556 * Takes care of script-local function names.
557 */
558 static void
559cat_func_name(char_u *buf, ufunc_T *fp)
560{
561 if (fp->uf_name[0] == K_SPECIAL)
562 {
563 STRCPY(buf, "<SNR>");
564 STRCAT(buf, fp->uf_name + 3);
565 }
566 else
567 STRCPY(buf, fp->uf_name);
568}
569
570/*
571 * Add a number variable "name" to dict "dp" with value "nr".
572 */
573 static void
574add_nr_var(
575 dict_T *dp,
576 dictitem_T *v,
577 char *name,
578 varnumber_T nr)
579{
580 STRCPY(v->di_key, name);
581 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
582 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
583 v->di_tv.v_type = VAR_NUMBER;
584 v->di_tv.v_lock = VAR_FIXED;
585 v->di_tv.vval.v_number = nr;
586}
587
588/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100589 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200590 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100591 static void
592free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200593{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100594 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200595
596 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
597 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100598 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200599
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100600 // When garbage collecting a funccall_T may be freed before the
601 // function that references it, clear its uf_scoped field.
602 // The function may have been redefined and point to another
603 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200604 if (fp != NULL && fp->uf_scoped == fc)
605 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200606 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200607 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200608
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200609 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200610 vim_free(fc);
611}
612
613/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100614 * Free "fc" and what it contains.
615 * Can be called only when "fc" is kept beyond the period of it called,
616 * i.e. after cleanup_function_call(fc).
617 */
618 static void
619free_funccal_contents(funccall_T *fc)
620{
621 listitem_T *li;
622
623 // Free all l: variables.
624 vars_clear(&fc->l_vars.dv_hashtab);
625
626 // Free all a: variables.
627 vars_clear(&fc->l_avars.dv_hashtab);
628
629 // Free the a:000 variables.
630 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
631 clear_tv(&li->li_tv);
632
633 free_funccal(fc);
634}
635
636/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200637 * Handle the last part of returning from a function: free the local hashtable.
638 * Unless it is still in use by a closure.
639 */
640 static void
641cleanup_function_call(funccall_T *fc)
642{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100643 int may_free_fc = fc->fc_refcount <= 0;
644 int free_fc = TRUE;
645
Bram Moolenaar6914c642017-04-01 21:21:30 +0200646 current_funccal = fc->caller;
647
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100648 // Free all l: variables if not referred.
649 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
650 vars_clear(&fc->l_vars.dv_hashtab);
651 else
652 free_fc = FALSE;
653
654 // If the a:000 list and the l: and a: dicts are not referenced and
655 // there is no closure using it, we can free the funccall_T and what's
656 // in it.
657 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
658 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200659 else
660 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100661 int todo;
662 hashitem_T *hi;
663 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200664
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100665 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200666
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100667 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200668 todo = (int)fc->l_avars.dv_hashtab.ht_used;
669 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
670 {
671 if (!HASHITEM_EMPTY(hi))
672 {
673 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100674 di = HI2DI(hi);
675 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200676 }
677 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100678 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200679
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100680 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
681 fc->l_varlist.lv_first = NULL;
682 else
683 {
684 listitem_T *li;
685
686 free_fc = FALSE;
687
688 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200689 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
690 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100691 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100692
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100693 if (free_fc)
694 free_funccal(fc);
695 else
696 {
697 static int made_copy = 0;
698
699 // "fc" is still in use. This can happen when returning "a:000",
700 // assigning "l:" to a global variable or defining a closure.
701 // Link "fc" in the list for garbage collection later.
702 fc->caller = previous_funccal;
703 previous_funccal = fc;
704
705 if (want_garbage_collect)
706 // If garbage collector is ready, clear count.
707 made_copy = 0;
708 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100709 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100710 // We have made a lot of copies, worth 4 Mbyte. This can happen
711 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100712 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100713 // too much memory.
714 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100715 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100716 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200717 }
718}
719
720/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200721 * Call a user function.
722 */
723 static void
724call_user_func(
725 ufunc_T *fp, /* pointer to function */
726 int argcount, /* nr of args */
727 typval_T *argvars, /* arguments */
728 typval_T *rettv, /* return value */
729 linenr_T firstline, /* first line of range */
730 linenr_T lastline, /* last line of range */
731 dict_T *selfdict) /* Dictionary for "self" */
732{
733 char_u *save_sourcing_name;
734 linenr_T save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200735 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +0200736 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200737 funccall_T *fc;
738 int save_did_emsg;
739 static int depth = 0;
740 dictitem_T *v;
741 int fixvar_idx = 0; /* index in fixvar[] */
742 int i;
743 int ai;
744 int islambda = FALSE;
745 char_u numbuf[NUMBUFLEN];
746 char_u *name;
747 size_t len;
748#ifdef FEAT_PROFILE
749 proftime_T wait_start;
750 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +0200751 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200752#endif
753
754 /* If depth of calling is getting too high, don't execute the function */
755 if (depth >= p_mfd)
756 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100757 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200758 rettv->v_type = VAR_NUMBER;
759 rettv->vval.v_number = -1;
760 return;
761 }
762 ++depth;
763
764 line_breakcheck(); /* check for CTRL-C hit */
765
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100766 fc = (funccall_T *)alloc_clear(sizeof(funccall_T));
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100767 if (fc == NULL)
768 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769 fc->caller = current_funccal;
770 current_funccal = fc;
771 fc->func = fp;
772 fc->rettv = rettv;
773 rettv->vval.v_number = 0;
774 fc->linenr = 0;
775 fc->returned = FALSE;
776 fc->level = ex_nesting_level;
777 /* Check if this function has a breakpoint. */
778 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
779 fc->dbg_tick = debug_tick;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200780 /* Set up fields for closure. */
781 fc->fc_refcount = 0;
782 fc->fc_copyID = 0;
783 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200784 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200785
786 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
787 islambda = TRUE;
788
789 /*
790 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
791 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
792 * each argument variable and saves a lot of time.
793 */
794 /*
795 * Init l: variables.
796 */
797 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
798 if (selfdict != NULL)
799 {
800 /* Set l:self to "selfdict". Use "name" to avoid a warning from
801 * some compiler that checks the destination size. */
802 v = &fc->fixvar[fixvar_idx++].var;
803 name = v->di_key;
804 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +0100805 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200806 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
807 v->di_tv.v_type = VAR_DICT;
808 v->di_tv.v_lock = 0;
809 v->di_tv.vval.v_dict = selfdict;
810 ++selfdict->dv_refcount;
811 }
812
813 /*
814 * Init a: variables.
815 * Set a:0 to "argcount".
816 * Set a:000 to a list with room for the "..." arguments.
817 */
818 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
819 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
820 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar31b81602019-02-10 22:14:27 +0100821 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200822 /* Use "name" to avoid a warning from some compiler that checks the
823 * destination size. */
824 v = &fc->fixvar[fixvar_idx++].var;
825 name = v->di_key;
826 STRCPY(name, "000");
827 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
828 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
829 v->di_tv.v_type = VAR_LIST;
830 v->di_tv.v_lock = VAR_FIXED;
831 v->di_tv.vval.v_list = &fc->l_varlist;
832 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
833 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
834 fc->l_varlist.lv_lock = VAR_FIXED;
835
836 /*
837 * Set a:firstline to "firstline" and a:lastline to "lastline".
838 * Set a:name to named arguments.
839 * Set a:N to the "..." arguments.
840 */
841 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
842 (varnumber_T)firstline);
843 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
844 (varnumber_T)lastline);
845 for (i = 0; i < argcount; ++i)
846 {
847 int addlocal = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200848
849 ai = i - fp->uf_args.ga_len;
850 if (ai < 0)
851 {
852 /* named argument a:name */
853 name = FUNCARG(fp, i);
854 if (islambda)
855 addlocal = TRUE;
856 }
857 else
858 {
859 /* "..." argument a:1, a:2, etc. */
860 sprintf((char *)numbuf, "%d", ai + 1);
861 name = numbuf;
862 }
863 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
864 {
865 v = &fc->fixvar[fixvar_idx++].var;
866 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100867 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200868 }
869 else
870 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100871 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200872 if (v == NULL)
873 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100874 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200875 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200876
877 /* Note: the values are copied directly to avoid alloc/free.
878 * "argvars" must have VAR_FIXED for v_lock. */
879 v->di_tv = argvars[i];
880 v->di_tv.v_lock = VAR_FIXED;
881
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200882 if (addlocal)
883 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200884 /* Named arguments should be accessed without the "a:" prefix in
885 * lambda expressions. Add to the l: dict. */
886 copy_tv(&v->di_tv, &v->di_tv);
887 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200888 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200889 else
890 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200891
892 if (ai >= 0 && ai < MAX_FUNC_ARGS)
893 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100894 listitem_T *li = &fc->l_listitems[ai];
895
896 li->li_tv = argvars[i];
897 li->li_tv.v_lock = VAR_FIXED;
898 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200899 }
900 }
901
902 /* Don't redraw while executing the function. */
903 ++RedrawingDisabled;
904 save_sourcing_name = sourcing_name;
905 save_sourcing_lnum = sourcing_lnum;
906 sourcing_lnum = 1;
Bram Moolenaar93343722018-07-10 19:39:18 +0200907
908 if (fp->uf_flags & FC_SANDBOX)
909 {
910 using_sandbox = TRUE;
911 ++sandbox;
912 }
913
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200914 /* need space for function name + ("function " + 3) or "[number]" */
915 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
916 + STRLEN(fp->uf_name) + 20;
917 sourcing_name = alloc((unsigned)len);
918 if (sourcing_name != NULL)
919 {
920 if (save_sourcing_name != NULL
921 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
922 sprintf((char *)sourcing_name, "%s[%d]..",
923 save_sourcing_name, (int)save_sourcing_lnum);
924 else
925 STRCPY(sourcing_name, "function ");
926 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
927
928 if (p_verbose >= 12)
929 {
930 ++no_wait_return;
931 verbose_enter_scroll();
932
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100933 smsg(_("calling %s"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200934 if (p_verbose >= 14)
935 {
936 char_u buf[MSG_BUF_LEN];
937 char_u numbuf2[NUMBUFLEN];
938 char_u *tofree;
939 char_u *s;
940
Bram Moolenaar32526b32019-01-19 17:43:09 +0100941 msg_puts("(");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200942 for (i = 0; i < argcount; ++i)
943 {
944 if (i > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100945 msg_puts(", ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200946 if (argvars[i].v_type == VAR_NUMBER)
947 msg_outnum((long)argvars[i].vval.v_number);
948 else
949 {
950 /* Do not want errors such as E724 here. */
951 ++emsg_off;
952 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
953 --emsg_off;
954 if (s != NULL)
955 {
956 if (vim_strsize(s) > MSG_BUF_CLEN)
957 {
958 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
959 s = buf;
960 }
Bram Moolenaar32526b32019-01-19 17:43:09 +0100961 msg_puts((char *)s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200962 vim_free(tofree);
963 }
964 }
965 }
Bram Moolenaar32526b32019-01-19 17:43:09 +0100966 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200967 }
Bram Moolenaar32526b32019-01-19 17:43:09 +0100968 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200969
970 verbose_leave_scroll();
971 --no_wait_return;
972 }
973 }
974#ifdef FEAT_PROFILE
975 if (do_profiling == PROF_YES)
976 {
977 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +0200978 {
979 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200980 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +0200981 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200982 if (fp->uf_profiling
983 || (fc->caller != NULL && fc->caller->func->uf_profiling))
984 {
985 ++fp->uf_tm_count;
986 profile_start(&call_start);
987 profile_zero(&fp->uf_tm_children);
988 }
989 script_prof_save(&wait_start);
990 }
991#endif
992
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200993 save_current_sctx = current_sctx;
994 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200995 save_did_emsg = did_emsg;
996 did_emsg = FALSE;
997
998 /* call do_cmdline() to execute the lines */
999 do_cmdline(NULL, get_func_line, (void *)fc,
1000 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1001
1002 --RedrawingDisabled;
1003
1004 /* when the function was aborted because of an error, return -1 */
1005 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1006 {
1007 clear_tv(rettv);
1008 rettv->v_type = VAR_NUMBER;
1009 rettv->vval.v_number = -1;
1010 }
1011
1012#ifdef FEAT_PROFILE
1013 if (do_profiling == PROF_YES && (fp->uf_profiling
1014 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1015 {
1016 profile_end(&call_start);
1017 profile_sub_wait(&wait_start, &call_start);
1018 profile_add(&fp->uf_tm_total, &call_start);
1019 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1020 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1021 {
1022 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1023 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1024 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001025 if (started_profiling)
1026 // make a ":profdel func" stop profiling the function
1027 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001028 }
1029#endif
1030
1031 /* when being verbose, mention the return value */
1032 if (p_verbose >= 12)
1033 {
1034 ++no_wait_return;
1035 verbose_enter_scroll();
1036
1037 if (aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001038 smsg(_("%s aborted"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001039 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001040 smsg(_("%s returning #%ld"), sourcing_name,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001041 (long)fc->rettv->vval.v_number);
1042 else
1043 {
1044 char_u buf[MSG_BUF_LEN];
1045 char_u numbuf2[NUMBUFLEN];
1046 char_u *tofree;
1047 char_u *s;
1048
1049 /* The value may be very long. Skip the middle part, so that we
1050 * have some idea how it starts and ends. smsg() would always
1051 * truncate it at the end. Don't want errors such as E724 here. */
1052 ++emsg_off;
1053 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1054 --emsg_off;
1055 if (s != NULL)
1056 {
1057 if (vim_strsize(s) > MSG_BUF_CLEN)
1058 {
1059 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1060 s = buf;
1061 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001062 smsg(_("%s returning %s"), sourcing_name, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001063 vim_free(tofree);
1064 }
1065 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001066 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001067
1068 verbose_leave_scroll();
1069 --no_wait_return;
1070 }
1071
1072 vim_free(sourcing_name);
1073 sourcing_name = save_sourcing_name;
1074 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001075 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001076#ifdef FEAT_PROFILE
1077 if (do_profiling == PROF_YES)
1078 script_prof_restore(&wait_start);
1079#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001080 if (using_sandbox)
1081 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001082
1083 if (p_verbose >= 12 && sourcing_name != NULL)
1084 {
1085 ++no_wait_return;
1086 verbose_enter_scroll();
1087
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001088 smsg(_("continuing in %s"), sourcing_name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001089 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001090
1091 verbose_leave_scroll();
1092 --no_wait_return;
1093 }
1094
1095 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096 --depth;
1097
Bram Moolenaar6914c642017-04-01 21:21:30 +02001098 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001099}
1100
1101/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001102 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001103 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001104 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001105 */
1106 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001107funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001108{
1109 funccall_T **pfc;
1110 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001111
1112 if (fc == NULL)
1113 return;
1114
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001115 if (--fc->fc_refcount <= 0 && (force || (
1116 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001117 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001118 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001119 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001120 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001121 if (fc == *pfc)
1122 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001123 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001124 free_funccal_contents(fc);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001125 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001126 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001127 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001128 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001129 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001130 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001131}
1132
1133/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001134 * Remove the function from the function hashtable. If the function was
1135 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001136 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001137 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001138 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001139func_remove(ufunc_T *fp)
1140{
1141 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1142
1143 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001144 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001145 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001146 return TRUE;
1147 }
1148 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001149}
1150
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001151 static void
1152func_clear_items(ufunc_T *fp)
1153{
1154 ga_clear_strings(&(fp->uf_args));
1155 ga_clear_strings(&(fp->uf_lines));
1156#ifdef FEAT_PROFILE
1157 vim_free(fp->uf_tml_count);
1158 fp->uf_tml_count = NULL;
1159 vim_free(fp->uf_tml_total);
1160 fp->uf_tml_total = NULL;
1161 vim_free(fp->uf_tml_self);
1162 fp->uf_tml_self = NULL;
1163#endif
1164}
1165
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001166/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001167 * Free all things that a function contains. Does not free the function
1168 * itself, use func_free() for that.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001169 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001170 */
1171 static void
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001172func_clear(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001173{
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001174 if (fp->uf_cleared)
1175 return;
1176 fp->uf_cleared = TRUE;
1177
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001178 /* clear this function */
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001179 func_clear_items(fp);
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001180 funccal_unref(fp->uf_scoped, fp, force);
1181}
1182
1183/*
1184 * Free a function and remove it from the list of functions. Does not free
1185 * what a function contains, call func_clear() first.
1186 */
1187 static void
1188func_free(ufunc_T *fp)
1189{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001190 /* only remove it when not done already, otherwise we would remove a newer
1191 * version of the function */
1192 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1193 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001194
1195 vim_free(fp);
1196}
1197
Bram Moolenaarc2574872016-08-11 22:51:05 +02001198/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001199 * Free all things that a function contains and free the function itself.
1200 * When "force" is TRUE we are exiting.
1201 */
1202 static void
1203func_clear_free(ufunc_T *fp, int force)
1204{
1205 func_clear(fp, force);
1206 func_free(fp);
1207}
1208
1209/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001210 * There are two kinds of function names:
1211 * 1. ordinary names, function defined with :function
1212 * 2. numbered functions and lambdas
1213 * For the first we only count the name stored in func_hashtab as a reference,
1214 * using function() does not count as a reference, because the function is
1215 * looked up by name.
1216 */
1217 static int
1218func_name_refcount(char_u *name)
1219{
1220 return isdigit(*name) || *name == '<';
1221}
1222
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001223static funccal_entry_T *funccal_stack = NULL;
1224
1225/*
1226 * Save the current function call pointer, and set it to NULL.
1227 * Used when executing autocommands and for ":source".
1228 */
1229 void
1230save_funccal(funccal_entry_T *entry)
1231{
1232 entry->top_funccal = current_funccal;
1233 entry->next = funccal_stack;
1234 funccal_stack = entry;
1235 current_funccal = NULL;
1236}
1237
1238 void
1239restore_funccal(void)
1240{
1241 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001242 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001243 else
1244 {
1245 current_funccal = funccal_stack->top_funccal;
1246 funccal_stack = funccal_stack->next;
1247 }
1248}
1249
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250#if defined(EXITFREE) || defined(PROTO)
1251 void
1252free_all_functions(void)
1253{
1254 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001255 ufunc_T *fp;
1256 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001257 long_u todo = 1;
1258 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001259
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001260 /* Clean up the current_funccal chain and the funccal stack. */
Bram Moolenaar6914c642017-04-01 21:21:30 +02001261 while (current_funccal != NULL)
1262 {
1263 clear_tv(current_funccal->rettv);
1264 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001265 if (current_funccal == NULL && funccal_stack != NULL)
1266 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001267 }
1268
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001269 /* First clear what the functions contain. Since this may lower the
1270 * reference count of a function, it may also free a function and change
1271 * the hash table. Restart if that happens. */
1272 while (todo > 0)
1273 {
1274 todo = func_hashtab.ht_used;
1275 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1276 if (!HASHITEM_EMPTY(hi))
1277 {
1278 /* Only free functions that are not refcounted, those are
1279 * supposed to be freed when no longer referenced. */
1280 fp = HI2UF(hi);
1281 if (func_name_refcount(fp->uf_name))
1282 ++skipped;
1283 else
1284 {
1285 used = func_hashtab.ht_used;
1286 func_clear(fp, TRUE);
1287 if (used != func_hashtab.ht_used)
1288 {
1289 skipped = 0;
1290 break;
1291 }
1292 }
1293 --todo;
1294 }
1295 }
1296
1297 /* Now actually free the functions. Need to start all over every time,
1298 * because func_free() may change the hash table. */
1299 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001300 while (func_hashtab.ht_used > skipped)
1301 {
1302 todo = func_hashtab.ht_used;
1303 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001304 if (!HASHITEM_EMPTY(hi))
1305 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001306 --todo;
1307 /* Only free functions that are not refcounted, those are
1308 * supposed to be freed when no longer referenced. */
1309 fp = HI2UF(hi);
1310 if (func_name_refcount(fp->uf_name))
1311 ++skipped;
1312 else
1313 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001314 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001315 skipped = 0;
1316 break;
1317 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001318 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001319 }
1320 if (skipped == 0)
1321 hash_clear(&func_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001322}
1323#endif
1324
1325/*
1326 * Return TRUE if "name" looks like a builtin function name: starts with a
1327 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1328 * "len" is the length of "name", or -1 for NUL terminated.
1329 */
1330 static int
1331builtin_function(char_u *name, int len)
1332{
1333 char_u *p;
1334
1335 if (!ASCII_ISLOWER(name[0]))
1336 return FALSE;
1337 p = vim_strchr(name, AUTOLOAD_CHAR);
1338 return p == NULL || (len > 0 && p > name + len);
1339}
1340
1341 int
1342func_call(
1343 char_u *name,
1344 typval_T *args,
1345 partial_T *partial,
1346 dict_T *selfdict,
1347 typval_T *rettv)
1348{
1349 listitem_T *item;
1350 typval_T argv[MAX_FUNC_ARGS + 1];
1351 int argc = 0;
1352 int dummy;
1353 int r = 0;
1354
1355 for (item = args->vval.v_list->lv_first; item != NULL;
1356 item = item->li_next)
1357 {
1358 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1359 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001360 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 break;
1362 }
1363 /* Make a copy of each argument. This is needed to be able to set
1364 * v_lock to VAR_FIXED in the copy without changing the original list.
1365 */
1366 copy_tv(&item->li_tv, &argv[argc++]);
1367 }
1368
1369 if (item == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001370 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001371 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
1372 &dummy, TRUE, partial, selfdict);
1373
1374 /* Free the arguments. */
1375 while (argc > 0)
1376 clear_tv(&argv[--argc]);
1377
1378 return r;
1379}
1380
1381/*
1382 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001383 *
1384 * "argv_func", when not NULL, can be used to fill in arguments only when the
1385 * invoked function uses them. It is called like this:
1386 * new_argcount = argv_func(current_argcount, argv, called_func_argcount)
1387 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001388 * Return FAIL when the function can't be called, OK otherwise.
1389 * Also returns OK when an error was encountered while executing the function.
1390 */
1391 int
1392call_func(
1393 char_u *funcname, /* name of the function */
1394 int len, /* length of "name" */
1395 typval_T *rettv, /* return value goes here */
1396 int argcount_in, /* number of "argvars" */
1397 typval_T *argvars_in, /* vars for arguments, must have "argcount"
1398 PLUS ONE elements! */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001399 int (* argv_func)(int, typval_T *, int),
1400 /* function to fill in argvars */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001401 linenr_T firstline, /* first line of range */
1402 linenr_T lastline, /* last line of range */
1403 int *doesrange, /* return: function handled range */
1404 int evaluate,
1405 partial_T *partial, /* optional, can be NULL */
1406 dict_T *selfdict_in) /* Dictionary for "self" */
1407{
1408 int ret = FAIL;
1409 int error = ERROR_NONE;
1410 int i;
1411 ufunc_T *fp;
1412 char_u fname_buf[FLEN_FIXED + 1];
1413 char_u *tofree = NULL;
1414 char_u *fname;
1415 char_u *name;
1416 int argcount = argcount_in;
1417 typval_T *argvars = argvars_in;
1418 dict_T *selfdict = selfdict_in;
1419 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
1420 int argv_clear = 0;
1421
1422 /* Make a copy of the name, if it comes from a funcref variable it could
1423 * be changed or deleted in the called function. */
1424 name = vim_strnsave(funcname, len);
1425 if (name == NULL)
1426 return ret;
1427
1428 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1429
1430 *doesrange = FALSE;
1431
1432 if (partial != NULL)
1433 {
1434 /* When the function has a partial with a dict and there is a dict
1435 * argument, use the dict argument. That is backwards compatible.
1436 * When the dict was bound explicitly use the one from the partial. */
1437 if (partial->pt_dict != NULL
1438 && (selfdict_in == NULL || !partial->pt_auto))
1439 selfdict = partial->pt_dict;
1440 if (error == ERROR_NONE && partial->pt_argc > 0)
1441 {
1442 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
1443 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
1444 for (i = 0; i < argcount_in; ++i)
1445 argv[i + argv_clear] = argvars_in[i];
1446 argvars = argv;
1447 argcount = partial->pt_argc + argcount_in;
1448 }
1449 }
1450
1451
Bram Moolenaarb4518562018-05-22 18:31:35 +02001452 /*
1453 * Execute the function if executing and no errors were detected.
1454 */
1455 if (!evaluate)
1456 {
1457 // Not evaluating, which means the return value is unknown. This
1458 // matters for giving error messages.
1459 rettv->v_type = VAR_UNKNOWN;
1460 }
1461 else if (error == ERROR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001462 {
1463 char_u *rfname = fname;
1464
1465 /* Ignore "g:" before a function name. */
1466 if (fname[0] == 'g' && fname[1] == ':')
1467 rfname = fname + 2;
1468
1469 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
1470 rettv->vval.v_number = 0;
1471 error = ERROR_UNKNOWN;
1472
1473 if (!builtin_function(rfname, -1))
1474 {
1475 /*
1476 * User defined function.
1477 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001478 if (partial != NULL && partial->pt_func != NULL)
1479 fp = partial->pt_func;
1480 else
1481 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001482
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001483 /* Trigger FuncUndefined event, may load the function. */
1484 if (fp == NULL
1485 && apply_autocmds(EVENT_FUNCUNDEFINED,
1486 rfname, rfname, TRUE, NULL)
1487 && !aborting())
1488 {
1489 /* executed an autocommand, search for the function again */
1490 fp = find_func(rfname);
1491 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001492 /* Try loading a package. */
1493 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1494 {
1495 /* loaded a package, search for the function again */
1496 fp = find_func(rfname);
1497 }
1498
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001499 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1500 error = ERROR_DELETED;
1501 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001502 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001503 if (argv_func != NULL)
1504 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len);
1505
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001506 if (fp->uf_flags & FC_RANGE)
1507 *doesrange = TRUE;
1508 if (argcount < fp->uf_args.ga_len)
1509 error = ERROR_TOOFEW;
1510 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1511 error = ERROR_TOOMANY;
1512 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1513 error = ERROR_DICT;
1514 else
1515 {
1516 int did_save_redo = FALSE;
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001517 save_redo_T save_redo;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001518
1519 /*
1520 * Call the user function.
1521 * Save and restore search patterns, script variables and
1522 * redo buffer.
1523 */
1524 save_search_patterns();
1525#ifdef FEAT_INS_EXPAND
1526 if (!ins_compl_active())
1527#endif
1528 {
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001529 saveRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001530 did_save_redo = TRUE;
1531 }
1532 ++fp->uf_calls;
1533 call_user_func(fp, argcount, argvars, rettv,
1534 firstline, lastline,
1535 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001536 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001537 /* Function was unreferenced while being used, free it
1538 * now. */
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001539 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 if (did_save_redo)
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001541 restoreRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542 restore_search_patterns();
1543 error = ERROR_NONE;
1544 }
1545 }
1546 }
1547 else
1548 {
1549 /*
1550 * Find the function name in the table, call its implementation.
1551 */
1552 error = call_internal_func(fname, argcount, argvars, rettv);
1553 }
1554 /*
1555 * The function call (or "FuncUndefined" autocommand sequence) might
1556 * have been aborted by an error, an interrupt, or an explicitly thrown
1557 * exception that has not been caught so far. This situation can be
1558 * tested for by calling aborting(). For an error in an internal
1559 * function or for the "E132" error in call_user_func(), however, the
1560 * throw point at which the "force_abort" flag (temporarily reset by
1561 * emsg()) is normally updated has not been reached yet. We need to
1562 * update that flag first to make aborting() reliable.
1563 */
1564 update_force_abort();
1565 }
1566 if (error == ERROR_NONE)
1567 ret = OK;
1568
1569 /*
1570 * Report an error unless the argument evaluation or function call has been
1571 * cancelled due to an aborting error, an interrupt, or an exception.
1572 */
1573 if (!aborting())
1574 {
1575 switch (error)
1576 {
1577 case ERROR_UNKNOWN:
1578 emsg_funcname(N_("E117: Unknown function: %s"), name);
1579 break;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001580 case ERROR_DELETED:
1581 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1582 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001583 case ERROR_TOOMANY:
1584 emsg_funcname((char *)e_toomanyarg, name);
1585 break;
1586 case ERROR_TOOFEW:
1587 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
1588 name);
1589 break;
1590 case ERROR_SCRIPT:
1591 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
1592 name);
1593 break;
1594 case ERROR_DICT:
1595 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
1596 name);
1597 break;
1598 }
1599 }
1600
1601 while (argv_clear > 0)
1602 clear_tv(&argv[--argv_clear]);
1603 vim_free(tofree);
1604 vim_free(name);
1605
1606 return ret;
1607}
1608
1609/*
1610 * List the head of the function: "name(arg1, arg2)".
1611 */
1612 static void
1613list_func_head(ufunc_T *fp, int indent)
1614{
1615 int j;
1616
1617 msg_start();
1618 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001619 msg_puts(" ");
1620 msg_puts("function ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 if (fp->uf_name[0] == K_SPECIAL)
1622 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001623 msg_puts_attr("<SNR>", HL_ATTR(HLF_8));
1624 msg_puts((char *)fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 }
1626 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001627 msg_puts((char *)fp->uf_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628 msg_putchar('(');
1629 for (j = 0; j < fp->uf_args.ga_len; ++j)
1630 {
1631 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001632 msg_puts(", ");
1633 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634 }
1635 if (fp->uf_varargs)
1636 {
1637 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001638 msg_puts(", ");
1639 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 }
1641 msg_putchar(')');
1642 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001643 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001644 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001645 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001646 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001647 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001648 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001649 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001650 msg_clr_eos();
1651 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001652 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653}
1654
1655/*
1656 * Get a function name, translating "<SID>" and "<SNR>".
1657 * Also handles a Funcref in a List or Dictionary.
1658 * Returns the function name in allocated memory, or NULL for failure.
1659 * flags:
1660 * TFN_INT: internal function name OK
1661 * TFN_QUIET: be quiet
1662 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001663 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001664 * Advances "pp" to just after the function name (if no error).
1665 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001666 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001667trans_function_name(
1668 char_u **pp,
1669 int skip, /* only find the end, don't evaluate */
1670 int flags,
1671 funcdict_T *fdp, /* return: info about dictionary used */
1672 partial_T **partial) /* return: partial of a FuncRef */
1673{
1674 char_u *name = NULL;
1675 char_u *start;
1676 char_u *end;
1677 int lead;
1678 char_u sid_buf[20];
1679 int len;
1680 lval_T lv;
1681
1682 if (fdp != NULL)
1683 vim_memset(fdp, 0, sizeof(funcdict_T));
1684 start = *pp;
1685
1686 /* Check for hard coded <SNR>: already translated function ID (from a user
1687 * command). */
1688 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1689 && (*pp)[2] == (int)KE_SNR)
1690 {
1691 *pp += 3;
1692 len = get_id_len(pp) + 3;
1693 return vim_strnsave(start, len);
1694 }
1695
1696 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
1697 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
1698 lead = eval_fname_script(start);
1699 if (lead > 2)
1700 start += lead;
1701
1702 /* Note that TFN_ flags use the same values as GLV_ flags. */
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001703 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001704 lead > 2 ? 0 : FNE_CHECK_START);
1705 if (end == start)
1706 {
1707 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001708 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001709 goto theend;
1710 }
1711 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1712 {
1713 /*
1714 * Report an invalid expression in braces, unless the expression
1715 * evaluation has been cancelled due to an aborting error, an
1716 * interrupt, or an exception.
1717 */
1718 if (!aborting())
1719 {
1720 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001721 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001722 }
1723 else
1724 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1725 goto theend;
1726 }
1727
1728 if (lv.ll_tv != NULL)
1729 {
1730 if (fdp != NULL)
1731 {
1732 fdp->fd_dict = lv.ll_dict;
1733 fdp->fd_newkey = lv.ll_newkey;
1734 lv.ll_newkey = NULL;
1735 fdp->fd_di = lv.ll_di;
1736 }
1737 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1738 {
1739 name = vim_strsave(lv.ll_tv->vval.v_string);
1740 *pp = end;
1741 }
1742 else if (lv.ll_tv->v_type == VAR_PARTIAL
1743 && lv.ll_tv->vval.v_partial != NULL)
1744 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001745 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 *pp = end;
1747 if (partial != NULL)
1748 *partial = lv.ll_tv->vval.v_partial;
1749 }
1750 else
1751 {
1752 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1753 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001754 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001755 else
1756 *pp = end;
1757 name = NULL;
1758 }
1759 goto theend;
1760 }
1761
1762 if (lv.ll_name == NULL)
1763 {
1764 /* Error found, but continue after the function name. */
1765 *pp = end;
1766 goto theend;
1767 }
1768
1769 /* Check if the name is a Funcref. If so, use the value. */
1770 if (lv.ll_exp_name != NULL)
1771 {
1772 len = (int)STRLEN(lv.ll_exp_name);
1773 name = deref_func_name(lv.ll_exp_name, &len, partial,
1774 flags & TFN_NO_AUTOLOAD);
1775 if (name == lv.ll_exp_name)
1776 name = NULL;
1777 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001778 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001779 {
1780 len = (int)(end - *pp);
1781 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1782 if (name == *pp)
1783 name = NULL;
1784 }
1785 if (name != NULL)
1786 {
1787 name = vim_strsave(name);
1788 *pp = end;
1789 if (STRNCMP(name, "<SNR>", 5) == 0)
1790 {
1791 /* Change "<SNR>" to the byte sequence. */
1792 name[0] = K_SPECIAL;
1793 name[1] = KS_EXTRA;
1794 name[2] = (int)KE_SNR;
1795 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1796 }
1797 goto theend;
1798 }
1799
1800 if (lv.ll_exp_name != NULL)
1801 {
1802 len = (int)STRLEN(lv.ll_exp_name);
1803 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1804 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1805 {
1806 /* When there was "s:" already or the name expanded to get a
1807 * leading "s:" then remove it. */
1808 lv.ll_name += 2;
1809 len -= 2;
1810 lead = 2;
1811 }
1812 }
1813 else
1814 {
1815 /* skip over "s:" and "g:" */
1816 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1817 lv.ll_name += 2;
1818 len = (int)(end - lv.ll_name);
1819 }
1820
1821 /*
1822 * Copy the function name to allocated memory.
1823 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1824 * Accept <SNR>123_name() outside a script.
1825 */
1826 if (skip)
1827 lead = 0; /* do nothing */
1828 else if (lead > 0)
1829 {
1830 lead = 3;
1831 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1832 || eval_fname_sid(*pp))
1833 {
1834 /* It's "s:" or "<SID>" */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001835 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001836 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 goto theend;
1839 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001840 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001841 lead += (int)STRLEN(sid_buf);
1842 }
1843 }
1844 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1845 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001846 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001847 start);
1848 goto theend;
1849 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001850 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 {
1852 char_u *cp = vim_strchr(lv.ll_name, ':');
1853
1854 if (cp != NULL && cp < end)
1855 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001856 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001857 goto theend;
1858 }
1859 }
1860
1861 name = alloc((unsigned)(len + lead + 1));
1862 if (name != NULL)
1863 {
1864 if (lead > 0)
1865 {
1866 name[0] = K_SPECIAL;
1867 name[1] = KS_EXTRA;
1868 name[2] = (int)KE_SNR;
1869 if (lead > 3) /* If it's "<SID>" */
1870 STRCPY(name + 3, sid_buf);
1871 }
1872 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1873 name[lead + len] = NUL;
1874 }
1875 *pp = end;
1876
1877theend:
1878 clear_lval(&lv);
1879 return name;
1880}
1881
1882/*
1883 * ":function"
1884 */
1885 void
1886ex_function(exarg_T *eap)
1887{
1888 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02001889 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001890 int j;
1891 int c;
1892 int saved_did_emsg;
1893 int saved_wait_return = need_wait_return;
1894 char_u *name = NULL;
1895 char_u *p;
1896 char_u *arg;
1897 char_u *line_arg = NULL;
1898 garray_T newargs;
1899 garray_T newlines;
1900 int varargs = FALSE;
1901 int flags = 0;
1902 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001903 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001904 int indent;
1905 int nesting;
1906 char_u *skip_until = NULL;
1907 dictitem_T *v;
1908 funcdict_T fudi;
1909 static int func_nr = 0; /* number for nameless function */
1910 int paren;
1911 hashtab_T *ht;
1912 int todo;
1913 hashitem_T *hi;
1914 int sourcing_lnum_off;
1915
1916 /*
1917 * ":function" without argument: list functions.
1918 */
1919 if (ends_excmd(*eap->arg))
1920 {
1921 if (!eap->skip)
1922 {
1923 todo = (int)func_hashtab.ht_used;
1924 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
1925 {
1926 if (!HASHITEM_EMPTY(hi))
1927 {
1928 --todo;
1929 fp = HI2UF(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001930 if (message_filtered(fp->uf_name))
1931 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001932 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001933 list_func_head(fp, FALSE);
1934 }
1935 }
1936 }
1937 eap->nextcmd = check_nextcmd(eap->arg);
1938 return;
1939 }
1940
1941 /*
1942 * ":function /pat": list functions matching pattern.
1943 */
1944 if (*eap->arg == '/')
1945 {
1946 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
1947 if (!eap->skip)
1948 {
1949 regmatch_T regmatch;
1950
1951 c = *p;
1952 *p = NUL;
1953 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
1954 *p = c;
1955 if (regmatch.regprog != NULL)
1956 {
1957 regmatch.rm_ic = p_ic;
1958
1959 todo = (int)func_hashtab.ht_used;
1960 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
1961 {
1962 if (!HASHITEM_EMPTY(hi))
1963 {
1964 --todo;
1965 fp = HI2UF(hi);
1966 if (!isdigit(*fp->uf_name)
1967 && vim_regexec(&regmatch, fp->uf_name, 0))
1968 list_func_head(fp, FALSE);
1969 }
1970 }
1971 vim_regfree(regmatch.regprog);
1972 }
1973 }
1974 if (*p == '/')
1975 ++p;
1976 eap->nextcmd = check_nextcmd(p);
1977 return;
1978 }
1979
1980 /*
1981 * Get the function name. There are these situations:
1982 * func normal function name
1983 * "name" == func, "fudi.fd_dict" == NULL
1984 * dict.func new dictionary entry
1985 * "name" == NULL, "fudi.fd_dict" set,
1986 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
1987 * dict.func existing dict entry with a Funcref
1988 * "name" == func, "fudi.fd_dict" set,
1989 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
1990 * dict.func existing dict entry that's not a Funcref
1991 * "name" == NULL, "fudi.fd_dict" set,
1992 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
1993 * s:func script-local function name
1994 * g:func global function name, same as "func"
1995 */
1996 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01001997 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001998 paren = (vim_strchr(p, '(') != NULL);
1999 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2000 {
2001 /*
2002 * Return on an invalid expression in braces, unless the expression
2003 * evaluation has been cancelled due to an aborting error, an
2004 * interrupt, or an exception.
2005 */
2006 if (!aborting())
2007 {
2008 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002009 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002010 vim_free(fudi.fd_newkey);
2011 return;
2012 }
2013 else
2014 eap->skip = TRUE;
2015 }
2016
2017 /* An error in a function call during evaluation of an expression in magic
2018 * braces should not cause the function not to be defined. */
2019 saved_did_emsg = did_emsg;
2020 did_emsg = FALSE;
2021
2022 /*
2023 * ":function func" with only function name: list function.
2024 */
2025 if (!paren)
2026 {
2027 if (!ends_excmd(*skipwhite(p)))
2028 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002029 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002030 goto ret_free;
2031 }
2032 eap->nextcmd = check_nextcmd(p);
2033 if (eap->nextcmd != NULL)
2034 *p = NUL;
2035 if (!eap->skip && !got_int)
2036 {
2037 fp = find_func(name);
2038 if (fp != NULL)
2039 {
2040 list_func_head(fp, TRUE);
2041 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2042 {
2043 if (FUNCLINE(fp, j) == NULL)
2044 continue;
2045 msg_putchar('\n');
2046 msg_outnum((long)(j + 1));
2047 if (j < 9)
2048 msg_putchar(' ');
2049 if (j < 99)
2050 msg_putchar(' ');
2051 msg_prt_line(FUNCLINE(fp, j), FALSE);
2052 out_flush(); /* show a line at a time */
2053 ui_breakcheck();
2054 }
2055 if (!got_int)
2056 {
2057 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01002058 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002059 }
2060 }
2061 else
2062 emsg_funcname(N_("E123: Undefined function: %s"), name);
2063 }
2064 goto ret_free;
2065 }
2066
2067 /*
2068 * ":function name(arg1, arg2)" Define function.
2069 */
2070 p = skipwhite(p);
2071 if (*p != '(')
2072 {
2073 if (!eap->skip)
2074 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002075 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002076 goto ret_free;
2077 }
2078 /* attempt to continue by skipping some text */
2079 if (vim_strchr(p, '(') != NULL)
2080 p = vim_strchr(p, '(');
2081 }
2082 p = skipwhite(p + 1);
2083
2084 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2085
2086 if (!eap->skip)
2087 {
2088 /* Check the name of the function. Unless it's a dictionary function
2089 * (that we are overwriting). */
2090 if (name != NULL)
2091 arg = name;
2092 else
2093 arg = fudi.fd_newkey;
2094 if (arg != NULL && (fudi.fd_di == NULL
2095 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2096 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2097 {
2098 if (*arg == K_SPECIAL)
2099 j = 3;
2100 else
2101 j = 0;
2102 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2103 : eval_isnamec(arg[j])))
2104 ++j;
2105 if (arg[j] != NUL)
2106 emsg_funcname((char *)e_invarg2, arg);
2107 }
2108 /* Disallow using the g: dict. */
2109 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002110 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002111 }
2112
2113 if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL)
2114 goto errret_2;
2115
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002116 /* find extra arguments "range", "dict", "abort" and "closure" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002117 for (;;)
2118 {
2119 p = skipwhite(p);
2120 if (STRNCMP(p, "range", 5) == 0)
2121 {
2122 flags |= FC_RANGE;
2123 p += 5;
2124 }
2125 else if (STRNCMP(p, "dict", 4) == 0)
2126 {
2127 flags |= FC_DICT;
2128 p += 4;
2129 }
2130 else if (STRNCMP(p, "abort", 5) == 0)
2131 {
2132 flags |= FC_ABORT;
2133 p += 5;
2134 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002135 else if (STRNCMP(p, "closure", 7) == 0)
2136 {
2137 flags |= FC_CLOSURE;
2138 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02002139 if (current_funccal == NULL)
2140 {
Bram Moolenaarba209902016-08-24 22:06:38 +02002141 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
Bram Moolenaar58016442016-07-31 18:30:22 +02002142 name == NULL ? (char_u *)"" : name);
2143 goto erret;
2144 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002145 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002146 else
2147 break;
2148 }
2149
2150 /* When there is a line break use what follows for the function body.
2151 * Makes 'exe "func Test()\n...\nendfunc"' work. */
2152 if (*p == '\n')
2153 line_arg = p + 1;
2154 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002155 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002156
2157 /*
2158 * Read the body of the function, until ":endfunction" is found.
2159 */
2160 if (KeyTyped)
2161 {
2162 /* Check if the function already exists, don't let the user type the
2163 * whole function before telling him it doesn't work! For a script we
2164 * need to skip the body to be able to find what follows. */
2165 if (!eap->skip && !eap->forceit)
2166 {
2167 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002168 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002169 else if (name != NULL && find_func(name) != NULL)
2170 emsg_funcname(e_funcexts, name);
2171 }
2172
2173 if (!eap->skip && did_emsg)
2174 goto erret;
2175
2176 msg_putchar('\n'); /* don't overwrite the function name */
2177 cmdline_row = msg_row;
2178 }
2179
2180 indent = 2;
2181 nesting = 0;
2182 for (;;)
2183 {
2184 if (KeyTyped)
2185 {
2186 msg_scroll = TRUE;
2187 saved_wait_return = FALSE;
2188 }
2189 need_wait_return = FALSE;
2190 sourcing_lnum_off = sourcing_lnum;
2191
2192 if (line_arg != NULL)
2193 {
2194 /* Use eap->arg, split up in parts by line breaks. */
2195 theline = line_arg;
2196 p = vim_strchr(theline, '\n');
2197 if (p == NULL)
2198 line_arg += STRLEN(line_arg);
2199 else
2200 {
2201 *p = NUL;
2202 line_arg = p + 1;
2203 }
2204 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002206 {
2207 vim_free(line_to_free);
2208 if (eap->getline == NULL)
2209 theline = getcmdline(':', 0L, indent);
2210 else
2211 theline = eap->getline(':', eap->cookie, indent);
2212 line_to_free = theline;
2213 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002214 if (KeyTyped)
2215 lines_left = Rows - 1;
2216 if (theline == NULL)
2217 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002218 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002219 goto erret;
2220 }
2221
2222 /* Detect line continuation: sourcing_lnum increased more than one. */
2223 if (sourcing_lnum > sourcing_lnum_off + 1)
2224 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
2225 else
2226 sourcing_lnum_off = 0;
2227
2228 if (skip_until != NULL)
2229 {
2230 /* between ":append" and "." and between ":python <<EOF" and "EOF"
2231 * don't check for ":endfunc". */
2232 if (STRCMP(theline, skip_until) == 0)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002233 VIM_CLEAR(skip_until);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002234 }
2235 else
2236 {
2237 /* skip ':' and blanks*/
Bram Moolenaar1c465442017-03-12 20:10:05 +01002238 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002239 ;
2240
2241 /* Check for "endfunction". */
2242 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2243 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002244 char_u *nextcmd = NULL;
2245
Bram Moolenaar663bb232017-06-22 19:12:10 +02002246 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002247 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002248 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002249 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002250 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002251 give_warning2(
2252 (char_u *)_("W22: Text found after :endfunction: %s"),
2253 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002254 if (nextcmd != NULL)
2255 {
2256 /* Another command follows. If the line came from "eap" we
2257 * can simply point into it, otherwise we need to change
2258 * "eap->cmdlinep". */
2259 eap->nextcmd = nextcmd;
2260 if (line_to_free != NULL)
2261 {
2262 vim_free(*eap->cmdlinep);
2263 *eap->cmdlinep = line_to_free;
2264 line_to_free = NULL;
2265 }
2266 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002267 break;
2268 }
2269
2270 /* Increase indent inside "if", "while", "for" and "try", decrease
2271 * at "end". */
2272 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2273 indent -= 2;
2274 else if (STRNCMP(p, "if", 2) == 0
2275 || STRNCMP(p, "wh", 2) == 0
2276 || STRNCMP(p, "for", 3) == 0
2277 || STRNCMP(p, "try", 3) == 0)
2278 indent += 2;
2279
2280 /* Check for defining a function inside this function. */
2281 if (checkforcmd(&p, "function", 2))
2282 {
2283 if (*p == '!')
2284 p = skipwhite(p + 1);
2285 p += eval_fname_script(p);
2286 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2287 if (*skipwhite(p) == '(')
2288 {
2289 ++nesting;
2290 indent += 2;
2291 }
2292 }
2293
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002294 /* Check for ":append", ":change", ":insert". */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002295 p = skip_range(p, NULL);
2296 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002297 || (p[0] == 'c'
2298 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2299 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2300 && (STRNCMP(&p[3], "nge", 3) != 0
2301 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002302 || (p[0] == 'i'
2303 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2304 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2305 skip_until = vim_strsave((char_u *)".");
2306
2307 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
2308 arg = skipwhite(skiptowhite(p));
2309 if (arg[0] == '<' && arg[1] =='<'
2310 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002311 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2312 || ((p[2] == '3' || p[2] == 'x')
2313 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002314 || (p[0] == 'p' && p[1] == 'e'
2315 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2316 || (p[0] == 't' && p[1] == 'c'
2317 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2318 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2319 && !ASCII_ISALPHA(p[3]))
2320 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2321 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2322 || (p[0] == 'm' && p[1] == 'z'
2323 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2324 ))
2325 {
2326 /* ":python <<" continues until a dot, like ":append" */
2327 p = skipwhite(arg + 2);
2328 if (*p == NUL)
2329 skip_until = vim_strsave((char_u *)".");
2330 else
2331 skip_until = vim_strsave(p);
2332 }
2333 }
2334
2335 /* Add the line to the function. */
2336 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002337 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338
2339 /* Copy the line to newly allocated memory. get_one_sourceline()
2340 * allocates 250 bytes per line, this saves 80% on average. The cost
2341 * is an extra alloc/free. */
2342 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002343 if (p == NULL)
2344 goto erret;
2345 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002346
2347 /* Add NULL lines for continuation lines, so that the line count is
2348 * equal to the index in the growarray. */
2349 while (sourcing_lnum_off-- > 0)
2350 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2351
2352 /* Check for end of eap->arg. */
2353 if (line_arg != NULL && *line_arg == NUL)
2354 line_arg = NULL;
2355 }
2356
2357 /* Don't define the function when skipping commands or when an error was
2358 * detected. */
2359 if (eap->skip || did_emsg)
2360 goto erret;
2361
2362 /*
2363 * If there are no errors, add the function
2364 */
2365 if (fudi.fd_dict == NULL)
2366 {
2367 v = find_var(name, &ht, FALSE);
2368 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2369 {
2370 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2371 name);
2372 goto erret;
2373 }
2374
2375 fp = find_func(name);
2376 if (fp != NULL)
2377 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002378 // Function can be replaced with "function!" and when sourcing the
2379 // same script again, but only once.
2380 if (!eap->forceit
2381 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2382 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002383 {
2384 emsg_funcname(e_funcexts, name);
2385 goto erret;
2386 }
2387 if (fp->uf_calls > 0)
2388 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002389 emsg_funcname(
2390 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002391 name);
2392 goto erret;
2393 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002394 if (fp->uf_refcount > 1)
2395 {
2396 /* This function is referenced somewhere, don't redefine it but
2397 * create a new one. */
2398 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002399 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002400 fp = NULL;
2401 overwrite = TRUE;
2402 }
2403 else
2404 {
2405 /* redefine existing function */
Bram Moolenaard23a8232018-02-10 18:45:26 +01002406 VIM_CLEAR(name);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002407 func_clear_items(fp);
2408#ifdef FEAT_PROFILE
2409 fp->uf_profiling = FALSE;
2410 fp->uf_prof_initialized = FALSE;
2411#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002412 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002413 }
2414 }
2415 else
2416 {
2417 char numbuf[20];
2418
2419 fp = NULL;
2420 if (fudi.fd_newkey == NULL && !eap->forceit)
2421 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002422 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002423 goto erret;
2424 }
2425 if (fudi.fd_di == NULL)
2426 {
2427 /* Can't add a function to a locked dictionary */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002428 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002429 goto erret;
2430 }
2431 /* Can't change an existing function if it is locked */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002432 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002433 goto erret;
2434
2435 /* Give the function a sequential number. Can only be used with a
2436 * Funcref! */
2437 vim_free(name);
2438 sprintf(numbuf, "%d", ++func_nr);
2439 name = vim_strsave((char_u *)numbuf);
2440 if (name == NULL)
2441 goto erret;
2442 }
2443
2444 if (fp == NULL)
2445 {
2446 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2447 {
2448 int slen, plen;
2449 char_u *scriptname;
2450
2451 /* Check that the autoload name matches the script name. */
2452 j = FAIL;
2453 if (sourcing_name != NULL)
2454 {
2455 scriptname = autoload_name(name);
2456 if (scriptname != NULL)
2457 {
2458 p = vim_strchr(scriptname, '/');
2459 plen = (int)STRLEN(p);
2460 slen = (int)STRLEN(sourcing_name);
2461 if (slen > plen && fnamecmp(p,
2462 sourcing_name + slen - plen) == 0)
2463 j = OK;
2464 vim_free(scriptname);
2465 }
2466 }
2467 if (j == FAIL)
2468 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002469 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470 goto erret;
2471 }
2472 }
2473
Bram Moolenaar58016442016-07-31 18:30:22 +02002474 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 if (fp == NULL)
2476 goto erret;
2477
2478 if (fudi.fd_dict != NULL)
2479 {
2480 if (fudi.fd_di == NULL)
2481 {
2482 /* add new dict entry */
2483 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2484 if (fudi.fd_di == NULL)
2485 {
2486 vim_free(fp);
2487 goto erret;
2488 }
2489 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2490 {
2491 vim_free(fudi.fd_di);
2492 vim_free(fp);
2493 goto erret;
2494 }
2495 }
2496 else
2497 /* overwrite existing dict entry */
2498 clear_tv(&fudi.fd_di->di_tv);
2499 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002501
2502 /* behave like "dict" was used */
2503 flags |= FC_DICT;
2504 }
2505
2506 /* insert the new function in the function list */
2507 STRCPY(fp->uf_name, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002508 if (overwrite)
2509 {
2510 hi = hash_find(&func_hashtab, name);
2511 hi->hi_key = UF2HIKEY(fp);
2512 }
2513 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514 {
2515 vim_free(fp);
2516 goto erret;
2517 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002518 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519 }
2520 fp->uf_args = newargs;
2521 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002522 if ((flags & FC_CLOSURE) != 0)
2523 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002524 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002525 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002526 }
2527 else
2528 fp->uf_scoped = NULL;
2529
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002530#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002531 if (prof_def_func())
2532 func_do_profile(fp);
2533#endif
2534 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02002535 if (sandbox)
2536 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002537 fp->uf_flags = flags;
2538 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002539 fp->uf_script_ctx = current_sctx;
2540 fp->uf_script_ctx.sc_lnum += sourcing_lnum - newlines.ga_len - 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002541 goto ret_free;
2542
2543erret:
2544 ga_clear_strings(&newargs);
2545errret_2:
2546 ga_clear_strings(&newlines);
2547ret_free:
2548 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002549 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002550 vim_free(fudi.fd_newkey);
2551 vim_free(name);
2552 did_emsg |= saved_did_emsg;
2553 need_wait_return |= saved_wait_return;
2554}
2555
2556/*
2557 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2558 * Return 2 if "p" starts with "s:".
2559 * Return 0 otherwise.
2560 */
2561 int
2562eval_fname_script(char_u *p)
2563{
2564 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2565 * the standard library function. */
2566 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2567 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2568 return 5;
2569 if (p[0] == 's' && p[1] == ':')
2570 return 2;
2571 return 0;
2572}
2573
2574 int
2575translated_function_exists(char_u *name)
2576{
2577 if (builtin_function(name, -1))
2578 return find_internal_func(name) >= 0;
2579 return find_func(name) != NULL;
2580}
2581
2582/*
2583 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002584 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002585 */
2586 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002587function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002588{
2589 char_u *nm = name;
2590 char_u *p;
2591 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002592 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002593
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002594 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2595 if (no_deref)
2596 flag |= TFN_NO_DEREF;
2597 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002598 nm = skipwhite(nm);
2599
2600 /* Only accept "funcname", "funcname ", "funcname (..." and
2601 * "funcname(...", not "funcname!...". */
2602 if (p != NULL && (*nm == NUL || *nm == '('))
2603 n = translated_function_exists(p);
2604 vim_free(p);
2605 return n;
2606}
2607
Bram Moolenaar113e1072019-01-20 15:30:40 +01002608#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002609 char_u *
2610get_expanded_name(char_u *name, int check)
2611{
2612 char_u *nm = name;
2613 char_u *p;
2614
2615 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2616
2617 if (p != NULL && *nm == NUL)
2618 if (!check || translated_function_exists(p))
2619 return p;
2620
2621 vim_free(p);
2622 return NULL;
2623}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002624#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002625
2626#if defined(FEAT_PROFILE) || defined(PROTO)
2627/*
2628 * Start profiling function "fp".
2629 */
2630 static void
2631func_do_profile(ufunc_T *fp)
2632{
2633 int len = fp->uf_lines.ga_len;
2634
Bram Moolenaarad648092018-06-30 18:28:03 +02002635 if (!fp->uf_prof_initialized)
2636 {
2637 if (len == 0)
2638 len = 1; /* avoid getting error for allocating zero bytes */
2639 fp->uf_tm_count = 0;
2640 profile_zero(&fp->uf_tm_self);
2641 profile_zero(&fp->uf_tm_total);
2642 if (fp->uf_tml_count == NULL)
2643 fp->uf_tml_count = (int *)alloc_clear(
2644 (unsigned)(sizeof(int) * len));
2645 if (fp->uf_tml_total == NULL)
2646 fp->uf_tml_total = (proftime_T *)alloc_clear(
2647 (unsigned)(sizeof(proftime_T) * len));
2648 if (fp->uf_tml_self == NULL)
2649 fp->uf_tml_self = (proftime_T *)alloc_clear(
2650 (unsigned)(sizeof(proftime_T) * len));
2651 fp->uf_tml_idx = -1;
2652 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
2653 || fp->uf_tml_self == NULL)
2654 return; /* out of memory */
2655 fp->uf_prof_initialized = TRUE;
2656 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002657
2658 fp->uf_profiling = TRUE;
2659}
2660
2661/*
2662 * Dump the profiling results for all functions in file "fd".
2663 */
2664 void
2665func_dump_profile(FILE *fd)
2666{
2667 hashitem_T *hi;
2668 int todo;
2669 ufunc_T *fp;
2670 int i;
2671 ufunc_T **sorttab;
2672 int st_len = 0;
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +02002673 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002674
2675 todo = (int)func_hashtab.ht_used;
2676 if (todo == 0)
2677 return; /* nothing to dump */
2678
2679 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
2680
2681 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2682 {
2683 if (!HASHITEM_EMPTY(hi))
2684 {
2685 --todo;
2686 fp = HI2UF(hi);
Bram Moolenaarad648092018-06-30 18:28:03 +02002687 if (fp->uf_prof_initialized)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002688 {
2689 if (sorttab != NULL)
2690 sorttab[st_len++] = fp;
2691
2692 if (fp->uf_name[0] == K_SPECIAL)
2693 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
2694 else
2695 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +02002696 p = home_replace_save(NULL,
2697 get_scriptname(fp->uf_script_ctx.sc_sid));
2698 if (p != NULL)
2699 {
2700 fprintf(fd, " Defined: %s line %ld\n",
2701 p, (long)fp->uf_script_ctx.sc_lnum);
2702 vim_free(p);
2703 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002704 if (fp->uf_tm_count == 1)
2705 fprintf(fd, "Called 1 time\n");
2706 else
2707 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
2708 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
2709 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
2710 fprintf(fd, "\n");
2711 fprintf(fd, "count total (s) self (s)\n");
2712
2713 for (i = 0; i < fp->uf_lines.ga_len; ++i)
2714 {
2715 if (FUNCLINE(fp, i) == NULL)
2716 continue;
2717 prof_func_line(fd, fp->uf_tml_count[i],
2718 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
2719 fprintf(fd, "%s\n", FUNCLINE(fp, i));
2720 }
2721 fprintf(fd, "\n");
2722 }
2723 }
2724 }
2725
2726 if (sorttab != NULL && st_len > 0)
2727 {
2728 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
2729 prof_total_cmp);
2730 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
2731 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
2732 prof_self_cmp);
2733 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
2734 }
2735
2736 vim_free(sorttab);
2737}
2738
2739 static void
2740prof_sort_list(
2741 FILE *fd,
2742 ufunc_T **sorttab,
2743 int st_len,
2744 char *title,
2745 int prefer_self) /* when equal print only self time */
2746{
2747 int i;
2748 ufunc_T *fp;
2749
2750 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
2751 fprintf(fd, "count total (s) self (s) function\n");
2752 for (i = 0; i < 20 && i < st_len; ++i)
2753 {
2754 fp = sorttab[i];
2755 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
2756 prefer_self);
2757 if (fp->uf_name[0] == K_SPECIAL)
2758 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
2759 else
2760 fprintf(fd, " %s()\n", fp->uf_name);
2761 }
2762 fprintf(fd, "\n");
2763}
2764
2765/*
2766 * Print the count and times for one function or function line.
2767 */
2768 static void
2769prof_func_line(
2770 FILE *fd,
2771 int count,
2772 proftime_T *total,
2773 proftime_T *self,
2774 int prefer_self) /* when equal print only self time */
2775{
2776 if (count > 0)
2777 {
2778 fprintf(fd, "%5d ", count);
2779 if (prefer_self && profile_equal(total, self))
2780 fprintf(fd, " ");
2781 else
2782 fprintf(fd, "%s ", profile_msg(total));
2783 if (!prefer_self && profile_equal(total, self))
2784 fprintf(fd, " ");
2785 else
2786 fprintf(fd, "%s ", profile_msg(self));
2787 }
2788 else
2789 fprintf(fd, " ");
2790}
2791
2792/*
2793 * Compare function for total time sorting.
2794 */
2795 static int
2796#ifdef __BORLANDC__
2797_RTLENTRYF
2798#endif
2799prof_total_cmp(const void *s1, const void *s2)
2800{
2801 ufunc_T *p1, *p2;
2802
2803 p1 = *(ufunc_T **)s1;
2804 p2 = *(ufunc_T **)s2;
2805 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
2806}
2807
2808/*
2809 * Compare function for self time sorting.
2810 */
2811 static int
2812#ifdef __BORLANDC__
2813_RTLENTRYF
2814#endif
2815prof_self_cmp(const void *s1, const void *s2)
2816{
2817 ufunc_T *p1, *p2;
2818
2819 p1 = *(ufunc_T **)s1;
2820 p2 = *(ufunc_T **)s2;
2821 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
2822}
2823
2824/*
2825 * Prepare profiling for entering a child or something else that is not
2826 * counted for the script/function itself.
2827 * Should always be called in pair with prof_child_exit().
2828 */
2829 void
2830prof_child_enter(
2831 proftime_T *tm) /* place to store waittime */
2832{
2833 funccall_T *fc = current_funccal;
2834
2835 if (fc != NULL && fc->func->uf_profiling)
2836 profile_start(&fc->prof_child);
2837 script_prof_save(tm);
2838}
2839
2840/*
2841 * Take care of time spent in a child.
2842 * Should always be called after prof_child_enter().
2843 */
2844 void
2845prof_child_exit(
2846 proftime_T *tm) /* where waittime was stored */
2847{
2848 funccall_T *fc = current_funccal;
2849
2850 if (fc != NULL && fc->func->uf_profiling)
2851 {
2852 profile_end(&fc->prof_child);
2853 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
2854 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
2855 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
2856 }
2857 script_prof_restore(tm);
2858}
2859
2860#endif /* FEAT_PROFILE */
2861
2862#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2863
2864/*
2865 * Function given to ExpandGeneric() to obtain the list of user defined
2866 * function names.
2867 */
2868 char_u *
2869get_user_func_name(expand_T *xp, int idx)
2870{
2871 static long_u done;
2872 static hashitem_T *hi;
2873 ufunc_T *fp;
2874
2875 if (idx == 0)
2876 {
2877 done = 0;
2878 hi = func_hashtab.ht_array;
2879 }
2880 if (done < func_hashtab.ht_used)
2881 {
2882 if (done++ > 0)
2883 ++hi;
2884 while (HASHITEM_EMPTY(hi))
2885 ++hi;
2886 fp = HI2UF(hi);
2887
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002888 if ((fp->uf_flags & FC_DICT)
2889 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2890 return (char_u *)""; /* don't show dict and lambda functions */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891
2892 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
2893 return fp->uf_name; /* prevents overflow */
2894
2895 cat_func_name(IObuff, fp);
2896 if (xp->xp_context != EXPAND_USER_FUNC)
2897 {
2898 STRCAT(IObuff, "(");
2899 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2900 STRCAT(IObuff, ")");
2901 }
2902 return IObuff;
2903 }
2904 return NULL;
2905}
2906
2907#endif /* FEAT_CMDL_COMPL */
2908
2909/*
2910 * ":delfunction {name}"
2911 */
2912 void
2913ex_delfunction(exarg_T *eap)
2914{
2915 ufunc_T *fp = NULL;
2916 char_u *p;
2917 char_u *name;
2918 funcdict_T fudi;
2919
2920 p = eap->arg;
2921 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2922 vim_free(fudi.fd_newkey);
2923 if (name == NULL)
2924 {
2925 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002926 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002927 return;
2928 }
2929 if (!ends_excmd(*skipwhite(p)))
2930 {
2931 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002932 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 return;
2934 }
2935 eap->nextcmd = check_nextcmd(p);
2936 if (eap->nextcmd != NULL)
2937 *p = NUL;
2938
2939 if (!eap->skip)
2940 fp = find_func(name);
2941 vim_free(name);
2942
2943 if (!eap->skip)
2944 {
2945 if (fp == NULL)
2946 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02002947 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002948 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002949 return;
2950 }
2951 if (fp->uf_calls > 0)
2952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002953 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002954 return;
2955 }
2956
2957 if (fudi.fd_dict != NULL)
2958 {
2959 /* Delete the dict item that refers to the function, it will
2960 * invoke func_unref() and possibly delete the function. */
2961 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2962 }
2963 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002964 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002965 /* A normal function (not a numbered function or lambda) has a
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002966 * refcount of 1 for the entry in the hashtable. When deleting
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002967 * it and the refcount is more than one, it should be kept.
Bram Moolenaarba209902016-08-24 22:06:38 +02002968 * A numbered function and lambda should be kept if the refcount is
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002969 * one or more. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002970 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002971 {
2972 /* Function is still referenced somewhere. Don't free it but
2973 * do remove it from the hashtable. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002974 if (func_remove(fp))
2975 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002976 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002977 }
2978 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002979 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002980 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002981 }
2982}
2983
2984/*
2985 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002986 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987 */
2988 void
2989func_unref(char_u *name)
2990{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002991 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002992
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002993 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002995 fp = find_func(name);
2996 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002997 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002999 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003000#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01003001 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003002 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003003 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003004 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003005 /* Only delete it when it's not being used. Otherwise it's done
3006 * when "uf_calls" becomes zero. */
3007 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003008 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02003009 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003010}
3011
3012/*
3013 * Unreference a Function: decrement the reference count and free it when it
3014 * becomes zero.
3015 */
3016 void
3017func_ptr_unref(ufunc_T *fp)
3018{
Bram Moolenaar97baee82016-07-26 20:46:08 +02003019 if (fp != NULL && --fp->uf_refcount <= 0)
3020 {
3021 /* Only delete it when it's not being used. Otherwise it's done
3022 * when "uf_calls" becomes zero. */
3023 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01003024 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003025 }
3026}
3027
3028/*
3029 * Count a reference to a Function.
3030 */
3031 void
3032func_ref(char_u *name)
3033{
3034 ufunc_T *fp;
3035
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003036 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003038 fp = find_func(name);
3039 if (fp != NULL)
3040 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003041 else if (isdigit(*name))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003042 /* Only give an error for a numbered function.
3043 * Fail silently, when named or lambda function isn't found. */
Bram Moolenaar95f09602016-11-10 20:01:45 +01003044 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003045}
3046
3047/*
3048 * Count a reference to a Function.
3049 */
3050 void
3051func_ptr_ref(ufunc_T *fp)
3052{
3053 if (fp != NULL)
3054 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003055}
3056
3057/*
3058 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3059 * referenced from anywhere that is in use.
3060 */
3061 static int
3062can_free_funccal(funccall_T *fc, int copyID)
3063{
3064 return (fc->l_varlist.lv_copyID != copyID
3065 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003066 && fc->l_avars.dv_copyID != copyID
3067 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003068}
3069
3070/*
3071 * ":return [expr]"
3072 */
3073 void
3074ex_return(exarg_T *eap)
3075{
3076 char_u *arg = eap->arg;
3077 typval_T rettv;
3078 int returning = FALSE;
3079
3080 if (current_funccal == NULL)
3081 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003082 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003083 return;
3084 }
3085
3086 if (eap->skip)
3087 ++emsg_skip;
3088
3089 eap->nextcmd = NULL;
3090 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3091 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3092 {
3093 if (!eap->skip)
3094 returning = do_return(eap, FALSE, TRUE, &rettv);
3095 else
3096 clear_tv(&rettv);
3097 }
3098 /* It's safer to return also on error. */
3099 else if (!eap->skip)
3100 {
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003101 /* In return statement, cause_abort should be force_abort. */
3102 update_force_abort();
3103
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003104 /*
3105 * Return unless the expression evaluation has been cancelled due to an
3106 * aborting error, an interrupt, or an exception.
3107 */
3108 if (!aborting())
3109 returning = do_return(eap, FALSE, TRUE, NULL);
3110 }
3111
3112 /* When skipping or the return gets pending, advance to the next command
3113 * in this line (!returning). Otherwise, ignore the rest of the line.
3114 * Following lines will be ignored by get_func_line(). */
3115 if (returning)
3116 eap->nextcmd = NULL;
3117 else if (eap->nextcmd == NULL) /* no argument */
3118 eap->nextcmd = check_nextcmd(arg);
3119
3120 if (eap->skip)
3121 --emsg_skip;
3122}
3123
3124/*
3125 * ":1,25call func(arg1, arg2)" function call.
3126 */
3127 void
3128ex_call(exarg_T *eap)
3129{
3130 char_u *arg = eap->arg;
3131 char_u *startarg;
3132 char_u *name;
3133 char_u *tofree;
3134 int len;
3135 typval_T rettv;
3136 linenr_T lnum;
3137 int doesrange;
3138 int failed = FALSE;
3139 funcdict_T fudi;
3140 partial_T *partial = NULL;
3141
3142 if (eap->skip)
3143 {
3144 /* trans_function_name() doesn't work well when skipping, use eval0()
3145 * instead to skip to any following command, e.g. for:
3146 * :if 0 | call dict.foo().bar() | endif */
3147 ++emsg_skip;
3148 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3149 clear_tv(&rettv);
3150 --emsg_skip;
3151 return;
3152 }
3153
3154 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3155 if (fudi.fd_newkey != NULL)
3156 {
3157 /* Still need to give an error message for missing key. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003158 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 vim_free(fudi.fd_newkey);
3160 }
3161 if (tofree == NULL)
3162 return;
3163
3164 /* Increase refcount on dictionary, it could get deleted when evaluating
3165 * the arguments. */
3166 if (fudi.fd_dict != NULL)
3167 ++fudi.fd_dict->dv_refcount;
3168
3169 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3170 * contents. For VAR_PARTIAL get its partial, unless we already have one
3171 * from trans_function_name(). */
3172 len = (int)STRLEN(tofree);
3173 name = deref_func_name(tofree, &len,
3174 partial != NULL ? NULL : &partial, FALSE);
3175
3176 /* Skip white space to allow ":call func ()". Not good, but required for
3177 * backward compatibility. */
3178 startarg = skipwhite(arg);
3179 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
3180
3181 if (*startarg != '(')
3182 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003183 semsg(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003184 goto end;
3185 }
3186
3187 /*
3188 * When skipping, evaluate the function once, to find the end of the
3189 * arguments.
3190 * When the function takes a range, this is discovered after the first
3191 * call, and the loop is broken.
3192 */
3193 if (eap->skip)
3194 {
3195 ++emsg_skip;
3196 lnum = eap->line2; /* do it once, also with an invalid range */
3197 }
3198 else
3199 lnum = eap->line1;
3200 for ( ; lnum <= eap->line2; ++lnum)
3201 {
3202 if (!eap->skip && eap->addr_count > 0)
3203 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003204 if (lnum > curbuf->b_ml.ml_line_count)
3205 {
3206 // If the function deleted lines or switched to another buffer
3207 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003208 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003209 break;
3210 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003211 curwin->w_cursor.lnum = lnum;
3212 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003213 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003214 }
3215 arg = startarg;
3216 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
3217 eap->line1, eap->line2, &doesrange,
3218 !eap->skip, partial, fudi.fd_dict) == FAIL)
3219 {
3220 failed = TRUE;
3221 break;
3222 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003223 if (has_watchexpr())
3224 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003225
3226 /* Handle a function returning a Funcref, Dictionary or List. */
3227 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3228 {
3229 failed = TRUE;
3230 break;
3231 }
3232
3233 clear_tv(&rettv);
3234 if (doesrange || eap->skip)
3235 break;
3236
3237 /* Stop when immediately aborting on error, or when an interrupt
3238 * occurred or an exception was thrown but not caught.
3239 * get_func_tv() returned OK, so that the check for trailing
3240 * characters below is executed. */
3241 if (aborting())
3242 break;
3243 }
3244 if (eap->skip)
3245 --emsg_skip;
3246
3247 if (!failed)
3248 {
3249 /* Check for trailing illegal characters and a following command. */
3250 if (!ends_excmd(*arg))
3251 {
3252 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003253 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 }
3255 else
3256 eap->nextcmd = check_nextcmd(arg);
3257 }
3258
3259end:
3260 dict_unref(fudi.fd_dict);
3261 vim_free(tofree);
3262}
3263
3264/*
3265 * Return from a function. Possibly makes the return pending. Also called
3266 * for a pending return at the ":endtry" or after returning from an extra
3267 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3268 * when called due to a ":return" command. "rettv" may point to a typval_T
3269 * with the return rettv. Returns TRUE when the return can be carried out,
3270 * FALSE when the return gets pending.
3271 */
3272 int
3273do_return(
3274 exarg_T *eap,
3275 int reanimate,
3276 int is_cmd,
3277 void *rettv)
3278{
3279 int idx;
3280 struct condstack *cstack = eap->cstack;
3281
3282 if (reanimate)
3283 /* Undo the return. */
3284 current_funccal->returned = FALSE;
3285
3286 /*
3287 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3288 * not in its finally clause (which then is to be executed next) is found.
3289 * In this case, make the ":return" pending for execution at the ":endtry".
3290 * Otherwise, return normally.
3291 */
3292 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3293 if (idx >= 0)
3294 {
3295 cstack->cs_pending[idx] = CSTP_RETURN;
3296
3297 if (!is_cmd && !reanimate)
3298 /* A pending return again gets pending. "rettv" points to an
3299 * allocated variable with the rettv of the original ":return"'s
3300 * argument if present or is NULL else. */
3301 cstack->cs_rettv[idx] = rettv;
3302 else
3303 {
3304 /* When undoing a return in order to make it pending, get the stored
3305 * return rettv. */
3306 if (reanimate)
3307 rettv = current_funccal->rettv;
3308
3309 if (rettv != NULL)
3310 {
3311 /* Store the value of the pending return. */
3312 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3313 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3314 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003315 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003316 }
3317 else
3318 cstack->cs_rettv[idx] = NULL;
3319
3320 if (reanimate)
3321 {
3322 /* The pending return value could be overwritten by a ":return"
3323 * without argument in a finally clause; reset the default
3324 * return value. */
3325 current_funccal->rettv->v_type = VAR_NUMBER;
3326 current_funccal->rettv->vval.v_number = 0;
3327 }
3328 }
3329 report_make_pending(CSTP_RETURN, rettv);
3330 }
3331 else
3332 {
3333 current_funccal->returned = TRUE;
3334
3335 /* If the return is carried out now, store the return value. For
3336 * a return immediately after reanimation, the value is already
3337 * there. */
3338 if (!reanimate && rettv != NULL)
3339 {
3340 clear_tv(current_funccal->rettv);
3341 *current_funccal->rettv = *(typval_T *)rettv;
3342 if (!is_cmd)
3343 vim_free(rettv);
3344 }
3345 }
3346
3347 return idx < 0;
3348}
3349
3350/*
3351 * Free the variable with a pending return value.
3352 */
3353 void
3354discard_pending_return(void *rettv)
3355{
3356 free_tv((typval_T *)rettv);
3357}
3358
3359/*
3360 * Generate a return command for producing the value of "rettv". The result
3361 * is an allocated string. Used by report_pending() for verbose messages.
3362 */
3363 char_u *
3364get_return_cmd(void *rettv)
3365{
3366 char_u *s = NULL;
3367 char_u *tofree = NULL;
3368 char_u numbuf[NUMBUFLEN];
3369
3370 if (rettv != NULL)
3371 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3372 if (s == NULL)
3373 s = (char_u *)"";
3374
3375 STRCPY(IObuff, ":return ");
3376 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3377 if (STRLEN(s) + 8 >= IOSIZE)
3378 STRCPY(IObuff + IOSIZE - 4, "...");
3379 vim_free(tofree);
3380 return vim_strsave(IObuff);
3381}
3382
3383/*
3384 * Get next function line.
3385 * Called by do_cmdline() to get the next line.
3386 * Returns allocated string, or NULL for end of function.
3387 */
3388 char_u *
3389get_func_line(
3390 int c UNUSED,
3391 void *cookie,
3392 int indent UNUSED)
3393{
3394 funccall_T *fcp = (funccall_T *)cookie;
3395 ufunc_T *fp = fcp->func;
3396 char_u *retval;
3397 garray_T *gap; /* growarray with function lines */
3398
3399 /* If breakpoints have been added/deleted need to check for it. */
3400 if (fcp->dbg_tick != debug_tick)
3401 {
3402 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3403 sourcing_lnum);
3404 fcp->dbg_tick = debug_tick;
3405 }
3406#ifdef FEAT_PROFILE
3407 if (do_profiling == PROF_YES)
3408 func_line_end(cookie);
3409#endif
3410
3411 gap = &fp->uf_lines;
3412 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3413 || fcp->returned)
3414 retval = NULL;
3415 else
3416 {
3417 /* Skip NULL lines (continuation lines). */
3418 while (fcp->linenr < gap->ga_len
3419 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3420 ++fcp->linenr;
3421 if (fcp->linenr >= gap->ga_len)
3422 retval = NULL;
3423 else
3424 {
3425 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
3426 sourcing_lnum = fcp->linenr;
3427#ifdef FEAT_PROFILE
3428 if (do_profiling == PROF_YES)
3429 func_line_start(cookie);
3430#endif
3431 }
3432 }
3433
3434 /* Did we encounter a breakpoint? */
3435 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
3436 {
3437 dbg_breakpoint(fp->uf_name, sourcing_lnum);
3438 /* Find next breakpoint. */
3439 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3440 sourcing_lnum);
3441 fcp->dbg_tick = debug_tick;
3442 }
3443
3444 return retval;
3445}
3446
3447#if defined(FEAT_PROFILE) || defined(PROTO)
3448/*
3449 * Called when starting to read a function line.
3450 * "sourcing_lnum" must be correct!
3451 * When skipping lines it may not actually be executed, but we won't find out
3452 * until later and we need to store the time now.
3453 */
3454 void
3455func_line_start(void *cookie)
3456{
3457 funccall_T *fcp = (funccall_T *)cookie;
3458 ufunc_T *fp = fcp->func;
3459
3460 if (fp->uf_profiling && sourcing_lnum >= 1
3461 && sourcing_lnum <= fp->uf_lines.ga_len)
3462 {
3463 fp->uf_tml_idx = sourcing_lnum - 1;
3464 /* Skip continuation lines. */
3465 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
3466 --fp->uf_tml_idx;
3467 fp->uf_tml_execed = FALSE;
3468 profile_start(&fp->uf_tml_start);
3469 profile_zero(&fp->uf_tml_children);
3470 profile_get_wait(&fp->uf_tml_wait);
3471 }
3472}
3473
3474/*
3475 * Called when actually executing a function line.
3476 */
3477 void
3478func_line_exec(void *cookie)
3479{
3480 funccall_T *fcp = (funccall_T *)cookie;
3481 ufunc_T *fp = fcp->func;
3482
3483 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
3484 fp->uf_tml_execed = TRUE;
3485}
3486
3487/*
3488 * Called when done with a function line.
3489 */
3490 void
3491func_line_end(void *cookie)
3492{
3493 funccall_T *fcp = (funccall_T *)cookie;
3494 ufunc_T *fp = fcp->func;
3495
3496 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
3497 {
3498 if (fp->uf_tml_execed)
3499 {
3500 ++fp->uf_tml_count[fp->uf_tml_idx];
3501 profile_end(&fp->uf_tml_start);
3502 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
3503 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
3504 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
3505 &fp->uf_tml_children);
3506 }
3507 fp->uf_tml_idx = -1;
3508 }
3509}
3510#endif
3511
3512/*
3513 * Return TRUE if the currently active function should be ended, because a
3514 * return was encountered or an error occurred. Used inside a ":while".
3515 */
3516 int
3517func_has_ended(void *cookie)
3518{
3519 funccall_T *fcp = (funccall_T *)cookie;
3520
3521 /* Ignore the "abort" flag if the abortion behavior has been changed due to
3522 * an error inside a try conditional. */
3523 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3524 || fcp->returned);
3525}
3526
3527/*
3528 * return TRUE if cookie indicates a function which "abort"s on errors.
3529 */
3530 int
3531func_has_abort(
3532 void *cookie)
3533{
3534 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3535}
3536
3537
3538/*
3539 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3540 * Don't do this when "Func" is already a partial that was bound
3541 * explicitly (pt_auto is FALSE).
3542 * Changes "rettv" in-place.
3543 * Returns the updated "selfdict_in".
3544 */
3545 dict_T *
3546make_partial(dict_T *selfdict_in, typval_T *rettv)
3547{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003548 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003549 char_u *tofree = NULL;
3550 ufunc_T *fp;
3551 char_u fname_buf[FLEN_FIXED + 1];
3552 int error;
3553 dict_T *selfdict = selfdict_in;
3554
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003555 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3556 fp = rettv->vval.v_partial->pt_func;
3557 else
3558 {
3559 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3560 : rettv->vval.v_partial->pt_name;
3561 /* Translate "s:func" to the stored function name. */
3562 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3563 fp = find_func(fname);
3564 vim_free(tofree);
3565 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566
3567 if (fp != NULL && (fp->uf_flags & FC_DICT))
3568 {
3569 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3570
3571 if (pt != NULL)
3572 {
3573 pt->pt_refcount = 1;
3574 pt->pt_dict = selfdict;
3575 pt->pt_auto = TRUE;
3576 selfdict = NULL;
3577 if (rettv->v_type == VAR_FUNC)
3578 {
3579 /* Just a function: Take over the function name and use
3580 * selfdict. */
3581 pt->pt_name = rettv->vval.v_string;
3582 }
3583 else
3584 {
3585 partial_T *ret_pt = rettv->vval.v_partial;
3586 int i;
3587
3588 /* Partial: copy the function name, use selfdict and copy
3589 * args. Can't take over name or args, the partial might
3590 * be referenced elsewhere. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003591 if (ret_pt->pt_name != NULL)
3592 {
3593 pt->pt_name = vim_strsave(ret_pt->pt_name);
3594 func_ref(pt->pt_name);
3595 }
3596 else
3597 {
3598 pt->pt_func = ret_pt->pt_func;
3599 func_ptr_ref(pt->pt_func);
3600 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003601 if (ret_pt->pt_argc > 0)
3602 {
3603 pt->pt_argv = (typval_T *)alloc(
3604 sizeof(typval_T) * ret_pt->pt_argc);
3605 if (pt->pt_argv == NULL)
3606 /* out of memory: drop the arguments */
3607 pt->pt_argc = 0;
3608 else
3609 {
3610 pt->pt_argc = ret_pt->pt_argc;
3611 for (i = 0; i < pt->pt_argc; i++)
3612 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3613 }
3614 }
3615 partial_unref(ret_pt);
3616 }
3617 rettv->v_type = VAR_PARTIAL;
3618 rettv->vval.v_partial = pt;
3619 }
3620 }
3621 return selfdict;
3622}
3623
3624/*
3625 * Return the name of the executed function.
3626 */
3627 char_u *
3628func_name(void *cookie)
3629{
3630 return ((funccall_T *)cookie)->func->uf_name;
3631}
3632
3633/*
3634 * Return the address holding the next breakpoint line for a funccall cookie.
3635 */
3636 linenr_T *
3637func_breakpoint(void *cookie)
3638{
3639 return &((funccall_T *)cookie)->breakpoint;
3640}
3641
3642/*
3643 * Return the address holding the debug tick for a funccall cookie.
3644 */
3645 int *
3646func_dbg_tick(void *cookie)
3647{
3648 return &((funccall_T *)cookie)->dbg_tick;
3649}
3650
3651/*
3652 * Return the nesting level for a funccall cookie.
3653 */
3654 int
3655func_level(void *cookie)
3656{
3657 return ((funccall_T *)cookie)->level;
3658}
3659
3660/*
3661 * Return TRUE when a function was ended by a ":return" command.
3662 */
3663 int
3664current_func_returned(void)
3665{
3666 return current_funccal->returned;
3667}
3668
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003669 int
3670free_unref_funccal(int copyID, int testing)
3671{
3672 int did_free = FALSE;
3673 int did_free_funccal = FALSE;
3674 funccall_T *fc, **pfc;
3675
3676 for (pfc = &previous_funccal; *pfc != NULL; )
3677 {
3678 if (can_free_funccal(*pfc, copyID))
3679 {
3680 fc = *pfc;
3681 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003682 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003683 did_free = TRUE;
3684 did_free_funccal = TRUE;
3685 }
3686 else
3687 pfc = &(*pfc)->caller;
3688 }
3689 if (did_free_funccal)
3690 /* When a funccal was freed some more items might be garbage
3691 * collected, so run again. */
3692 (void)garbage_collect(testing);
3693
3694 return did_free;
3695}
3696
3697/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003698 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003699 */
3700 static funccall_T *
3701get_funccal(void)
3702{
3703 int i;
3704 funccall_T *funccal;
3705 funccall_T *temp_funccal;
3706
3707 funccal = current_funccal;
3708 if (debug_backtrace_level > 0)
3709 {
3710 for (i = 0; i < debug_backtrace_level; i++)
3711 {
3712 temp_funccal = funccal->caller;
3713 if (temp_funccal)
3714 funccal = temp_funccal;
3715 else
3716 /* backtrace level overflow. reset to max */
3717 debug_backtrace_level = i;
3718 }
3719 }
3720 return funccal;
3721}
3722
3723/*
3724 * Return the hashtable used for local variables in the current funccal.
3725 * Return NULL if there is no current funccal.
3726 */
3727 hashtab_T *
3728get_funccal_local_ht()
3729{
3730 if (current_funccal == NULL)
3731 return NULL;
3732 return &get_funccal()->l_vars.dv_hashtab;
3733}
3734
3735/*
3736 * Return the l: scope variable.
3737 * Return NULL if there is no current funccal.
3738 */
3739 dictitem_T *
3740get_funccal_local_var()
3741{
3742 if (current_funccal == NULL)
3743 return NULL;
3744 return &get_funccal()->l_vars_var;
3745}
3746
3747/*
3748 * Return the hashtable used for argument in the current funccal.
3749 * Return NULL if there is no current funccal.
3750 */
3751 hashtab_T *
3752get_funccal_args_ht()
3753{
3754 if (current_funccal == NULL)
3755 return NULL;
3756 return &get_funccal()->l_avars.dv_hashtab;
3757}
3758
3759/*
3760 * Return the a: scope variable.
3761 * Return NULL if there is no current funccal.
3762 */
3763 dictitem_T *
3764get_funccal_args_var()
3765{
3766 if (current_funccal == NULL)
3767 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01003768 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003769}
3770
3771/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003772 * List function variables, if there is a function.
3773 */
3774 void
3775list_func_vars(int *first)
3776{
3777 if (current_funccal != NULL)
3778 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01003779 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003780}
3781
3782/*
3783 * If "ht" is the hashtable for local variables in the current funccal, return
3784 * the dict that contains it.
3785 * Otherwise return NULL.
3786 */
3787 dict_T *
3788get_current_funccal_dict(hashtab_T *ht)
3789{
3790 if (current_funccal != NULL
3791 && ht == &current_funccal->l_vars.dv_hashtab)
3792 return &current_funccal->l_vars;
3793 return NULL;
3794}
3795
3796/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003797 * Search hashitem in parent scope.
3798 */
3799 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003800find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003801{
3802 funccall_T *old_current_funccal = current_funccal;
3803 hashtab_T *ht;
3804 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003805 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003806
3807 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3808 return NULL;
3809
3810 /* Search in parent scope which is possible to reference from lambda */
3811 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003812 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003813 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003814 ht = find_var_ht(name, &varname);
3815 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003816 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003817 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003818 if (!HASHITEM_EMPTY(hi))
3819 {
3820 *pht = ht;
3821 break;
3822 }
3823 }
3824 if (current_funccal == current_funccal->func->uf_scoped)
3825 break;
3826 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003827 }
3828 current_funccal = old_current_funccal;
3829
3830 return hi;
3831}
3832
3833/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003834 * Search variable in parent scope.
3835 */
3836 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003837find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003838{
3839 dictitem_T *v = NULL;
3840 funccall_T *old_current_funccal = current_funccal;
3841 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003842 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003843
3844 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3845 return NULL;
3846
3847 /* Search in parent scope which is possible to reference from lambda */
3848 current_funccal = current_funccal->func->uf_scoped;
3849 while (current_funccal)
3850 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003851 ht = find_var_ht(name, &varname);
3852 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003853 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003854 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003855 if (v != NULL)
3856 break;
3857 }
3858 if (current_funccal == current_funccal->func->uf_scoped)
3859 break;
3860 current_funccal = current_funccal->func->uf_scoped;
3861 }
3862 current_funccal = old_current_funccal;
3863
3864 return v;
3865}
3866
3867/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003868 * Set "copyID + 1" in previous_funccal and callers.
3869 */
3870 int
3871set_ref_in_previous_funccal(int copyID)
3872{
3873 int abort = FALSE;
3874 funccall_T *fc;
3875
3876 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
3877 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003878 fc->fc_copyID = copyID + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003879 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
3880 NULL);
3881 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
3882 NULL);
3883 }
3884 return abort;
3885}
3886
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003887 static int
3888set_ref_in_funccal(funccall_T *fc, int copyID)
3889{
3890 int abort = FALSE;
3891
3892 if (fc->fc_copyID != copyID)
3893 {
3894 fc->fc_copyID = copyID;
3895 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
3896 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
3897 abort = abort || set_ref_in_func(NULL, fc->func, copyID);
3898 }
3899 return abort;
3900}
3901
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003902/*
3903 * Set "copyID" in all local vars and arguments in the call stack.
3904 */
3905 int
3906set_ref_in_call_stack(int copyID)
3907{
3908 int abort = FALSE;
3909 funccall_T *fc;
3910
3911 for (fc = current_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003912 abort = abort || set_ref_in_funccal(fc, copyID);
3913 return abort;
3914}
3915
3916/*
3917 * Set "copyID" in all functions available by name.
3918 */
3919 int
3920set_ref_in_functions(int copyID)
3921{
3922 int todo;
3923 hashitem_T *hi = NULL;
3924 int abort = FALSE;
3925 ufunc_T *fp;
3926
3927 todo = (int)func_hashtab.ht_used;
3928 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003929 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003930 if (!HASHITEM_EMPTY(hi))
3931 {
3932 --todo;
3933 fp = HI2UF(hi);
3934 if (!func_name_refcount(fp->uf_name))
3935 abort = abort || set_ref_in_func(NULL, fp, copyID);
3936 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003937 }
3938 return abort;
3939}
3940
3941/*
3942 * Set "copyID" in all function arguments.
3943 */
3944 int
3945set_ref_in_func_args(int copyID)
3946{
3947 int i;
3948 int abort = FALSE;
3949
3950 for (i = 0; i < funcargs.ga_len; ++i)
3951 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3952 copyID, NULL, NULL);
3953 return abort;
3954}
3955
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003956/*
3957 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003958 * Returns TRUE if setting references failed somehow.
3959 */
3960 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003961set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003962{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003963 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003964 funccall_T *fc;
3965 int error = ERROR_NONE;
3966 char_u fname_buf[FLEN_FIXED + 1];
3967 char_u *tofree = NULL;
3968 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003969 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003970
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003971 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003972 return FALSE;
3973
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003974 if (fp_in == NULL)
3975 {
3976 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3977 fp = find_func(fname);
3978 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003979 if (fp != NULL)
3980 {
3981 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003982 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003983 }
3984 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003985 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003986}
3987
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003988#endif /* FEAT_EVAL */