blob: a293dd68e3f42efbaa7435f6c56eea3a0c365c4e [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
42/* pointer to funccal for currently active function */
43funccall_T *current_funccal = NULL;
44
Bram Moolenaar6914c642017-04-01 21:21:30 +020045/* Pointer to list of previously used funccal, still around because some
Bram Moolenaara9b579f2016-07-17 18:29:19 +020046 * item in it is still being used. */
47funccall_T *previous_funccal = NULL;
48
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;
208 int varargs;
209 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200210 char_u *start = skipwhite(*arg + 1);
211 char_u *s, *e;
212 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200213 int *old_eval_lavars = eval_lavars_used;
214 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200215
216 ga_init(&newargs);
217 ga_init(&newlines);
218
219 /* First, check if this is a lambda expression. "->" must exist. */
220 ret = get_function_args(&start, '-', NULL, NULL, TRUE);
221 if (ret == FAIL || *start != '>')
222 return NOTDONE;
223
224 /* Parse the arguments again. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200225 if (evaluate)
226 pnewargs = &newargs;
227 else
228 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200229 *arg = skipwhite(*arg + 1);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200230 ret = get_function_args(arg, '-', pnewargs, &varargs, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200231 if (ret == FAIL || **arg != '>')
232 goto errret;
233
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +0200234 /* Set up a flag for checking local variables and arguments. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200235 if (evaluate)
236 eval_lavars_used = &eval_lavars;
237
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200238 /* Get the start and the end of the expression. */
239 *arg = skipwhite(*arg + 1);
240 s = *arg;
241 ret = skip_expr(arg);
242 if (ret == FAIL)
243 goto errret;
244 e = *arg;
245 *arg = skipwhite(*arg);
246 if (**arg != '}')
247 goto errret;
248 ++*arg;
249
250 if (evaluate)
251 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200252 int len, flags = 0;
253 char_u *p;
254 char_u name[20];
255 partial_T *pt;
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)
264 {
265 vim_free(fp);
266 goto errret;
267 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200268
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200269 ga_init2(&newlines, (int)sizeof(char_u *), 1);
270 if (ga_grow(&newlines, 1) == FAIL)
271 goto errret;
272
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200273 /* Add "return " before the expression. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200274 len = 7 + e - s + 1;
275 p = (char_u *)alloc(len);
276 if (p == NULL)
277 goto errret;
278 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
279 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200280 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200281
282 fp->uf_refcount = 1;
283 STRCPY(fp->uf_name, name);
284 hash_add(&func_hashtab, UF2HIKEY(fp));
285 fp->uf_args = newargs;
286 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200287 if (current_funccal != NULL && eval_lavars)
288 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200289 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200290 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200291 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200292 }
293 else
294 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200295
296#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297 if (prof_def_func())
298 func_do_profile(fp);
299#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200300 if (sandbox)
301 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200302 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200303 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200304 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200305 fp->uf_script_ctx = current_sctx;
306 fp->uf_script_ctx.sc_lnum += sourcing_lnum - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200307
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200308 pt->pt_func = fp;
309 pt->pt_refcount = 1;
310 rettv->vval.v_partial = pt;
311 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200312 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200314 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200315 return OK;
316
317errret:
318 ga_clear_strings(&newargs);
319 ga_clear_strings(&newlines);
320 vim_free(fp);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200321 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200322 return FAIL;
323}
324
325/*
326 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
327 * name it contains, otherwise return "name".
328 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
329 * "partialp".
330 */
331 char_u *
332deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
333{
334 dictitem_T *v;
335 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200336 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337
338 if (partialp != NULL)
339 *partialp = NULL;
340
341 cc = name[*lenp];
342 name[*lenp] = NUL;
343 v = find_var(name, NULL, no_autoload);
344 name[*lenp] = cc;
345 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
346 {
347 if (v->di_tv.vval.v_string == NULL)
348 {
349 *lenp = 0;
350 return (char_u *)""; /* just in case */
351 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200352 s = v->di_tv.vval.v_string;
353 *lenp = (int)STRLEN(s);
354 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200355 }
356
357 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
358 {
359 partial_T *pt = v->di_tv.vval.v_partial;
360
361 if (pt == NULL)
362 {
363 *lenp = 0;
364 return (char_u *)""; /* just in case */
365 }
366 if (partialp != NULL)
367 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200368 s = partial_name(pt);
369 *lenp = (int)STRLEN(s);
370 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200371 }
372
373 return name;
374}
375
376/*
377 * Give an error message with a function name. Handle <SNR> things.
378 * "ermsg" is to be passed without translation, use N_() instead of _().
379 */
380 static void
381emsg_funcname(char *ermsg, char_u *name)
382{
383 char_u *p;
384
385 if (*name == K_SPECIAL)
386 p = concat_str((char_u *)"<SNR>", name + 3);
387 else
388 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100389 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200390 if (p != name)
391 vim_free(p);
392}
393
394/*
395 * Allocate a variable for the result of a function.
396 * Return OK or FAIL.
397 */
398 int
399get_func_tv(
400 char_u *name, /* name of the function */
401 int len, /* length of "name" */
402 typval_T *rettv,
403 char_u **arg, /* argument, pointing to the '(' */
404 linenr_T firstline, /* first line of range */
405 linenr_T lastline, /* last line of range */
406 int *doesrange, /* return: function handled range */
407 int evaluate,
408 partial_T *partial, /* for extra arguments */
409 dict_T *selfdict) /* Dictionary for "self" */
410{
411 char_u *argp;
412 int ret = OK;
413 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
414 int argcount = 0; /* number of arguments found */
415
416 /*
417 * Get the arguments.
418 */
419 argp = *arg;
420 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
421 {
422 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
423 if (*argp == ')' || *argp == ',' || *argp == NUL)
424 break;
425 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
426 {
427 ret = FAIL;
428 break;
429 }
430 ++argcount;
431 if (*argp != ',')
432 break;
433 }
434 if (*argp == ')')
435 ++argp;
436 else
437 ret = FAIL;
438
439 if (ret == OK)
440 {
441 int i = 0;
442
443 if (get_vim_var_nr(VV_TESTING))
444 {
445 /* Prepare for calling test_garbagecollect_now(), need to know
446 * what variables are used on the call stack. */
447 if (funcargs.ga_itemsize == 0)
448 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
449 for (i = 0; i < argcount; ++i)
450 if (ga_grow(&funcargs, 1) == OK)
451 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
452 &argvars[i];
453 }
454
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200455 ret = call_func(name, len, rettv, argcount, argvars, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200456 firstline, lastline, doesrange, evaluate, partial, selfdict);
457
458 funcargs.ga_len -= i;
459 }
460 else if (!aborting())
461 {
462 if (argcount == MAX_FUNC_ARGS)
463 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
464 else
465 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
466 }
467
468 while (--argcount >= 0)
469 clear_tv(&argvars[argcount]);
470
471 *arg = skipwhite(argp);
472 return ret;
473}
474
475#define FLEN_FIXED 40
476
477/*
478 * Return TRUE if "p" starts with "<SID>" or "s:".
479 * Only works if eval_fname_script() returned non-zero for "p"!
480 */
481 static int
482eval_fname_sid(char_u *p)
483{
484 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
485}
486
487/*
488 * In a script change <SID>name() and s:name() to K_SNR 123_name().
489 * Change <SNR>123_name() to K_SNR 123_name().
490 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
491 * (slow).
492 */
493 static char_u *
494fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
495{
496 int llen;
497 char_u *fname;
498 int i;
499
500 llen = eval_fname_script(name);
501 if (llen > 0)
502 {
503 fname_buf[0] = K_SPECIAL;
504 fname_buf[1] = KS_EXTRA;
505 fname_buf[2] = (int)KE_SNR;
506 i = 3;
507 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
508 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200509 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200510 *error = ERROR_SCRIPT;
511 else
512 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200513 sprintf((char *)fname_buf + 3, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200514 i = (int)STRLEN(fname_buf);
515 }
516 }
517 if (i + STRLEN(name + llen) < FLEN_FIXED)
518 {
519 STRCPY(fname_buf + i, name + llen);
520 fname = fname_buf;
521 }
522 else
523 {
524 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
525 if (fname == NULL)
526 *error = ERROR_OTHER;
527 else
528 {
529 *tofree = fname;
530 mch_memmove(fname, fname_buf, (size_t)i);
531 STRCPY(fname + i, name + llen);
532 }
533 }
534 }
535 else
536 fname = name;
537 return fname;
538}
539
540/*
541 * Find a function by name, return pointer to it in ufuncs.
542 * Return NULL for unknown function.
543 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200544 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200545find_func(char_u *name)
546{
547 hashitem_T *hi;
548
549 hi = hash_find(&func_hashtab, name);
550 if (!HASHITEM_EMPTY(hi))
551 return HI2UF(hi);
552 return NULL;
553}
554
555/*
556 * Copy the function name of "fp" to buffer "buf".
557 * "buf" must be able to hold the function name plus three bytes.
558 * Takes care of script-local function names.
559 */
560 static void
561cat_func_name(char_u *buf, ufunc_T *fp)
562{
563 if (fp->uf_name[0] == K_SPECIAL)
564 {
565 STRCPY(buf, "<SNR>");
566 STRCAT(buf, fp->uf_name + 3);
567 }
568 else
569 STRCPY(buf, fp->uf_name);
570}
571
572/*
573 * Add a number variable "name" to dict "dp" with value "nr".
574 */
575 static void
576add_nr_var(
577 dict_T *dp,
578 dictitem_T *v,
579 char *name,
580 varnumber_T nr)
581{
582 STRCPY(v->di_key, name);
583 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
584 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
585 v->di_tv.v_type = VAR_NUMBER;
586 v->di_tv.v_lock = VAR_FIXED;
587 v->di_tv.vval.v_number = nr;
588}
589
590/*
591 * Free "fc" and what it contains.
592 */
593 static void
594free_funccal(
595 funccall_T *fc,
596 int free_val) /* a: vars were allocated */
597{
598 listitem_T *li;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200599 int i;
600
601 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
602 {
603 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
604
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200605 /* When garbage collecting a funccall_T may be freed before the
606 * function that references it, clear its uf_scoped field.
607 * The function may have been redefined and point to another
608 * funccall_T, don't clear it then. */
609 if (fp != NULL && fp->uf_scoped == fc)
610 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200611 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200612 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200613
614 /* The a: variables typevals may not have been allocated, only free the
615 * allocated variables. */
616 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
617
618 /* free all l: variables */
619 vars_clear(&fc->l_vars.dv_hashtab);
620
621 /* Free the a:000 variables if they were allocated. */
622 if (free_val)
623 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
624 clear_tv(&li->li_tv);
625
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200626 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627 vim_free(fc);
628}
629
630/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200631 * Handle the last part of returning from a function: free the local hashtable.
632 * Unless it is still in use by a closure.
633 */
634 static void
635cleanup_function_call(funccall_T *fc)
636{
637 current_funccal = fc->caller;
638
639 /* If the a:000 list and the l: and a: dicts are not referenced and there
640 * is no closure using it, we can free the funccall_T and what's in it. */
641 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
642 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
643 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT
644 && fc->fc_refcount <= 0)
645 {
646 free_funccal(fc, FALSE);
647 }
648 else
649 {
650 hashitem_T *hi;
651 listitem_T *li;
652 int todo;
653 dictitem_T *v;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100654 static int made_copy = 0;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200655
656 /* "fc" is still in use. This can happen when returning "a:000",
657 * assigning "l:" to a global variable or defining a closure.
658 * Link "fc" in the list for garbage collection later. */
659 fc->caller = previous_funccal;
660 previous_funccal = fc;
661
662 /* Make a copy of the a: variables, since we didn't do that above. */
663 todo = (int)fc->l_avars.dv_hashtab.ht_used;
664 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
665 {
666 if (!HASHITEM_EMPTY(hi))
667 {
668 --todo;
669 v = HI2DI(hi);
670 copy_tv(&v->di_tv, &v->di_tv);
671 }
672 }
673
674 /* Make a copy of the a:000 items, since we didn't do that above. */
675 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
676 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100677
678 if (++made_copy == 10000)
679 {
680 // We have made a lot of copies. This can happen when
681 // repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100682 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100683 // too much memory.
684 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100685 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100686 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200687 }
688}
689
690/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200691 * Call a user function.
692 */
693 static void
694call_user_func(
695 ufunc_T *fp, /* pointer to function */
696 int argcount, /* nr of args */
697 typval_T *argvars, /* arguments */
698 typval_T *rettv, /* return value */
699 linenr_T firstline, /* first line of range */
700 linenr_T lastline, /* last line of range */
701 dict_T *selfdict) /* Dictionary for "self" */
702{
703 char_u *save_sourcing_name;
704 linenr_T save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200705 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +0200706 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200707 funccall_T *fc;
708 int save_did_emsg;
709 static int depth = 0;
710 dictitem_T *v;
711 int fixvar_idx = 0; /* index in fixvar[] */
712 int i;
713 int ai;
714 int islambda = FALSE;
715 char_u numbuf[NUMBUFLEN];
716 char_u *name;
717 size_t len;
718#ifdef FEAT_PROFILE
719 proftime_T wait_start;
720 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +0200721 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200722#endif
723
724 /* If depth of calling is getting too high, don't execute the function */
725 if (depth >= p_mfd)
726 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100727 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200728 rettv->v_type = VAR_NUMBER;
729 rettv->vval.v_number = -1;
730 return;
731 }
732 ++depth;
733
734 line_breakcheck(); /* check for CTRL-C hit */
735
736 fc = (funccall_T *)alloc(sizeof(funccall_T));
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100737 if (fc == NULL)
738 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200739 fc->caller = current_funccal;
740 current_funccal = fc;
741 fc->func = fp;
742 fc->rettv = rettv;
743 rettv->vval.v_number = 0;
744 fc->linenr = 0;
745 fc->returned = FALSE;
746 fc->level = ex_nesting_level;
747 /* Check if this function has a breakpoint. */
748 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
749 fc->dbg_tick = debug_tick;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200750 /* Set up fields for closure. */
751 fc->fc_refcount = 0;
752 fc->fc_copyID = 0;
753 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200754 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200755
756 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
757 islambda = TRUE;
758
759 /*
760 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
761 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
762 * each argument variable and saves a lot of time.
763 */
764 /*
765 * Init l: variables.
766 */
767 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
768 if (selfdict != NULL)
769 {
770 /* Set l:self to "selfdict". Use "name" to avoid a warning from
771 * some compiler that checks the destination size. */
772 v = &fc->fixvar[fixvar_idx++].var;
773 name = v->di_key;
774 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +0100775 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200776 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
777 v->di_tv.v_type = VAR_DICT;
778 v->di_tv.v_lock = 0;
779 v->di_tv.vval.v_dict = selfdict;
780 ++selfdict->dv_refcount;
781 }
782
783 /*
784 * Init a: variables.
785 * Set a:0 to "argcount".
786 * Set a:000 to a list with room for the "..." arguments.
787 */
788 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
789 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
790 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar31b81602019-02-10 22:14:27 +0100791 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200792 /* Use "name" to avoid a warning from some compiler that checks the
793 * destination size. */
794 v = &fc->fixvar[fixvar_idx++].var;
795 name = v->di_key;
796 STRCPY(name, "000");
797 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
798 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
799 v->di_tv.v_type = VAR_LIST;
800 v->di_tv.v_lock = VAR_FIXED;
801 v->di_tv.vval.v_list = &fc->l_varlist;
802 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
803 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
804 fc->l_varlist.lv_lock = VAR_FIXED;
805
806 /*
807 * Set a:firstline to "firstline" and a:lastline to "lastline".
808 * Set a:name to named arguments.
809 * Set a:N to the "..." arguments.
810 */
811 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
812 (varnumber_T)firstline);
813 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
814 (varnumber_T)lastline);
815 for (i = 0; i < argcount; ++i)
816 {
817 int addlocal = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200818
819 ai = i - fp->uf_args.ga_len;
820 if (ai < 0)
821 {
822 /* named argument a:name */
823 name = FUNCARG(fp, i);
824 if (islambda)
825 addlocal = TRUE;
826 }
827 else
828 {
829 /* "..." argument a:1, a:2, etc. */
830 sprintf((char *)numbuf, "%d", ai + 1);
831 name = numbuf;
832 }
833 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
834 {
835 v = &fc->fixvar[fixvar_idx++].var;
836 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200837 }
838 else
839 {
840 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
841 + STRLEN(name)));
842 if (v == NULL)
843 break;
844 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200845 }
846 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200847
848 /* Note: the values are copied directly to avoid alloc/free.
849 * "argvars" must have VAR_FIXED for v_lock. */
850 v->di_tv = argvars[i];
851 v->di_tv.v_lock = VAR_FIXED;
852
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200853 if (addlocal)
854 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200855 /* Named arguments should be accessed without the "a:" prefix in
856 * lambda expressions. Add to the l: dict. */
857 copy_tv(&v->di_tv, &v->di_tv);
858 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200859 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200860 else
861 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200862
863 if (ai >= 0 && ai < MAX_FUNC_ARGS)
864 {
865 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
866 fc->l_listitems[ai].li_tv = argvars[i];
867 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
868 }
869 }
870
871 /* Don't redraw while executing the function. */
872 ++RedrawingDisabled;
873 save_sourcing_name = sourcing_name;
874 save_sourcing_lnum = sourcing_lnum;
875 sourcing_lnum = 1;
Bram Moolenaar93343722018-07-10 19:39:18 +0200876
877 if (fp->uf_flags & FC_SANDBOX)
878 {
879 using_sandbox = TRUE;
880 ++sandbox;
881 }
882
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200883 /* need space for function name + ("function " + 3) or "[number]" */
884 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
885 + STRLEN(fp->uf_name) + 20;
886 sourcing_name = alloc((unsigned)len);
887 if (sourcing_name != NULL)
888 {
889 if (save_sourcing_name != NULL
890 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
891 sprintf((char *)sourcing_name, "%s[%d]..",
892 save_sourcing_name, (int)save_sourcing_lnum);
893 else
894 STRCPY(sourcing_name, "function ");
895 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
896
897 if (p_verbose >= 12)
898 {
899 ++no_wait_return;
900 verbose_enter_scroll();
901
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100902 smsg(_("calling %s"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200903 if (p_verbose >= 14)
904 {
905 char_u buf[MSG_BUF_LEN];
906 char_u numbuf2[NUMBUFLEN];
907 char_u *tofree;
908 char_u *s;
909
Bram Moolenaar32526b32019-01-19 17:43:09 +0100910 msg_puts("(");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200911 for (i = 0; i < argcount; ++i)
912 {
913 if (i > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100914 msg_puts(", ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200915 if (argvars[i].v_type == VAR_NUMBER)
916 msg_outnum((long)argvars[i].vval.v_number);
917 else
918 {
919 /* Do not want errors such as E724 here. */
920 ++emsg_off;
921 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
922 --emsg_off;
923 if (s != NULL)
924 {
925 if (vim_strsize(s) > MSG_BUF_CLEN)
926 {
927 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
928 s = buf;
929 }
Bram Moolenaar32526b32019-01-19 17:43:09 +0100930 msg_puts((char *)s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200931 vim_free(tofree);
932 }
933 }
934 }
Bram Moolenaar32526b32019-01-19 17:43:09 +0100935 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200936 }
Bram Moolenaar32526b32019-01-19 17:43:09 +0100937 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200938
939 verbose_leave_scroll();
940 --no_wait_return;
941 }
942 }
943#ifdef FEAT_PROFILE
944 if (do_profiling == PROF_YES)
945 {
946 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +0200947 {
948 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200949 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +0200950 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200951 if (fp->uf_profiling
952 || (fc->caller != NULL && fc->caller->func->uf_profiling))
953 {
954 ++fp->uf_tm_count;
955 profile_start(&call_start);
956 profile_zero(&fp->uf_tm_children);
957 }
958 script_prof_save(&wait_start);
959 }
960#endif
961
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200962 save_current_sctx = current_sctx;
963 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200964 save_did_emsg = did_emsg;
965 did_emsg = FALSE;
966
967 /* call do_cmdline() to execute the lines */
968 do_cmdline(NULL, get_func_line, (void *)fc,
969 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
970
971 --RedrawingDisabled;
972
973 /* when the function was aborted because of an error, return -1 */
974 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
975 {
976 clear_tv(rettv);
977 rettv->v_type = VAR_NUMBER;
978 rettv->vval.v_number = -1;
979 }
980
981#ifdef FEAT_PROFILE
982 if (do_profiling == PROF_YES && (fp->uf_profiling
983 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
984 {
985 profile_end(&call_start);
986 profile_sub_wait(&wait_start, &call_start);
987 profile_add(&fp->uf_tm_total, &call_start);
988 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
989 if (fc->caller != NULL && fc->caller->func->uf_profiling)
990 {
991 profile_add(&fc->caller->func->uf_tm_children, &call_start);
992 profile_add(&fc->caller->func->uf_tml_children, &call_start);
993 }
Bram Moolenaarad648092018-06-30 18:28:03 +0200994 if (started_profiling)
995 // make a ":profdel func" stop profiling the function
996 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200997 }
998#endif
999
1000 /* when being verbose, mention the return value */
1001 if (p_verbose >= 12)
1002 {
1003 ++no_wait_return;
1004 verbose_enter_scroll();
1005
1006 if (aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001007 smsg(_("%s aborted"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001008 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001009 smsg(_("%s returning #%ld"), sourcing_name,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001010 (long)fc->rettv->vval.v_number);
1011 else
1012 {
1013 char_u buf[MSG_BUF_LEN];
1014 char_u numbuf2[NUMBUFLEN];
1015 char_u *tofree;
1016 char_u *s;
1017
1018 /* The value may be very long. Skip the middle part, so that we
1019 * have some idea how it starts and ends. smsg() would always
1020 * truncate it at the end. Don't want errors such as E724 here. */
1021 ++emsg_off;
1022 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1023 --emsg_off;
1024 if (s != NULL)
1025 {
1026 if (vim_strsize(s) > MSG_BUF_CLEN)
1027 {
1028 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1029 s = buf;
1030 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001031 smsg(_("%s returning %s"), sourcing_name, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001032 vim_free(tofree);
1033 }
1034 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001035 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001036
1037 verbose_leave_scroll();
1038 --no_wait_return;
1039 }
1040
1041 vim_free(sourcing_name);
1042 sourcing_name = save_sourcing_name;
1043 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001044 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001045#ifdef FEAT_PROFILE
1046 if (do_profiling == PROF_YES)
1047 script_prof_restore(&wait_start);
1048#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001049 if (using_sandbox)
1050 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001051
1052 if (p_verbose >= 12 && sourcing_name != NULL)
1053 {
1054 ++no_wait_return;
1055 verbose_enter_scroll();
1056
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001057 smsg(_("continuing in %s"), sourcing_name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001058 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001059
1060 verbose_leave_scroll();
1061 --no_wait_return;
1062 }
1063
1064 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001065 --depth;
1066
Bram Moolenaar6914c642017-04-01 21:21:30 +02001067 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001068}
1069
1070/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001071 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001072 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001073 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001074 */
1075 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001076funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001077{
1078 funccall_T **pfc;
1079 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001080
1081 if (fc == NULL)
1082 return;
1083
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001084 if (--fc->fc_refcount <= 0 && (force || (
1085 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001086 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001087 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001088 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001089 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001090 if (fc == *pfc)
1091 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001092 *pfc = fc->caller;
1093 free_funccal(fc, TRUE);
1094 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001095 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001096 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001097 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001098 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001099 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001100}
1101
1102/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001103 * Remove the function from the function hashtable. If the function was
1104 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001105 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001106 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001107 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001108func_remove(ufunc_T *fp)
1109{
1110 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1111
1112 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001113 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001114 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001115 return TRUE;
1116 }
1117 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001118}
1119
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001120 static void
1121func_clear_items(ufunc_T *fp)
1122{
1123 ga_clear_strings(&(fp->uf_args));
1124 ga_clear_strings(&(fp->uf_lines));
1125#ifdef FEAT_PROFILE
1126 vim_free(fp->uf_tml_count);
1127 fp->uf_tml_count = NULL;
1128 vim_free(fp->uf_tml_total);
1129 fp->uf_tml_total = NULL;
1130 vim_free(fp->uf_tml_self);
1131 fp->uf_tml_self = NULL;
1132#endif
1133}
1134
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001135/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001136 * Free all things that a function contains. Does not free the function
1137 * itself, use func_free() for that.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001138 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001139 */
1140 static void
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001141func_clear(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001142{
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001143 if (fp->uf_cleared)
1144 return;
1145 fp->uf_cleared = TRUE;
1146
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001147 /* clear this function */
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001148 func_clear_items(fp);
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001149 funccal_unref(fp->uf_scoped, fp, force);
1150}
1151
1152/*
1153 * Free a function and remove it from the list of functions. Does not free
1154 * what a function contains, call func_clear() first.
1155 */
1156 static void
1157func_free(ufunc_T *fp)
1158{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001159 /* only remove it when not done already, otherwise we would remove a newer
1160 * version of the function */
1161 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1162 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001163
1164 vim_free(fp);
1165}
1166
Bram Moolenaarc2574872016-08-11 22:51:05 +02001167/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001168 * Free all things that a function contains and free the function itself.
1169 * When "force" is TRUE we are exiting.
1170 */
1171 static void
1172func_clear_free(ufunc_T *fp, int force)
1173{
1174 func_clear(fp, force);
1175 func_free(fp);
1176}
1177
1178/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001179 * There are two kinds of function names:
1180 * 1. ordinary names, function defined with :function
1181 * 2. numbered functions and lambdas
1182 * For the first we only count the name stored in func_hashtab as a reference,
1183 * using function() does not count as a reference, because the function is
1184 * looked up by name.
1185 */
1186 static int
1187func_name_refcount(char_u *name)
1188{
1189 return isdigit(*name) || *name == '<';
1190}
1191
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001192static funccal_entry_T *funccal_stack = NULL;
1193
1194/*
1195 * Save the current function call pointer, and set it to NULL.
1196 * Used when executing autocommands and for ":source".
1197 */
1198 void
1199save_funccal(funccal_entry_T *entry)
1200{
1201 entry->top_funccal = current_funccal;
1202 entry->next = funccal_stack;
1203 funccal_stack = entry;
1204 current_funccal = NULL;
1205}
1206
1207 void
1208restore_funccal(void)
1209{
1210 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001211 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001212 else
1213 {
1214 current_funccal = funccal_stack->top_funccal;
1215 funccal_stack = funccal_stack->next;
1216 }
1217}
1218
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001219#if defined(EXITFREE) || defined(PROTO)
1220 void
1221free_all_functions(void)
1222{
1223 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001224 ufunc_T *fp;
1225 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001226 long_u todo = 1;
1227 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001228
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001229 /* Clean up the current_funccal chain and the funccal stack. */
Bram Moolenaar6914c642017-04-01 21:21:30 +02001230 while (current_funccal != NULL)
1231 {
1232 clear_tv(current_funccal->rettv);
1233 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001234 if (current_funccal == NULL && funccal_stack != NULL)
1235 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001236 }
1237
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001238 /* First clear what the functions contain. Since this may lower the
1239 * reference count of a function, it may also free a function and change
1240 * the hash table. Restart if that happens. */
1241 while (todo > 0)
1242 {
1243 todo = func_hashtab.ht_used;
1244 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1245 if (!HASHITEM_EMPTY(hi))
1246 {
1247 /* Only free functions that are not refcounted, those are
1248 * supposed to be freed when no longer referenced. */
1249 fp = HI2UF(hi);
1250 if (func_name_refcount(fp->uf_name))
1251 ++skipped;
1252 else
1253 {
1254 used = func_hashtab.ht_used;
1255 func_clear(fp, TRUE);
1256 if (used != func_hashtab.ht_used)
1257 {
1258 skipped = 0;
1259 break;
1260 }
1261 }
1262 --todo;
1263 }
1264 }
1265
1266 /* Now actually free the functions. Need to start all over every time,
1267 * because func_free() may change the hash table. */
1268 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001269 while (func_hashtab.ht_used > skipped)
1270 {
1271 todo = func_hashtab.ht_used;
1272 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001273 if (!HASHITEM_EMPTY(hi))
1274 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001275 --todo;
1276 /* Only free functions that are not refcounted, those are
1277 * supposed to be freed when no longer referenced. */
1278 fp = HI2UF(hi);
1279 if (func_name_refcount(fp->uf_name))
1280 ++skipped;
1281 else
1282 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001283 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001284 skipped = 0;
1285 break;
1286 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001287 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001288 }
1289 if (skipped == 0)
1290 hash_clear(&func_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001291}
1292#endif
1293
1294/*
1295 * Return TRUE if "name" looks like a builtin function name: starts with a
1296 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1297 * "len" is the length of "name", or -1 for NUL terminated.
1298 */
1299 static int
1300builtin_function(char_u *name, int len)
1301{
1302 char_u *p;
1303
1304 if (!ASCII_ISLOWER(name[0]))
1305 return FALSE;
1306 p = vim_strchr(name, AUTOLOAD_CHAR);
1307 return p == NULL || (len > 0 && p > name + len);
1308}
1309
1310 int
1311func_call(
1312 char_u *name,
1313 typval_T *args,
1314 partial_T *partial,
1315 dict_T *selfdict,
1316 typval_T *rettv)
1317{
1318 listitem_T *item;
1319 typval_T argv[MAX_FUNC_ARGS + 1];
1320 int argc = 0;
1321 int dummy;
1322 int r = 0;
1323
1324 for (item = args->vval.v_list->lv_first; item != NULL;
1325 item = item->li_next)
1326 {
1327 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1328 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001329 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001330 break;
1331 }
1332 /* Make a copy of each argument. This is needed to be able to set
1333 * v_lock to VAR_FIXED in the copy without changing the original list.
1334 */
1335 copy_tv(&item->li_tv, &argv[argc++]);
1336 }
1337
1338 if (item == NULL)
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001339 r = call_func(name, (int)STRLEN(name), rettv, argc, argv, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001340 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
1341 &dummy, TRUE, partial, selfdict);
1342
1343 /* Free the arguments. */
1344 while (argc > 0)
1345 clear_tv(&argv[--argc]);
1346
1347 return r;
1348}
1349
1350/*
1351 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001352 *
1353 * "argv_func", when not NULL, can be used to fill in arguments only when the
1354 * invoked function uses them. It is called like this:
1355 * new_argcount = argv_func(current_argcount, argv, called_func_argcount)
1356 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001357 * Return FAIL when the function can't be called, OK otherwise.
1358 * Also returns OK when an error was encountered while executing the function.
1359 */
1360 int
1361call_func(
1362 char_u *funcname, /* name of the function */
1363 int len, /* length of "name" */
1364 typval_T *rettv, /* return value goes here */
1365 int argcount_in, /* number of "argvars" */
1366 typval_T *argvars_in, /* vars for arguments, must have "argcount"
1367 PLUS ONE elements! */
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001368 int (* argv_func)(int, typval_T *, int),
1369 /* function to fill in argvars */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001370 linenr_T firstline, /* first line of range */
1371 linenr_T lastline, /* last line of range */
1372 int *doesrange, /* return: function handled range */
1373 int evaluate,
1374 partial_T *partial, /* optional, can be NULL */
1375 dict_T *selfdict_in) /* Dictionary for "self" */
1376{
1377 int ret = FAIL;
1378 int error = ERROR_NONE;
1379 int i;
1380 ufunc_T *fp;
1381 char_u fname_buf[FLEN_FIXED + 1];
1382 char_u *tofree = NULL;
1383 char_u *fname;
1384 char_u *name;
1385 int argcount = argcount_in;
1386 typval_T *argvars = argvars_in;
1387 dict_T *selfdict = selfdict_in;
1388 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
1389 int argv_clear = 0;
1390
1391 /* Make a copy of the name, if it comes from a funcref variable it could
1392 * be changed or deleted in the called function. */
1393 name = vim_strnsave(funcname, len);
1394 if (name == NULL)
1395 return ret;
1396
1397 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1398
1399 *doesrange = FALSE;
1400
1401 if (partial != NULL)
1402 {
1403 /* When the function has a partial with a dict and there is a dict
1404 * argument, use the dict argument. That is backwards compatible.
1405 * When the dict was bound explicitly use the one from the partial. */
1406 if (partial->pt_dict != NULL
1407 && (selfdict_in == NULL || !partial->pt_auto))
1408 selfdict = partial->pt_dict;
1409 if (error == ERROR_NONE && partial->pt_argc > 0)
1410 {
1411 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
1412 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
1413 for (i = 0; i < argcount_in; ++i)
1414 argv[i + argv_clear] = argvars_in[i];
1415 argvars = argv;
1416 argcount = partial->pt_argc + argcount_in;
1417 }
1418 }
1419
1420
Bram Moolenaarb4518562018-05-22 18:31:35 +02001421 /*
1422 * Execute the function if executing and no errors were detected.
1423 */
1424 if (!evaluate)
1425 {
1426 // Not evaluating, which means the return value is unknown. This
1427 // matters for giving error messages.
1428 rettv->v_type = VAR_UNKNOWN;
1429 }
1430 else if (error == ERROR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431 {
1432 char_u *rfname = fname;
1433
1434 /* Ignore "g:" before a function name. */
1435 if (fname[0] == 'g' && fname[1] == ':')
1436 rfname = fname + 2;
1437
1438 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
1439 rettv->vval.v_number = 0;
1440 error = ERROR_UNKNOWN;
1441
1442 if (!builtin_function(rfname, -1))
1443 {
1444 /*
1445 * User defined function.
1446 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001447 if (partial != NULL && partial->pt_func != NULL)
1448 fp = partial->pt_func;
1449 else
1450 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001451
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001452 /* Trigger FuncUndefined event, may load the function. */
1453 if (fp == NULL
1454 && apply_autocmds(EVENT_FUNCUNDEFINED,
1455 rfname, rfname, TRUE, NULL)
1456 && !aborting())
1457 {
1458 /* executed an autocommand, search for the function again */
1459 fp = find_func(rfname);
1460 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001461 /* Try loading a package. */
1462 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1463 {
1464 /* loaded a package, search for the function again */
1465 fp = find_func(rfname);
1466 }
1467
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001468 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1469 error = ERROR_DELETED;
1470 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001471 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001472 if (argv_func != NULL)
1473 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len);
1474
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001475 if (fp->uf_flags & FC_RANGE)
1476 *doesrange = TRUE;
1477 if (argcount < fp->uf_args.ga_len)
1478 error = ERROR_TOOFEW;
1479 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1480 error = ERROR_TOOMANY;
1481 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1482 error = ERROR_DICT;
1483 else
1484 {
1485 int did_save_redo = FALSE;
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001486 save_redo_T save_redo;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001487
1488 /*
1489 * Call the user function.
1490 * Save and restore search patterns, script variables and
1491 * redo buffer.
1492 */
1493 save_search_patterns();
1494#ifdef FEAT_INS_EXPAND
1495 if (!ins_compl_active())
1496#endif
1497 {
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001498 saveRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001499 did_save_redo = TRUE;
1500 }
1501 ++fp->uf_calls;
1502 call_user_func(fp, argcount, argvars, rettv,
1503 firstline, lastline,
1504 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001505 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001506 /* Function was unreferenced while being used, free it
1507 * now. */
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001508 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001509 if (did_save_redo)
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001510 restoreRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511 restore_search_patterns();
1512 error = ERROR_NONE;
1513 }
1514 }
1515 }
1516 else
1517 {
1518 /*
1519 * Find the function name in the table, call its implementation.
1520 */
1521 error = call_internal_func(fname, argcount, argvars, rettv);
1522 }
1523 /*
1524 * The function call (or "FuncUndefined" autocommand sequence) might
1525 * have been aborted by an error, an interrupt, or an explicitly thrown
1526 * exception that has not been caught so far. This situation can be
1527 * tested for by calling aborting(). For an error in an internal
1528 * function or for the "E132" error in call_user_func(), however, the
1529 * throw point at which the "force_abort" flag (temporarily reset by
1530 * emsg()) is normally updated has not been reached yet. We need to
1531 * update that flag first to make aborting() reliable.
1532 */
1533 update_force_abort();
1534 }
1535 if (error == ERROR_NONE)
1536 ret = OK;
1537
1538 /*
1539 * Report an error unless the argument evaluation or function call has been
1540 * cancelled due to an aborting error, an interrupt, or an exception.
1541 */
1542 if (!aborting())
1543 {
1544 switch (error)
1545 {
1546 case ERROR_UNKNOWN:
1547 emsg_funcname(N_("E117: Unknown function: %s"), name);
1548 break;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001549 case ERROR_DELETED:
1550 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1551 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552 case ERROR_TOOMANY:
1553 emsg_funcname((char *)e_toomanyarg, name);
1554 break;
1555 case ERROR_TOOFEW:
1556 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
1557 name);
1558 break;
1559 case ERROR_SCRIPT:
1560 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
1561 name);
1562 break;
1563 case ERROR_DICT:
1564 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
1565 name);
1566 break;
1567 }
1568 }
1569
1570 while (argv_clear > 0)
1571 clear_tv(&argv[--argv_clear]);
1572 vim_free(tofree);
1573 vim_free(name);
1574
1575 return ret;
1576}
1577
1578/*
1579 * List the head of the function: "name(arg1, arg2)".
1580 */
1581 static void
1582list_func_head(ufunc_T *fp, int indent)
1583{
1584 int j;
1585
1586 msg_start();
1587 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001588 msg_puts(" ");
1589 msg_puts("function ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001590 if (fp->uf_name[0] == K_SPECIAL)
1591 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001592 msg_puts_attr("<SNR>", HL_ATTR(HLF_8));
1593 msg_puts((char *)fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001594 }
1595 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001596 msg_puts((char *)fp->uf_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001597 msg_putchar('(');
1598 for (j = 0; j < fp->uf_args.ga_len; ++j)
1599 {
1600 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001601 msg_puts(", ");
1602 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001603 }
1604 if (fp->uf_varargs)
1605 {
1606 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001607 msg_puts(", ");
1608 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 }
1610 msg_putchar(')');
1611 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001612 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001613 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001614 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001615 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001616 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001617 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001618 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001619 msg_clr_eos();
1620 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001621 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001622}
1623
1624/*
1625 * Get a function name, translating "<SID>" and "<SNR>".
1626 * Also handles a Funcref in a List or Dictionary.
1627 * Returns the function name in allocated memory, or NULL for failure.
1628 * flags:
1629 * TFN_INT: internal function name OK
1630 * TFN_QUIET: be quiet
1631 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001632 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001633 * Advances "pp" to just after the function name (if no error).
1634 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001635 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001636trans_function_name(
1637 char_u **pp,
1638 int skip, /* only find the end, don't evaluate */
1639 int flags,
1640 funcdict_T *fdp, /* return: info about dictionary used */
1641 partial_T **partial) /* return: partial of a FuncRef */
1642{
1643 char_u *name = NULL;
1644 char_u *start;
1645 char_u *end;
1646 int lead;
1647 char_u sid_buf[20];
1648 int len;
1649 lval_T lv;
1650
1651 if (fdp != NULL)
1652 vim_memset(fdp, 0, sizeof(funcdict_T));
1653 start = *pp;
1654
1655 /* Check for hard coded <SNR>: already translated function ID (from a user
1656 * command). */
1657 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1658 && (*pp)[2] == (int)KE_SNR)
1659 {
1660 *pp += 3;
1661 len = get_id_len(pp) + 3;
1662 return vim_strnsave(start, len);
1663 }
1664
1665 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
1666 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
1667 lead = eval_fname_script(start);
1668 if (lead > 2)
1669 start += lead;
1670
1671 /* Note that TFN_ flags use the same values as GLV_ flags. */
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001672 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001673 lead > 2 ? 0 : FNE_CHECK_START);
1674 if (end == start)
1675 {
1676 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001677 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001678 goto theend;
1679 }
1680 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1681 {
1682 /*
1683 * Report an invalid expression in braces, unless the expression
1684 * evaluation has been cancelled due to an aborting error, an
1685 * interrupt, or an exception.
1686 */
1687 if (!aborting())
1688 {
1689 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001690 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001691 }
1692 else
1693 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1694 goto theend;
1695 }
1696
1697 if (lv.ll_tv != NULL)
1698 {
1699 if (fdp != NULL)
1700 {
1701 fdp->fd_dict = lv.ll_dict;
1702 fdp->fd_newkey = lv.ll_newkey;
1703 lv.ll_newkey = NULL;
1704 fdp->fd_di = lv.ll_di;
1705 }
1706 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1707 {
1708 name = vim_strsave(lv.ll_tv->vval.v_string);
1709 *pp = end;
1710 }
1711 else if (lv.ll_tv->v_type == VAR_PARTIAL
1712 && lv.ll_tv->vval.v_partial != NULL)
1713 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001714 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 *pp = end;
1716 if (partial != NULL)
1717 *partial = lv.ll_tv->vval.v_partial;
1718 }
1719 else
1720 {
1721 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1722 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001723 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001724 else
1725 *pp = end;
1726 name = NULL;
1727 }
1728 goto theend;
1729 }
1730
1731 if (lv.ll_name == NULL)
1732 {
1733 /* Error found, but continue after the function name. */
1734 *pp = end;
1735 goto theend;
1736 }
1737
1738 /* Check if the name is a Funcref. If so, use the value. */
1739 if (lv.ll_exp_name != NULL)
1740 {
1741 len = (int)STRLEN(lv.ll_exp_name);
1742 name = deref_func_name(lv.ll_exp_name, &len, partial,
1743 flags & TFN_NO_AUTOLOAD);
1744 if (name == lv.ll_exp_name)
1745 name = NULL;
1746 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001747 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001748 {
1749 len = (int)(end - *pp);
1750 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1751 if (name == *pp)
1752 name = NULL;
1753 }
1754 if (name != NULL)
1755 {
1756 name = vim_strsave(name);
1757 *pp = end;
1758 if (STRNCMP(name, "<SNR>", 5) == 0)
1759 {
1760 /* Change "<SNR>" to the byte sequence. */
1761 name[0] = K_SPECIAL;
1762 name[1] = KS_EXTRA;
1763 name[2] = (int)KE_SNR;
1764 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1765 }
1766 goto theend;
1767 }
1768
1769 if (lv.ll_exp_name != NULL)
1770 {
1771 len = (int)STRLEN(lv.ll_exp_name);
1772 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1773 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1774 {
1775 /* When there was "s:" already or the name expanded to get a
1776 * leading "s:" then remove it. */
1777 lv.ll_name += 2;
1778 len -= 2;
1779 lead = 2;
1780 }
1781 }
1782 else
1783 {
1784 /* skip over "s:" and "g:" */
1785 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1786 lv.ll_name += 2;
1787 len = (int)(end - lv.ll_name);
1788 }
1789
1790 /*
1791 * Copy the function name to allocated memory.
1792 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1793 * Accept <SNR>123_name() outside a script.
1794 */
1795 if (skip)
1796 lead = 0; /* do nothing */
1797 else if (lead > 0)
1798 {
1799 lead = 3;
1800 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1801 || eval_fname_sid(*pp))
1802 {
1803 /* It's "s:" or "<SID>" */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001804 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001806 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001807 goto theend;
1808 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001809 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001810 lead += (int)STRLEN(sid_buf);
1811 }
1812 }
1813 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1814 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001815 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001816 start);
1817 goto theend;
1818 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001819 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 {
1821 char_u *cp = vim_strchr(lv.ll_name, ':');
1822
1823 if (cp != NULL && cp < end)
1824 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001825 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 goto theend;
1827 }
1828 }
1829
1830 name = alloc((unsigned)(len + lead + 1));
1831 if (name != NULL)
1832 {
1833 if (lead > 0)
1834 {
1835 name[0] = K_SPECIAL;
1836 name[1] = KS_EXTRA;
1837 name[2] = (int)KE_SNR;
1838 if (lead > 3) /* If it's "<SID>" */
1839 STRCPY(name + 3, sid_buf);
1840 }
1841 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1842 name[lead + len] = NUL;
1843 }
1844 *pp = end;
1845
1846theend:
1847 clear_lval(&lv);
1848 return name;
1849}
1850
1851/*
1852 * ":function"
1853 */
1854 void
1855ex_function(exarg_T *eap)
1856{
1857 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02001858 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001859 int j;
1860 int c;
1861 int saved_did_emsg;
1862 int saved_wait_return = need_wait_return;
1863 char_u *name = NULL;
1864 char_u *p;
1865 char_u *arg;
1866 char_u *line_arg = NULL;
1867 garray_T newargs;
1868 garray_T newlines;
1869 int varargs = FALSE;
1870 int flags = 0;
1871 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001872 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001873 int indent;
1874 int nesting;
1875 char_u *skip_until = NULL;
1876 dictitem_T *v;
1877 funcdict_T fudi;
1878 static int func_nr = 0; /* number for nameless function */
1879 int paren;
1880 hashtab_T *ht;
1881 int todo;
1882 hashitem_T *hi;
1883 int sourcing_lnum_off;
1884
1885 /*
1886 * ":function" without argument: list functions.
1887 */
1888 if (ends_excmd(*eap->arg))
1889 {
1890 if (!eap->skip)
1891 {
1892 todo = (int)func_hashtab.ht_used;
1893 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
1894 {
1895 if (!HASHITEM_EMPTY(hi))
1896 {
1897 --todo;
1898 fp = HI2UF(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02001899 if (message_filtered(fp->uf_name))
1900 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001901 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001902 list_func_head(fp, FALSE);
1903 }
1904 }
1905 }
1906 eap->nextcmd = check_nextcmd(eap->arg);
1907 return;
1908 }
1909
1910 /*
1911 * ":function /pat": list functions matching pattern.
1912 */
1913 if (*eap->arg == '/')
1914 {
1915 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
1916 if (!eap->skip)
1917 {
1918 regmatch_T regmatch;
1919
1920 c = *p;
1921 *p = NUL;
1922 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
1923 *p = c;
1924 if (regmatch.regprog != NULL)
1925 {
1926 regmatch.rm_ic = p_ic;
1927
1928 todo = (int)func_hashtab.ht_used;
1929 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
1930 {
1931 if (!HASHITEM_EMPTY(hi))
1932 {
1933 --todo;
1934 fp = HI2UF(hi);
1935 if (!isdigit(*fp->uf_name)
1936 && vim_regexec(&regmatch, fp->uf_name, 0))
1937 list_func_head(fp, FALSE);
1938 }
1939 }
1940 vim_regfree(regmatch.regprog);
1941 }
1942 }
1943 if (*p == '/')
1944 ++p;
1945 eap->nextcmd = check_nextcmd(p);
1946 return;
1947 }
1948
1949 /*
1950 * Get the function name. There are these situations:
1951 * func normal function name
1952 * "name" == func, "fudi.fd_dict" == NULL
1953 * dict.func new dictionary entry
1954 * "name" == NULL, "fudi.fd_dict" set,
1955 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
1956 * dict.func existing dict entry with a Funcref
1957 * "name" == func, "fudi.fd_dict" set,
1958 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
1959 * dict.func existing dict entry that's not a Funcref
1960 * "name" == NULL, "fudi.fd_dict" set,
1961 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
1962 * s:func script-local function name
1963 * g:func global function name, same as "func"
1964 */
1965 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01001966 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 paren = (vim_strchr(p, '(') != NULL);
1968 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
1969 {
1970 /*
1971 * Return on an invalid expression in braces, unless the expression
1972 * evaluation has been cancelled due to an aborting error, an
1973 * interrupt, or an exception.
1974 */
1975 if (!aborting())
1976 {
1977 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 vim_free(fudi.fd_newkey);
1980 return;
1981 }
1982 else
1983 eap->skip = TRUE;
1984 }
1985
1986 /* An error in a function call during evaluation of an expression in magic
1987 * braces should not cause the function not to be defined. */
1988 saved_did_emsg = did_emsg;
1989 did_emsg = FALSE;
1990
1991 /*
1992 * ":function func" with only function name: list function.
1993 */
1994 if (!paren)
1995 {
1996 if (!ends_excmd(*skipwhite(p)))
1997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001998 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001999 goto ret_free;
2000 }
2001 eap->nextcmd = check_nextcmd(p);
2002 if (eap->nextcmd != NULL)
2003 *p = NUL;
2004 if (!eap->skip && !got_int)
2005 {
2006 fp = find_func(name);
2007 if (fp != NULL)
2008 {
2009 list_func_head(fp, TRUE);
2010 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2011 {
2012 if (FUNCLINE(fp, j) == NULL)
2013 continue;
2014 msg_putchar('\n');
2015 msg_outnum((long)(j + 1));
2016 if (j < 9)
2017 msg_putchar(' ');
2018 if (j < 99)
2019 msg_putchar(' ');
2020 msg_prt_line(FUNCLINE(fp, j), FALSE);
2021 out_flush(); /* show a line at a time */
2022 ui_breakcheck();
2023 }
2024 if (!got_int)
2025 {
2026 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01002027 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002028 }
2029 }
2030 else
2031 emsg_funcname(N_("E123: Undefined function: %s"), name);
2032 }
2033 goto ret_free;
2034 }
2035
2036 /*
2037 * ":function name(arg1, arg2)" Define function.
2038 */
2039 p = skipwhite(p);
2040 if (*p != '(')
2041 {
2042 if (!eap->skip)
2043 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002044 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002045 goto ret_free;
2046 }
2047 /* attempt to continue by skipping some text */
2048 if (vim_strchr(p, '(') != NULL)
2049 p = vim_strchr(p, '(');
2050 }
2051 p = skipwhite(p + 1);
2052
2053 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2054
2055 if (!eap->skip)
2056 {
2057 /* Check the name of the function. Unless it's a dictionary function
2058 * (that we are overwriting). */
2059 if (name != NULL)
2060 arg = name;
2061 else
2062 arg = fudi.fd_newkey;
2063 if (arg != NULL && (fudi.fd_di == NULL
2064 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2065 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2066 {
2067 if (*arg == K_SPECIAL)
2068 j = 3;
2069 else
2070 j = 0;
2071 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2072 : eval_isnamec(arg[j])))
2073 ++j;
2074 if (arg[j] != NUL)
2075 emsg_funcname((char *)e_invarg2, arg);
2076 }
2077 /* Disallow using the g: dict. */
2078 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002079 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002080 }
2081
2082 if (get_function_args(&p, ')', &newargs, &varargs, eap->skip) == FAIL)
2083 goto errret_2;
2084
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002085 /* find extra arguments "range", "dict", "abort" and "closure" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002086 for (;;)
2087 {
2088 p = skipwhite(p);
2089 if (STRNCMP(p, "range", 5) == 0)
2090 {
2091 flags |= FC_RANGE;
2092 p += 5;
2093 }
2094 else if (STRNCMP(p, "dict", 4) == 0)
2095 {
2096 flags |= FC_DICT;
2097 p += 4;
2098 }
2099 else if (STRNCMP(p, "abort", 5) == 0)
2100 {
2101 flags |= FC_ABORT;
2102 p += 5;
2103 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002104 else if (STRNCMP(p, "closure", 7) == 0)
2105 {
2106 flags |= FC_CLOSURE;
2107 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02002108 if (current_funccal == NULL)
2109 {
Bram Moolenaarba209902016-08-24 22:06:38 +02002110 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
Bram Moolenaar58016442016-07-31 18:30:22 +02002111 name == NULL ? (char_u *)"" : name);
2112 goto erret;
2113 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002114 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002115 else
2116 break;
2117 }
2118
2119 /* When there is a line break use what follows for the function body.
2120 * Makes 'exe "func Test()\n...\nendfunc"' work. */
2121 if (*p == '\n')
2122 line_arg = p + 1;
2123 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002124 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002125
2126 /*
2127 * Read the body of the function, until ":endfunction" is found.
2128 */
2129 if (KeyTyped)
2130 {
2131 /* Check if the function already exists, don't let the user type the
2132 * whole function before telling him it doesn't work! For a script we
2133 * need to skip the body to be able to find what follows. */
2134 if (!eap->skip && !eap->forceit)
2135 {
2136 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002137 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002138 else if (name != NULL && find_func(name) != NULL)
2139 emsg_funcname(e_funcexts, name);
2140 }
2141
2142 if (!eap->skip && did_emsg)
2143 goto erret;
2144
2145 msg_putchar('\n'); /* don't overwrite the function name */
2146 cmdline_row = msg_row;
2147 }
2148
2149 indent = 2;
2150 nesting = 0;
2151 for (;;)
2152 {
2153 if (KeyTyped)
2154 {
2155 msg_scroll = TRUE;
2156 saved_wait_return = FALSE;
2157 }
2158 need_wait_return = FALSE;
2159 sourcing_lnum_off = sourcing_lnum;
2160
2161 if (line_arg != NULL)
2162 {
2163 /* Use eap->arg, split up in parts by line breaks. */
2164 theline = line_arg;
2165 p = vim_strchr(theline, '\n');
2166 if (p == NULL)
2167 line_arg += STRLEN(line_arg);
2168 else
2169 {
2170 *p = NUL;
2171 line_arg = p + 1;
2172 }
2173 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002175 {
2176 vim_free(line_to_free);
2177 if (eap->getline == NULL)
2178 theline = getcmdline(':', 0L, indent);
2179 else
2180 theline = eap->getline(':', eap->cookie, indent);
2181 line_to_free = theline;
2182 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002183 if (KeyTyped)
2184 lines_left = Rows - 1;
2185 if (theline == NULL)
2186 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002187 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 goto erret;
2189 }
2190
2191 /* Detect line continuation: sourcing_lnum increased more than one. */
2192 if (sourcing_lnum > sourcing_lnum_off + 1)
2193 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
2194 else
2195 sourcing_lnum_off = 0;
2196
2197 if (skip_until != NULL)
2198 {
2199 /* between ":append" and "." and between ":python <<EOF" and "EOF"
2200 * don't check for ":endfunc". */
2201 if (STRCMP(theline, skip_until) == 0)
Bram Moolenaard23a8232018-02-10 18:45:26 +01002202 VIM_CLEAR(skip_until);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 }
2204 else
2205 {
2206 /* skip ':' and blanks*/
Bram Moolenaar1c465442017-03-12 20:10:05 +01002207 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002208 ;
2209
2210 /* Check for "endfunction". */
2211 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2212 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002213 char_u *nextcmd = NULL;
2214
Bram Moolenaar663bb232017-06-22 19:12:10 +02002215 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002216 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002217 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002218 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002219 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002220 give_warning2(
2221 (char_u *)_("W22: Text found after :endfunction: %s"),
2222 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002223 if (nextcmd != NULL)
2224 {
2225 /* Another command follows. If the line came from "eap" we
2226 * can simply point into it, otherwise we need to change
2227 * "eap->cmdlinep". */
2228 eap->nextcmd = nextcmd;
2229 if (line_to_free != NULL)
2230 {
2231 vim_free(*eap->cmdlinep);
2232 *eap->cmdlinep = line_to_free;
2233 line_to_free = NULL;
2234 }
2235 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 break;
2237 }
2238
2239 /* Increase indent inside "if", "while", "for" and "try", decrease
2240 * at "end". */
2241 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2242 indent -= 2;
2243 else if (STRNCMP(p, "if", 2) == 0
2244 || STRNCMP(p, "wh", 2) == 0
2245 || STRNCMP(p, "for", 3) == 0
2246 || STRNCMP(p, "try", 3) == 0)
2247 indent += 2;
2248
2249 /* Check for defining a function inside this function. */
2250 if (checkforcmd(&p, "function", 2))
2251 {
2252 if (*p == '!')
2253 p = skipwhite(p + 1);
2254 p += eval_fname_script(p);
2255 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2256 if (*skipwhite(p) == '(')
2257 {
2258 ++nesting;
2259 indent += 2;
2260 }
2261 }
2262
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002263 /* Check for ":append", ":change", ":insert". */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002264 p = skip_range(p, NULL);
2265 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002266 || (p[0] == 'c'
2267 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2268 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2269 && (STRNCMP(&p[3], "nge", 3) != 0
2270 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002271 || (p[0] == 'i'
2272 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2273 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2274 skip_until = vim_strsave((char_u *)".");
2275
2276 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
2277 arg = skipwhite(skiptowhite(p));
2278 if (arg[0] == '<' && arg[1] =='<'
2279 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002280 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2281 || ((p[2] == '3' || p[2] == 'x')
2282 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002283 || (p[0] == 'p' && p[1] == 'e'
2284 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2285 || (p[0] == 't' && p[1] == 'c'
2286 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2287 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2288 && !ASCII_ISALPHA(p[3]))
2289 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2290 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2291 || (p[0] == 'm' && p[1] == 'z'
2292 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2293 ))
2294 {
2295 /* ":python <<" continues until a dot, like ":append" */
2296 p = skipwhite(arg + 2);
2297 if (*p == NUL)
2298 skip_until = vim_strsave((char_u *)".");
2299 else
2300 skip_until = vim_strsave(p);
2301 }
2302 }
2303
2304 /* Add the line to the function. */
2305 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002306 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307
2308 /* Copy the line to newly allocated memory. get_one_sourceline()
2309 * allocates 250 bytes per line, this saves 80% on average. The cost
2310 * is an extra alloc/free. */
2311 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002312 if (p == NULL)
2313 goto erret;
2314 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002315
2316 /* Add NULL lines for continuation lines, so that the line count is
2317 * equal to the index in the growarray. */
2318 while (sourcing_lnum_off-- > 0)
2319 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2320
2321 /* Check for end of eap->arg. */
2322 if (line_arg != NULL && *line_arg == NUL)
2323 line_arg = NULL;
2324 }
2325
2326 /* Don't define the function when skipping commands or when an error was
2327 * detected. */
2328 if (eap->skip || did_emsg)
2329 goto erret;
2330
2331 /*
2332 * If there are no errors, add the function
2333 */
2334 if (fudi.fd_dict == NULL)
2335 {
2336 v = find_var(name, &ht, FALSE);
2337 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2338 {
2339 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2340 name);
2341 goto erret;
2342 }
2343
2344 fp = find_func(name);
2345 if (fp != NULL)
2346 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002347 // Function can be replaced with "function!" and when sourcing the
2348 // same script again, but only once.
2349 if (!eap->forceit
2350 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2351 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002352 {
2353 emsg_funcname(e_funcexts, name);
2354 goto erret;
2355 }
2356 if (fp->uf_calls > 0)
2357 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002358 emsg_funcname(
2359 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002360 name);
2361 goto erret;
2362 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002363 if (fp->uf_refcount > 1)
2364 {
2365 /* This function is referenced somewhere, don't redefine it but
2366 * create a new one. */
2367 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002368 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002369 fp = NULL;
2370 overwrite = TRUE;
2371 }
2372 else
2373 {
2374 /* redefine existing function */
Bram Moolenaard23a8232018-02-10 18:45:26 +01002375 VIM_CLEAR(name);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002376 func_clear_items(fp);
2377#ifdef FEAT_PROFILE
2378 fp->uf_profiling = FALSE;
2379 fp->uf_prof_initialized = FALSE;
2380#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002381 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002382 }
2383 }
2384 else
2385 {
2386 char numbuf[20];
2387
2388 fp = NULL;
2389 if (fudi.fd_newkey == NULL && !eap->forceit)
2390 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002391 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 goto erret;
2393 }
2394 if (fudi.fd_di == NULL)
2395 {
2396 /* Can't add a function to a locked dictionary */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002397 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002398 goto erret;
2399 }
2400 /* Can't change an existing function if it is locked */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002401 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002402 goto erret;
2403
2404 /* Give the function a sequential number. Can only be used with a
2405 * Funcref! */
2406 vim_free(name);
2407 sprintf(numbuf, "%d", ++func_nr);
2408 name = vim_strsave((char_u *)numbuf);
2409 if (name == NULL)
2410 goto erret;
2411 }
2412
2413 if (fp == NULL)
2414 {
2415 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2416 {
2417 int slen, plen;
2418 char_u *scriptname;
2419
2420 /* Check that the autoload name matches the script name. */
2421 j = FAIL;
2422 if (sourcing_name != NULL)
2423 {
2424 scriptname = autoload_name(name);
2425 if (scriptname != NULL)
2426 {
2427 p = vim_strchr(scriptname, '/');
2428 plen = (int)STRLEN(p);
2429 slen = (int)STRLEN(sourcing_name);
2430 if (slen > plen && fnamecmp(p,
2431 sourcing_name + slen - plen) == 0)
2432 j = OK;
2433 vim_free(scriptname);
2434 }
2435 }
2436 if (j == FAIL)
2437 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002438 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002439 goto erret;
2440 }
2441 }
2442
Bram Moolenaar58016442016-07-31 18:30:22 +02002443 fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002444 if (fp == NULL)
2445 goto erret;
2446
2447 if (fudi.fd_dict != NULL)
2448 {
2449 if (fudi.fd_di == NULL)
2450 {
2451 /* add new dict entry */
2452 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2453 if (fudi.fd_di == NULL)
2454 {
2455 vim_free(fp);
2456 goto erret;
2457 }
2458 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2459 {
2460 vim_free(fudi.fd_di);
2461 vim_free(fp);
2462 goto erret;
2463 }
2464 }
2465 else
2466 /* overwrite existing dict entry */
2467 clear_tv(&fudi.fd_di->di_tv);
2468 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002469 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002470
2471 /* behave like "dict" was used */
2472 flags |= FC_DICT;
2473 }
2474
2475 /* insert the new function in the function list */
2476 STRCPY(fp->uf_name, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002477 if (overwrite)
2478 {
2479 hi = hash_find(&func_hashtab, name);
2480 hi->hi_key = UF2HIKEY(fp);
2481 }
2482 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002483 {
2484 vim_free(fp);
2485 goto erret;
2486 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002487 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002488 }
2489 fp->uf_args = newargs;
2490 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002491 if ((flags & FC_CLOSURE) != 0)
2492 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002493 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002494 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002495 }
2496 else
2497 fp->uf_scoped = NULL;
2498
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002499#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002500 if (prof_def_func())
2501 func_do_profile(fp);
2502#endif
2503 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02002504 if (sandbox)
2505 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002506 fp->uf_flags = flags;
2507 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002508 fp->uf_script_ctx = current_sctx;
2509 fp->uf_script_ctx.sc_lnum += sourcing_lnum - newlines.ga_len - 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 goto ret_free;
2511
2512erret:
2513 ga_clear_strings(&newargs);
2514errret_2:
2515 ga_clear_strings(&newlines);
2516ret_free:
2517 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002518 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519 vim_free(fudi.fd_newkey);
2520 vim_free(name);
2521 did_emsg |= saved_did_emsg;
2522 need_wait_return |= saved_wait_return;
2523}
2524
2525/*
2526 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2527 * Return 2 if "p" starts with "s:".
2528 * Return 0 otherwise.
2529 */
2530 int
2531eval_fname_script(char_u *p)
2532{
2533 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2534 * the standard library function. */
2535 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2536 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2537 return 5;
2538 if (p[0] == 's' && p[1] == ':')
2539 return 2;
2540 return 0;
2541}
2542
2543 int
2544translated_function_exists(char_u *name)
2545{
2546 if (builtin_function(name, -1))
2547 return find_internal_func(name) >= 0;
2548 return find_func(name) != NULL;
2549}
2550
2551/*
2552 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002553 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002554 */
2555 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002556function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002557{
2558 char_u *nm = name;
2559 char_u *p;
2560 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002561 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002563 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2564 if (no_deref)
2565 flag |= TFN_NO_DEREF;
2566 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002567 nm = skipwhite(nm);
2568
2569 /* Only accept "funcname", "funcname ", "funcname (..." and
2570 * "funcname(...", not "funcname!...". */
2571 if (p != NULL && (*nm == NUL || *nm == '('))
2572 n = translated_function_exists(p);
2573 vim_free(p);
2574 return n;
2575}
2576
Bram Moolenaar113e1072019-01-20 15:30:40 +01002577#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002578 char_u *
2579get_expanded_name(char_u *name, int check)
2580{
2581 char_u *nm = name;
2582 char_u *p;
2583
2584 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2585
2586 if (p != NULL && *nm == NUL)
2587 if (!check || translated_function_exists(p))
2588 return p;
2589
2590 vim_free(p);
2591 return NULL;
2592}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002593#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002594
2595#if defined(FEAT_PROFILE) || defined(PROTO)
2596/*
2597 * Start profiling function "fp".
2598 */
2599 static void
2600func_do_profile(ufunc_T *fp)
2601{
2602 int len = fp->uf_lines.ga_len;
2603
Bram Moolenaarad648092018-06-30 18:28:03 +02002604 if (!fp->uf_prof_initialized)
2605 {
2606 if (len == 0)
2607 len = 1; /* avoid getting error for allocating zero bytes */
2608 fp->uf_tm_count = 0;
2609 profile_zero(&fp->uf_tm_self);
2610 profile_zero(&fp->uf_tm_total);
2611 if (fp->uf_tml_count == NULL)
2612 fp->uf_tml_count = (int *)alloc_clear(
2613 (unsigned)(sizeof(int) * len));
2614 if (fp->uf_tml_total == NULL)
2615 fp->uf_tml_total = (proftime_T *)alloc_clear(
2616 (unsigned)(sizeof(proftime_T) * len));
2617 if (fp->uf_tml_self == NULL)
2618 fp->uf_tml_self = (proftime_T *)alloc_clear(
2619 (unsigned)(sizeof(proftime_T) * len));
2620 fp->uf_tml_idx = -1;
2621 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
2622 || fp->uf_tml_self == NULL)
2623 return; /* out of memory */
2624 fp->uf_prof_initialized = TRUE;
2625 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002626
2627 fp->uf_profiling = TRUE;
2628}
2629
2630/*
2631 * Dump the profiling results for all functions in file "fd".
2632 */
2633 void
2634func_dump_profile(FILE *fd)
2635{
2636 hashitem_T *hi;
2637 int todo;
2638 ufunc_T *fp;
2639 int i;
2640 ufunc_T **sorttab;
2641 int st_len = 0;
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +02002642 char_u *p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002643
2644 todo = (int)func_hashtab.ht_used;
2645 if (todo == 0)
2646 return; /* nothing to dump */
2647
2648 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
2649
2650 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
2651 {
2652 if (!HASHITEM_EMPTY(hi))
2653 {
2654 --todo;
2655 fp = HI2UF(hi);
Bram Moolenaarad648092018-06-30 18:28:03 +02002656 if (fp->uf_prof_initialized)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002657 {
2658 if (sorttab != NULL)
2659 sorttab[st_len++] = fp;
2660
2661 if (fp->uf_name[0] == K_SPECIAL)
2662 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
2663 else
2664 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
Bram Moolenaar4c7b08f2018-09-10 22:03:40 +02002665 p = home_replace_save(NULL,
2666 get_scriptname(fp->uf_script_ctx.sc_sid));
2667 if (p != NULL)
2668 {
2669 fprintf(fd, " Defined: %s line %ld\n",
2670 p, (long)fp->uf_script_ctx.sc_lnum);
2671 vim_free(p);
2672 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002673 if (fp->uf_tm_count == 1)
2674 fprintf(fd, "Called 1 time\n");
2675 else
2676 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
2677 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
2678 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
2679 fprintf(fd, "\n");
2680 fprintf(fd, "count total (s) self (s)\n");
2681
2682 for (i = 0; i < fp->uf_lines.ga_len; ++i)
2683 {
2684 if (FUNCLINE(fp, i) == NULL)
2685 continue;
2686 prof_func_line(fd, fp->uf_tml_count[i],
2687 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
2688 fprintf(fd, "%s\n", FUNCLINE(fp, i));
2689 }
2690 fprintf(fd, "\n");
2691 }
2692 }
2693 }
2694
2695 if (sorttab != NULL && st_len > 0)
2696 {
2697 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
2698 prof_total_cmp);
2699 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
2700 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
2701 prof_self_cmp);
2702 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
2703 }
2704
2705 vim_free(sorttab);
2706}
2707
2708 static void
2709prof_sort_list(
2710 FILE *fd,
2711 ufunc_T **sorttab,
2712 int st_len,
2713 char *title,
2714 int prefer_self) /* when equal print only self time */
2715{
2716 int i;
2717 ufunc_T *fp;
2718
2719 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
2720 fprintf(fd, "count total (s) self (s) function\n");
2721 for (i = 0; i < 20 && i < st_len; ++i)
2722 {
2723 fp = sorttab[i];
2724 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
2725 prefer_self);
2726 if (fp->uf_name[0] == K_SPECIAL)
2727 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
2728 else
2729 fprintf(fd, " %s()\n", fp->uf_name);
2730 }
2731 fprintf(fd, "\n");
2732}
2733
2734/*
2735 * Print the count and times for one function or function line.
2736 */
2737 static void
2738prof_func_line(
2739 FILE *fd,
2740 int count,
2741 proftime_T *total,
2742 proftime_T *self,
2743 int prefer_self) /* when equal print only self time */
2744{
2745 if (count > 0)
2746 {
2747 fprintf(fd, "%5d ", count);
2748 if (prefer_self && profile_equal(total, self))
2749 fprintf(fd, " ");
2750 else
2751 fprintf(fd, "%s ", profile_msg(total));
2752 if (!prefer_self && profile_equal(total, self))
2753 fprintf(fd, " ");
2754 else
2755 fprintf(fd, "%s ", profile_msg(self));
2756 }
2757 else
2758 fprintf(fd, " ");
2759}
2760
2761/*
2762 * Compare function for total time sorting.
2763 */
2764 static int
2765#ifdef __BORLANDC__
2766_RTLENTRYF
2767#endif
2768prof_total_cmp(const void *s1, const void *s2)
2769{
2770 ufunc_T *p1, *p2;
2771
2772 p1 = *(ufunc_T **)s1;
2773 p2 = *(ufunc_T **)s2;
2774 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
2775}
2776
2777/*
2778 * Compare function for self time sorting.
2779 */
2780 static int
2781#ifdef __BORLANDC__
2782_RTLENTRYF
2783#endif
2784prof_self_cmp(const void *s1, const void *s2)
2785{
2786 ufunc_T *p1, *p2;
2787
2788 p1 = *(ufunc_T **)s1;
2789 p2 = *(ufunc_T **)s2;
2790 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
2791}
2792
2793/*
2794 * Prepare profiling for entering a child or something else that is not
2795 * counted for the script/function itself.
2796 * Should always be called in pair with prof_child_exit().
2797 */
2798 void
2799prof_child_enter(
2800 proftime_T *tm) /* place to store waittime */
2801{
2802 funccall_T *fc = current_funccal;
2803
2804 if (fc != NULL && fc->func->uf_profiling)
2805 profile_start(&fc->prof_child);
2806 script_prof_save(tm);
2807}
2808
2809/*
2810 * Take care of time spent in a child.
2811 * Should always be called after prof_child_enter().
2812 */
2813 void
2814prof_child_exit(
2815 proftime_T *tm) /* where waittime was stored */
2816{
2817 funccall_T *fc = current_funccal;
2818
2819 if (fc != NULL && fc->func->uf_profiling)
2820 {
2821 profile_end(&fc->prof_child);
2822 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
2823 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
2824 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
2825 }
2826 script_prof_restore(tm);
2827}
2828
2829#endif /* FEAT_PROFILE */
2830
2831#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2832
2833/*
2834 * Function given to ExpandGeneric() to obtain the list of user defined
2835 * function names.
2836 */
2837 char_u *
2838get_user_func_name(expand_T *xp, int idx)
2839{
2840 static long_u done;
2841 static hashitem_T *hi;
2842 ufunc_T *fp;
2843
2844 if (idx == 0)
2845 {
2846 done = 0;
2847 hi = func_hashtab.ht_array;
2848 }
2849 if (done < func_hashtab.ht_used)
2850 {
2851 if (done++ > 0)
2852 ++hi;
2853 while (HASHITEM_EMPTY(hi))
2854 ++hi;
2855 fp = HI2UF(hi);
2856
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002857 if ((fp->uf_flags & FC_DICT)
2858 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2859 return (char_u *)""; /* don't show dict and lambda functions */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002860
2861 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
2862 return fp->uf_name; /* prevents overflow */
2863
2864 cat_func_name(IObuff, fp);
2865 if (xp->xp_context != EXPAND_USER_FUNC)
2866 {
2867 STRCAT(IObuff, "(");
2868 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2869 STRCAT(IObuff, ")");
2870 }
2871 return IObuff;
2872 }
2873 return NULL;
2874}
2875
2876#endif /* FEAT_CMDL_COMPL */
2877
2878/*
2879 * ":delfunction {name}"
2880 */
2881 void
2882ex_delfunction(exarg_T *eap)
2883{
2884 ufunc_T *fp = NULL;
2885 char_u *p;
2886 char_u *name;
2887 funcdict_T fudi;
2888
2889 p = eap->arg;
2890 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2891 vim_free(fudi.fd_newkey);
2892 if (name == NULL)
2893 {
2894 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002895 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896 return;
2897 }
2898 if (!ends_excmd(*skipwhite(p)))
2899 {
2900 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002901 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902 return;
2903 }
2904 eap->nextcmd = check_nextcmd(p);
2905 if (eap->nextcmd != NULL)
2906 *p = NUL;
2907
2908 if (!eap->skip)
2909 fp = find_func(name);
2910 vim_free(name);
2911
2912 if (!eap->skip)
2913 {
2914 if (fp == NULL)
2915 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02002916 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002917 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002918 return;
2919 }
2920 if (fp->uf_calls > 0)
2921 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002922 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002923 return;
2924 }
2925
2926 if (fudi.fd_dict != NULL)
2927 {
2928 /* Delete the dict item that refers to the function, it will
2929 * invoke func_unref() and possibly delete the function. */
2930 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2931 }
2932 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002933 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002934 /* A normal function (not a numbered function or lambda) has a
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002935 * refcount of 1 for the entry in the hashtable. When deleting
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002936 * it and the refcount is more than one, it should be kept.
Bram Moolenaarba209902016-08-24 22:06:38 +02002937 * A numbered function and lambda should be kept if the refcount is
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002938 * one or more. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002939 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002940 {
2941 /* Function is still referenced somewhere. Don't free it but
2942 * do remove it from the hashtable. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002943 if (func_remove(fp))
2944 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002945 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002946 }
2947 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002948 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002949 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002950 }
2951}
2952
2953/*
2954 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002955 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002956 */
2957 void
2958func_unref(char_u *name)
2959{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002960 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002961
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002962 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002963 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002964 fp = find_func(name);
2965 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002967#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002968 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002969#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002970 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002971 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002972 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002973 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002974 /* Only delete it when it's not being used. Otherwise it's done
2975 * when "uf_calls" becomes zero. */
2976 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002977 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02002978 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002979}
2980
2981/*
2982 * Unreference a Function: decrement the reference count and free it when it
2983 * becomes zero.
2984 */
2985 void
2986func_ptr_unref(ufunc_T *fp)
2987{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002988 if (fp != NULL && --fp->uf_refcount <= 0)
2989 {
2990 /* Only delete it when it's not being used. Otherwise it's done
2991 * when "uf_calls" becomes zero. */
2992 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002993 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994 }
2995}
2996
2997/*
2998 * Count a reference to a Function.
2999 */
3000 void
3001func_ref(char_u *name)
3002{
3003 ufunc_T *fp;
3004
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02003005 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003006 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003007 fp = find_func(name);
3008 if (fp != NULL)
3009 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003010 else if (isdigit(*name))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003011 /* Only give an error for a numbered function.
3012 * Fail silently, when named or lambda function isn't found. */
Bram Moolenaar95f09602016-11-10 20:01:45 +01003013 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003014}
3015
3016/*
3017 * Count a reference to a Function.
3018 */
3019 void
3020func_ptr_ref(ufunc_T *fp)
3021{
3022 if (fp != NULL)
3023 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024}
3025
3026/*
3027 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3028 * referenced from anywhere that is in use.
3029 */
3030 static int
3031can_free_funccal(funccall_T *fc, int copyID)
3032{
3033 return (fc->l_varlist.lv_copyID != copyID
3034 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003035 && fc->l_avars.dv_copyID != copyID
3036 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003037}
3038
3039/*
3040 * ":return [expr]"
3041 */
3042 void
3043ex_return(exarg_T *eap)
3044{
3045 char_u *arg = eap->arg;
3046 typval_T rettv;
3047 int returning = FALSE;
3048
3049 if (current_funccal == NULL)
3050 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003051 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003052 return;
3053 }
3054
3055 if (eap->skip)
3056 ++emsg_skip;
3057
3058 eap->nextcmd = NULL;
3059 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3060 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3061 {
3062 if (!eap->skip)
3063 returning = do_return(eap, FALSE, TRUE, &rettv);
3064 else
3065 clear_tv(&rettv);
3066 }
3067 /* It's safer to return also on error. */
3068 else if (!eap->skip)
3069 {
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003070 /* In return statement, cause_abort should be force_abort. */
3071 update_force_abort();
3072
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003073 /*
3074 * Return unless the expression evaluation has been cancelled due to an
3075 * aborting error, an interrupt, or an exception.
3076 */
3077 if (!aborting())
3078 returning = do_return(eap, FALSE, TRUE, NULL);
3079 }
3080
3081 /* When skipping or the return gets pending, advance to the next command
3082 * in this line (!returning). Otherwise, ignore the rest of the line.
3083 * Following lines will be ignored by get_func_line(). */
3084 if (returning)
3085 eap->nextcmd = NULL;
3086 else if (eap->nextcmd == NULL) /* no argument */
3087 eap->nextcmd = check_nextcmd(arg);
3088
3089 if (eap->skip)
3090 --emsg_skip;
3091}
3092
3093/*
3094 * ":1,25call func(arg1, arg2)" function call.
3095 */
3096 void
3097ex_call(exarg_T *eap)
3098{
3099 char_u *arg = eap->arg;
3100 char_u *startarg;
3101 char_u *name;
3102 char_u *tofree;
3103 int len;
3104 typval_T rettv;
3105 linenr_T lnum;
3106 int doesrange;
3107 int failed = FALSE;
3108 funcdict_T fudi;
3109 partial_T *partial = NULL;
3110
3111 if (eap->skip)
3112 {
3113 /* trans_function_name() doesn't work well when skipping, use eval0()
3114 * instead to skip to any following command, e.g. for:
3115 * :if 0 | call dict.foo().bar() | endif */
3116 ++emsg_skip;
3117 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3118 clear_tv(&rettv);
3119 --emsg_skip;
3120 return;
3121 }
3122
3123 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3124 if (fudi.fd_newkey != NULL)
3125 {
3126 /* Still need to give an error message for missing key. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003127 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003128 vim_free(fudi.fd_newkey);
3129 }
3130 if (tofree == NULL)
3131 return;
3132
3133 /* Increase refcount on dictionary, it could get deleted when evaluating
3134 * the arguments. */
3135 if (fudi.fd_dict != NULL)
3136 ++fudi.fd_dict->dv_refcount;
3137
3138 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3139 * contents. For VAR_PARTIAL get its partial, unless we already have one
3140 * from trans_function_name(). */
3141 len = (int)STRLEN(tofree);
3142 name = deref_func_name(tofree, &len,
3143 partial != NULL ? NULL : &partial, FALSE);
3144
3145 /* Skip white space to allow ":call func ()". Not good, but required for
3146 * backward compatibility. */
3147 startarg = skipwhite(arg);
3148 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
3149
3150 if (*startarg != '(')
3151 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003152 semsg(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003153 goto end;
3154 }
3155
3156 /*
3157 * When skipping, evaluate the function once, to find the end of the
3158 * arguments.
3159 * When the function takes a range, this is discovered after the first
3160 * call, and the loop is broken.
3161 */
3162 if (eap->skip)
3163 {
3164 ++emsg_skip;
3165 lnum = eap->line2; /* do it once, also with an invalid range */
3166 }
3167 else
3168 lnum = eap->line1;
3169 for ( ; lnum <= eap->line2; ++lnum)
3170 {
3171 if (!eap->skip && eap->addr_count > 0)
3172 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003173 if (lnum > curbuf->b_ml.ml_line_count)
3174 {
3175 // If the function deleted lines or switched to another buffer
3176 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003177 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003178 break;
3179 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 curwin->w_cursor.lnum = lnum;
3181 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003182 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003183 }
3184 arg = startarg;
3185 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
3186 eap->line1, eap->line2, &doesrange,
3187 !eap->skip, partial, fudi.fd_dict) == FAIL)
3188 {
3189 failed = TRUE;
3190 break;
3191 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003192 if (has_watchexpr())
3193 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003194
3195 /* Handle a function returning a Funcref, Dictionary or List. */
3196 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3197 {
3198 failed = TRUE;
3199 break;
3200 }
3201
3202 clear_tv(&rettv);
3203 if (doesrange || eap->skip)
3204 break;
3205
3206 /* Stop when immediately aborting on error, or when an interrupt
3207 * occurred or an exception was thrown but not caught.
3208 * get_func_tv() returned OK, so that the check for trailing
3209 * characters below is executed. */
3210 if (aborting())
3211 break;
3212 }
3213 if (eap->skip)
3214 --emsg_skip;
3215
3216 if (!failed)
3217 {
3218 /* Check for trailing illegal characters and a following command. */
3219 if (!ends_excmd(*arg))
3220 {
3221 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003222 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003223 }
3224 else
3225 eap->nextcmd = check_nextcmd(arg);
3226 }
3227
3228end:
3229 dict_unref(fudi.fd_dict);
3230 vim_free(tofree);
3231}
3232
3233/*
3234 * Return from a function. Possibly makes the return pending. Also called
3235 * for a pending return at the ":endtry" or after returning from an extra
3236 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3237 * when called due to a ":return" command. "rettv" may point to a typval_T
3238 * with the return rettv. Returns TRUE when the return can be carried out,
3239 * FALSE when the return gets pending.
3240 */
3241 int
3242do_return(
3243 exarg_T *eap,
3244 int reanimate,
3245 int is_cmd,
3246 void *rettv)
3247{
3248 int idx;
3249 struct condstack *cstack = eap->cstack;
3250
3251 if (reanimate)
3252 /* Undo the return. */
3253 current_funccal->returned = FALSE;
3254
3255 /*
3256 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3257 * not in its finally clause (which then is to be executed next) is found.
3258 * In this case, make the ":return" pending for execution at the ":endtry".
3259 * Otherwise, return normally.
3260 */
3261 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3262 if (idx >= 0)
3263 {
3264 cstack->cs_pending[idx] = CSTP_RETURN;
3265
3266 if (!is_cmd && !reanimate)
3267 /* A pending return again gets pending. "rettv" points to an
3268 * allocated variable with the rettv of the original ":return"'s
3269 * argument if present or is NULL else. */
3270 cstack->cs_rettv[idx] = rettv;
3271 else
3272 {
3273 /* When undoing a return in order to make it pending, get the stored
3274 * return rettv. */
3275 if (reanimate)
3276 rettv = current_funccal->rettv;
3277
3278 if (rettv != NULL)
3279 {
3280 /* Store the value of the pending return. */
3281 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3282 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3283 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003284 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003285 }
3286 else
3287 cstack->cs_rettv[idx] = NULL;
3288
3289 if (reanimate)
3290 {
3291 /* The pending return value could be overwritten by a ":return"
3292 * without argument in a finally clause; reset the default
3293 * return value. */
3294 current_funccal->rettv->v_type = VAR_NUMBER;
3295 current_funccal->rettv->vval.v_number = 0;
3296 }
3297 }
3298 report_make_pending(CSTP_RETURN, rettv);
3299 }
3300 else
3301 {
3302 current_funccal->returned = TRUE;
3303
3304 /* If the return is carried out now, store the return value. For
3305 * a return immediately after reanimation, the value is already
3306 * there. */
3307 if (!reanimate && rettv != NULL)
3308 {
3309 clear_tv(current_funccal->rettv);
3310 *current_funccal->rettv = *(typval_T *)rettv;
3311 if (!is_cmd)
3312 vim_free(rettv);
3313 }
3314 }
3315
3316 return idx < 0;
3317}
3318
3319/*
3320 * Free the variable with a pending return value.
3321 */
3322 void
3323discard_pending_return(void *rettv)
3324{
3325 free_tv((typval_T *)rettv);
3326}
3327
3328/*
3329 * Generate a return command for producing the value of "rettv". The result
3330 * is an allocated string. Used by report_pending() for verbose messages.
3331 */
3332 char_u *
3333get_return_cmd(void *rettv)
3334{
3335 char_u *s = NULL;
3336 char_u *tofree = NULL;
3337 char_u numbuf[NUMBUFLEN];
3338
3339 if (rettv != NULL)
3340 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3341 if (s == NULL)
3342 s = (char_u *)"";
3343
3344 STRCPY(IObuff, ":return ");
3345 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3346 if (STRLEN(s) + 8 >= IOSIZE)
3347 STRCPY(IObuff + IOSIZE - 4, "...");
3348 vim_free(tofree);
3349 return vim_strsave(IObuff);
3350}
3351
3352/*
3353 * Get next function line.
3354 * Called by do_cmdline() to get the next line.
3355 * Returns allocated string, or NULL for end of function.
3356 */
3357 char_u *
3358get_func_line(
3359 int c UNUSED,
3360 void *cookie,
3361 int indent UNUSED)
3362{
3363 funccall_T *fcp = (funccall_T *)cookie;
3364 ufunc_T *fp = fcp->func;
3365 char_u *retval;
3366 garray_T *gap; /* growarray with function lines */
3367
3368 /* If breakpoints have been added/deleted need to check for it. */
3369 if (fcp->dbg_tick != debug_tick)
3370 {
3371 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3372 sourcing_lnum);
3373 fcp->dbg_tick = debug_tick;
3374 }
3375#ifdef FEAT_PROFILE
3376 if (do_profiling == PROF_YES)
3377 func_line_end(cookie);
3378#endif
3379
3380 gap = &fp->uf_lines;
3381 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3382 || fcp->returned)
3383 retval = NULL;
3384 else
3385 {
3386 /* Skip NULL lines (continuation lines). */
3387 while (fcp->linenr < gap->ga_len
3388 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3389 ++fcp->linenr;
3390 if (fcp->linenr >= gap->ga_len)
3391 retval = NULL;
3392 else
3393 {
3394 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
3395 sourcing_lnum = fcp->linenr;
3396#ifdef FEAT_PROFILE
3397 if (do_profiling == PROF_YES)
3398 func_line_start(cookie);
3399#endif
3400 }
3401 }
3402
3403 /* Did we encounter a breakpoint? */
3404 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
3405 {
3406 dbg_breakpoint(fp->uf_name, sourcing_lnum);
3407 /* Find next breakpoint. */
3408 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3409 sourcing_lnum);
3410 fcp->dbg_tick = debug_tick;
3411 }
3412
3413 return retval;
3414}
3415
3416#if defined(FEAT_PROFILE) || defined(PROTO)
3417/*
3418 * Called when starting to read a function line.
3419 * "sourcing_lnum" must be correct!
3420 * When skipping lines it may not actually be executed, but we won't find out
3421 * until later and we need to store the time now.
3422 */
3423 void
3424func_line_start(void *cookie)
3425{
3426 funccall_T *fcp = (funccall_T *)cookie;
3427 ufunc_T *fp = fcp->func;
3428
3429 if (fp->uf_profiling && sourcing_lnum >= 1
3430 && sourcing_lnum <= fp->uf_lines.ga_len)
3431 {
3432 fp->uf_tml_idx = sourcing_lnum - 1;
3433 /* Skip continuation lines. */
3434 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
3435 --fp->uf_tml_idx;
3436 fp->uf_tml_execed = FALSE;
3437 profile_start(&fp->uf_tml_start);
3438 profile_zero(&fp->uf_tml_children);
3439 profile_get_wait(&fp->uf_tml_wait);
3440 }
3441}
3442
3443/*
3444 * Called when actually executing a function line.
3445 */
3446 void
3447func_line_exec(void *cookie)
3448{
3449 funccall_T *fcp = (funccall_T *)cookie;
3450 ufunc_T *fp = fcp->func;
3451
3452 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
3453 fp->uf_tml_execed = TRUE;
3454}
3455
3456/*
3457 * Called when done with a function line.
3458 */
3459 void
3460func_line_end(void *cookie)
3461{
3462 funccall_T *fcp = (funccall_T *)cookie;
3463 ufunc_T *fp = fcp->func;
3464
3465 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
3466 {
3467 if (fp->uf_tml_execed)
3468 {
3469 ++fp->uf_tml_count[fp->uf_tml_idx];
3470 profile_end(&fp->uf_tml_start);
3471 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
3472 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
3473 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
3474 &fp->uf_tml_children);
3475 }
3476 fp->uf_tml_idx = -1;
3477 }
3478}
3479#endif
3480
3481/*
3482 * Return TRUE if the currently active function should be ended, because a
3483 * return was encountered or an error occurred. Used inside a ":while".
3484 */
3485 int
3486func_has_ended(void *cookie)
3487{
3488 funccall_T *fcp = (funccall_T *)cookie;
3489
3490 /* Ignore the "abort" flag if the abortion behavior has been changed due to
3491 * an error inside a try conditional. */
3492 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3493 || fcp->returned);
3494}
3495
3496/*
3497 * return TRUE if cookie indicates a function which "abort"s on errors.
3498 */
3499 int
3500func_has_abort(
3501 void *cookie)
3502{
3503 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3504}
3505
3506
3507/*
3508 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3509 * Don't do this when "Func" is already a partial that was bound
3510 * explicitly (pt_auto is FALSE).
3511 * Changes "rettv" in-place.
3512 * Returns the updated "selfdict_in".
3513 */
3514 dict_T *
3515make_partial(dict_T *selfdict_in, typval_T *rettv)
3516{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003517 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003518 char_u *tofree = NULL;
3519 ufunc_T *fp;
3520 char_u fname_buf[FLEN_FIXED + 1];
3521 int error;
3522 dict_T *selfdict = selfdict_in;
3523
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003524 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3525 fp = rettv->vval.v_partial->pt_func;
3526 else
3527 {
3528 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3529 : rettv->vval.v_partial->pt_name;
3530 /* Translate "s:func" to the stored function name. */
3531 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3532 fp = find_func(fname);
3533 vim_free(tofree);
3534 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003535
3536 if (fp != NULL && (fp->uf_flags & FC_DICT))
3537 {
3538 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
3539
3540 if (pt != NULL)
3541 {
3542 pt->pt_refcount = 1;
3543 pt->pt_dict = selfdict;
3544 pt->pt_auto = TRUE;
3545 selfdict = NULL;
3546 if (rettv->v_type == VAR_FUNC)
3547 {
3548 /* Just a function: Take over the function name and use
3549 * selfdict. */
3550 pt->pt_name = rettv->vval.v_string;
3551 }
3552 else
3553 {
3554 partial_T *ret_pt = rettv->vval.v_partial;
3555 int i;
3556
3557 /* Partial: copy the function name, use selfdict and copy
3558 * args. Can't take over name or args, the partial might
3559 * be referenced elsewhere. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003560 if (ret_pt->pt_name != NULL)
3561 {
3562 pt->pt_name = vim_strsave(ret_pt->pt_name);
3563 func_ref(pt->pt_name);
3564 }
3565 else
3566 {
3567 pt->pt_func = ret_pt->pt_func;
3568 func_ptr_ref(pt->pt_func);
3569 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 if (ret_pt->pt_argc > 0)
3571 {
3572 pt->pt_argv = (typval_T *)alloc(
3573 sizeof(typval_T) * ret_pt->pt_argc);
3574 if (pt->pt_argv == NULL)
3575 /* out of memory: drop the arguments */
3576 pt->pt_argc = 0;
3577 else
3578 {
3579 pt->pt_argc = ret_pt->pt_argc;
3580 for (i = 0; i < pt->pt_argc; i++)
3581 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3582 }
3583 }
3584 partial_unref(ret_pt);
3585 }
3586 rettv->v_type = VAR_PARTIAL;
3587 rettv->vval.v_partial = pt;
3588 }
3589 }
3590 return selfdict;
3591}
3592
3593/*
3594 * Return the name of the executed function.
3595 */
3596 char_u *
3597func_name(void *cookie)
3598{
3599 return ((funccall_T *)cookie)->func->uf_name;
3600}
3601
3602/*
3603 * Return the address holding the next breakpoint line for a funccall cookie.
3604 */
3605 linenr_T *
3606func_breakpoint(void *cookie)
3607{
3608 return &((funccall_T *)cookie)->breakpoint;
3609}
3610
3611/*
3612 * Return the address holding the debug tick for a funccall cookie.
3613 */
3614 int *
3615func_dbg_tick(void *cookie)
3616{
3617 return &((funccall_T *)cookie)->dbg_tick;
3618}
3619
3620/*
3621 * Return the nesting level for a funccall cookie.
3622 */
3623 int
3624func_level(void *cookie)
3625{
3626 return ((funccall_T *)cookie)->level;
3627}
3628
3629/*
3630 * Return TRUE when a function was ended by a ":return" command.
3631 */
3632 int
3633current_func_returned(void)
3634{
3635 return current_funccal->returned;
3636}
3637
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003638 int
3639free_unref_funccal(int copyID, int testing)
3640{
3641 int did_free = FALSE;
3642 int did_free_funccal = FALSE;
3643 funccall_T *fc, **pfc;
3644
3645 for (pfc = &previous_funccal; *pfc != NULL; )
3646 {
3647 if (can_free_funccal(*pfc, copyID))
3648 {
3649 fc = *pfc;
3650 *pfc = fc->caller;
3651 free_funccal(fc, TRUE);
3652 did_free = TRUE;
3653 did_free_funccal = TRUE;
3654 }
3655 else
3656 pfc = &(*pfc)->caller;
3657 }
3658 if (did_free_funccal)
3659 /* When a funccal was freed some more items might be garbage
3660 * collected, so run again. */
3661 (void)garbage_collect(testing);
3662
3663 return did_free;
3664}
3665
3666/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003667 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003668 */
3669 static funccall_T *
3670get_funccal(void)
3671{
3672 int i;
3673 funccall_T *funccal;
3674 funccall_T *temp_funccal;
3675
3676 funccal = current_funccal;
3677 if (debug_backtrace_level > 0)
3678 {
3679 for (i = 0; i < debug_backtrace_level; i++)
3680 {
3681 temp_funccal = funccal->caller;
3682 if (temp_funccal)
3683 funccal = temp_funccal;
3684 else
3685 /* backtrace level overflow. reset to max */
3686 debug_backtrace_level = i;
3687 }
3688 }
3689 return funccal;
3690}
3691
3692/*
3693 * Return the hashtable used for local variables in the current funccal.
3694 * Return NULL if there is no current funccal.
3695 */
3696 hashtab_T *
3697get_funccal_local_ht()
3698{
3699 if (current_funccal == NULL)
3700 return NULL;
3701 return &get_funccal()->l_vars.dv_hashtab;
3702}
3703
3704/*
3705 * Return the l: scope variable.
3706 * Return NULL if there is no current funccal.
3707 */
3708 dictitem_T *
3709get_funccal_local_var()
3710{
3711 if (current_funccal == NULL)
3712 return NULL;
3713 return &get_funccal()->l_vars_var;
3714}
3715
3716/*
3717 * Return the hashtable used for argument in the current funccal.
3718 * Return NULL if there is no current funccal.
3719 */
3720 hashtab_T *
3721get_funccal_args_ht()
3722{
3723 if (current_funccal == NULL)
3724 return NULL;
3725 return &get_funccal()->l_avars.dv_hashtab;
3726}
3727
3728/*
3729 * Return the a: scope variable.
3730 * Return NULL if there is no current funccal.
3731 */
3732 dictitem_T *
3733get_funccal_args_var()
3734{
3735 if (current_funccal == NULL)
3736 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01003737 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003738}
3739
3740/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003741 * List function variables, if there is a function.
3742 */
3743 void
3744list_func_vars(int *first)
3745{
3746 if (current_funccal != NULL)
3747 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01003748 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003749}
3750
3751/*
3752 * If "ht" is the hashtable for local variables in the current funccal, return
3753 * the dict that contains it.
3754 * Otherwise return NULL.
3755 */
3756 dict_T *
3757get_current_funccal_dict(hashtab_T *ht)
3758{
3759 if (current_funccal != NULL
3760 && ht == &current_funccal->l_vars.dv_hashtab)
3761 return &current_funccal->l_vars;
3762 return NULL;
3763}
3764
3765/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003766 * Search hashitem in parent scope.
3767 */
3768 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003769find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003770{
3771 funccall_T *old_current_funccal = current_funccal;
3772 hashtab_T *ht;
3773 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003774 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003775
3776 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3777 return NULL;
3778
3779 /* Search in parent scope which is possible to reference from lambda */
3780 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003781 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003782 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003783 ht = find_var_ht(name, &varname);
3784 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003785 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003786 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003787 if (!HASHITEM_EMPTY(hi))
3788 {
3789 *pht = ht;
3790 break;
3791 }
3792 }
3793 if (current_funccal == current_funccal->func->uf_scoped)
3794 break;
3795 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003796 }
3797 current_funccal = old_current_funccal;
3798
3799 return hi;
3800}
3801
3802/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003803 * Search variable in parent scope.
3804 */
3805 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003806find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003807{
3808 dictitem_T *v = NULL;
3809 funccall_T *old_current_funccal = current_funccal;
3810 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003811 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003812
3813 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3814 return NULL;
3815
3816 /* Search in parent scope which is possible to reference from lambda */
3817 current_funccal = current_funccal->func->uf_scoped;
3818 while (current_funccal)
3819 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003820 ht = find_var_ht(name, &varname);
3821 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003822 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003823 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003824 if (v != NULL)
3825 break;
3826 }
3827 if (current_funccal == current_funccal->func->uf_scoped)
3828 break;
3829 current_funccal = current_funccal->func->uf_scoped;
3830 }
3831 current_funccal = old_current_funccal;
3832
3833 return v;
3834}
3835
3836/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003837 * Set "copyID + 1" in previous_funccal and callers.
3838 */
3839 int
3840set_ref_in_previous_funccal(int copyID)
3841{
3842 int abort = FALSE;
3843 funccall_T *fc;
3844
3845 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
3846 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003847 fc->fc_copyID = copyID + 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003848 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
3849 NULL);
3850 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
3851 NULL);
3852 }
3853 return abort;
3854}
3855
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003856 static int
3857set_ref_in_funccal(funccall_T *fc, int copyID)
3858{
3859 int abort = FALSE;
3860
3861 if (fc->fc_copyID != copyID)
3862 {
3863 fc->fc_copyID = copyID;
3864 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
3865 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
3866 abort = abort || set_ref_in_func(NULL, fc->func, copyID);
3867 }
3868 return abort;
3869}
3870
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003871/*
3872 * Set "copyID" in all local vars and arguments in the call stack.
3873 */
3874 int
3875set_ref_in_call_stack(int copyID)
3876{
3877 int abort = FALSE;
3878 funccall_T *fc;
3879
3880 for (fc = current_funccal; fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003881 abort = abort || set_ref_in_funccal(fc, copyID);
3882 return abort;
3883}
3884
3885/*
3886 * Set "copyID" in all functions available by name.
3887 */
3888 int
3889set_ref_in_functions(int copyID)
3890{
3891 int todo;
3892 hashitem_T *hi = NULL;
3893 int abort = FALSE;
3894 ufunc_T *fp;
3895
3896 todo = (int)func_hashtab.ht_used;
3897 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003898 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003899 if (!HASHITEM_EMPTY(hi))
3900 {
3901 --todo;
3902 fp = HI2UF(hi);
3903 if (!func_name_refcount(fp->uf_name))
3904 abort = abort || set_ref_in_func(NULL, fp, copyID);
3905 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003906 }
3907 return abort;
3908}
3909
3910/*
3911 * Set "copyID" in all function arguments.
3912 */
3913 int
3914set_ref_in_func_args(int copyID)
3915{
3916 int i;
3917 int abort = FALSE;
3918
3919 for (i = 0; i < funcargs.ga_len; ++i)
3920 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3921 copyID, NULL, NULL);
3922 return abort;
3923}
3924
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003925/*
3926 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003927 * Returns TRUE if setting references failed somehow.
3928 */
3929 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003930set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003931{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003932 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003933 funccall_T *fc;
3934 int error = ERROR_NONE;
3935 char_u fname_buf[FLEN_FIXED + 1];
3936 char_u *tofree = NULL;
3937 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003938 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003939
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003940 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003941 return FALSE;
3942
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003943 if (fp_in == NULL)
3944 {
3945 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3946 fp = find_func(fname);
3947 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003948 if (fp != NULL)
3949 {
3950 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003951 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003952 }
3953 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003954 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003955}
3956
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003957#endif /* FEAT_EVAL */