blob: cfc52befe4aacd4ae3b407ce7eda2203cf043871 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaara9b579f2016-07-17 18:29:19 +020025
Bram Moolenaara9b579f2016-07-17 18:29:19 +020026#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028/*
29 * All user-defined functions are found in this hashtable.
30 */
31static hashtab_T func_hashtab;
32
33/* Used by get_func_tv() */
34static garray_T funcargs = GA_EMPTY;
35
Bram Moolenaar209b8e32019-03-14 13:43:24 +010036// pointer to funccal for currently active function
37static funccall_T *current_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020038
Bram Moolenaar209b8e32019-03-14 13:43:24 +010039// Pointer to list of previously used funccal, still around because some
40// item in it is still being used.
41static funccall_T *previous_funccal = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020042
43static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
44static char *e_funcdict = N_("E717: Dictionary entry already exists");
45static char *e_funcref = N_("E718: Funcref required");
46static char *e_nofunc = N_("E130: Unknown function: %s");
47
Bram Moolenaarbc7ce672016-08-01 22:49:22 +020048static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020049
50 void
51func_init()
52{
53 hash_init(&func_hashtab);
54}
55
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020056/*
Bram Moolenaar660a10a2019-07-14 15:48:38 +020057 * Return the function hash table
58 */
59 hashtab_T *
60func_tbl_get(void)
61{
62 return &func_hashtab;
63}
64
65/*
Bram Moolenaar4f0383b2016-07-19 22:43:11 +020066 * Get function arguments.
67 */
Bram Moolenaara9b579f2016-07-17 18:29:19 +020068 static int
69get_function_args(
70 char_u **argp,
71 char_u endchar,
72 garray_T *newargs,
73 int *varargs,
Bram Moolenaar42ae78c2019-05-09 21:08:58 +020074 garray_T *default_args,
Bram Moolenaara9b579f2016-07-17 18:29:19 +020075 int skip)
76{
77 int mustend = FALSE;
78 char_u *arg = *argp;
79 char_u *p = arg;
80 int c;
81 int i;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +020082 int any_default = FALSE;
83 char_u *expr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +020084
85 if (newargs != NULL)
86 ga_init2(newargs, (int)sizeof(char_u *), 3);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +020087 if (default_args != NULL)
88 ga_init2(default_args, (int)sizeof(char_u *), 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +020089
90 if (varargs != NULL)
91 *varargs = FALSE;
92
93 /*
94 * Isolate the arguments: "arg1, arg2, ...)"
95 */
96 while (*p != endchar)
97 {
98 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
99 {
100 if (varargs != NULL)
101 *varargs = TRUE;
102 p += 3;
103 mustend = TRUE;
104 }
105 else
106 {
107 arg = p;
108 while (ASCII_ISALNUM(*p) || *p == '_')
109 ++p;
110 if (arg == p || isdigit(*arg)
111 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
112 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
113 {
114 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100115 semsg(_("E125: Illegal argument: %s"), arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200116 break;
117 }
118 if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200119 goto err_ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200120 if (newargs != NULL)
121 {
122 c = *p;
123 *p = NUL;
124 arg = vim_strsave(arg);
125 if (arg == NULL)
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200126 {
127 *p = c;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200128 goto err_ret;
Bram Moolenaar19df5cc2016-07-20 22:11:06 +0200129 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200130
131 /* Check for duplicate argument name. */
132 for (i = 0; i < newargs->ga_len; ++i)
133 if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
134 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100135 semsg(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200136 vim_free(arg);
137 goto err_ret;
138 }
139 ((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
140 newargs->ga_len++;
141
142 *p = c;
143 }
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200144 if (*skipwhite(p) == '=' && default_args != NULL)
145 {
146 typval_T rettv;
147
148 any_default = TRUE;
149 p = skipwhite(p) + 1;
150 p = skipwhite(p);
151 expr = p;
152 if (eval1(&p, &rettv, FALSE) != FAIL)
153 {
154 if (ga_grow(default_args, 1) == FAIL)
155 goto err_ret;
156
157 // trim trailing whitespace
158 while (p > expr && VIM_ISWHITE(p[-1]))
159 p--;
160 c = *p;
161 *p = NUL;
162 expr = vim_strsave(expr);
163 if (expr == NULL)
164 {
165 *p = c;
166 goto err_ret;
167 }
168 ((char_u **)(default_args->ga_data))
169 [default_args->ga_len] = expr;
170 default_args->ga_len++;
171 *p = c;
172 }
173 else
174 mustend = TRUE;
175 }
176 else if (any_default)
177 {
178 emsg(_("E989: Non-default argument follows default argument"));
179 mustend = TRUE;
180 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200181 if (*p == ',')
182 ++p;
183 else
184 mustend = TRUE;
185 }
186 p = skipwhite(p);
187 if (mustend && *p != endchar)
188 {
189 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100190 semsg(_(e_invarg2), *argp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200191 break;
192 }
193 }
Bram Moolenaar4f0383b2016-07-19 22:43:11 +0200194 if (*p != endchar)
195 goto err_ret;
196 ++p; /* skip "endchar" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200197
198 *argp = p;
199 return OK;
200
201err_ret:
202 if (newargs != NULL)
203 ga_clear_strings(newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200204 if (default_args != NULL)
205 ga_clear_strings(default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200206 return FAIL;
207}
208
209/*
Bram Moolenaar58016442016-07-31 18:30:22 +0200210 * Register function "fp" as using "current_funccal" as its scope.
211 */
212 static int
213register_closure(ufunc_T *fp)
214{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200215 if (fp->uf_scoped == current_funccal)
216 /* no change */
217 return OK;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200218 funccal_unref(fp->uf_scoped, fp, FALSE);
Bram Moolenaar58016442016-07-31 18:30:22 +0200219 fp->uf_scoped = current_funccal;
220 current_funccal->fc_refcount++;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200221
Bram Moolenaar58016442016-07-31 18:30:22 +0200222 if (ga_grow(&current_funccal->fc_funcs, 1) == FAIL)
223 return FAIL;
224 ((ufunc_T **)current_funccal->fc_funcs.ga_data)
225 [current_funccal->fc_funcs.ga_len++] = fp;
Bram Moolenaar58016442016-07-31 18:30:22 +0200226 return OK;
227}
228
229/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200230 * Parse a lambda expression and get a Funcref from "*arg".
231 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
232 */
233 int
234get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
235{
236 garray_T newargs;
237 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200238 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200239 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100240 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200241 int varargs;
242 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200243 char_u *start = skipwhite(*arg + 1);
244 char_u *s, *e;
245 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200246 int *old_eval_lavars = eval_lavars_used;
247 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200248
249 ga_init(&newargs);
250 ga_init(&newlines);
251
252 /* First, check if this is a lambda expression. "->" must exist. */
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200253 ret = get_function_args(&start, '-', NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200254 if (ret == FAIL || *start != '>')
255 return NOTDONE;
256
257 /* Parse the arguments again. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200258 if (evaluate)
259 pnewargs = &newargs;
260 else
261 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200262 *arg = skipwhite(*arg + 1);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200263 ret = get_function_args(arg, '-', pnewargs, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200264 if (ret == FAIL || **arg != '>')
265 goto errret;
266
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +0200267 /* Set up a flag for checking local variables and arguments. */
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200268 if (evaluate)
269 eval_lavars_used = &eval_lavars;
270
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200271 /* Get the start and the end of the expression. */
272 *arg = skipwhite(*arg + 1);
273 s = *arg;
274 ret = skip_expr(arg);
275 if (ret == FAIL)
276 goto errret;
277 e = *arg;
278 *arg = skipwhite(*arg);
279 if (**arg != '}')
280 goto errret;
281 ++*arg;
282
283 if (evaluate)
284 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200285 int len, flags = 0;
286 char_u *p;
287 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200288
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200289 sprintf((char*)name, "<lambda>%d", ++lambda_no);
290
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200291 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 if (fp == NULL)
293 goto errret;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200294 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200295 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200296 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298 ga_init2(&newlines, (int)sizeof(char_u *), 1);
299 if (ga_grow(&newlines, 1) == FAIL)
300 goto errret;
301
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200302 /* Add "return " before the expression. */
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200303 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200304 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200305 if (p == NULL)
306 goto errret;
307 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
308 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200309 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200310
311 fp->uf_refcount = 1;
312 STRCPY(fp->uf_name, name);
313 hash_add(&func_hashtab, UF2HIKEY(fp));
314 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200315 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200316 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200317 if (current_funccal != NULL && eval_lavars)
318 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200319 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200320 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200321 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200322 }
323 else
324 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200325
326#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200327 if (prof_def_func())
328 func_do_profile(fp);
329#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200330 if (sandbox)
331 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200332 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200333 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200334 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200335 fp->uf_script_ctx = current_sctx;
336 fp->uf_script_ctx.sc_lnum += sourcing_lnum - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200337
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200338 pt->pt_func = fp;
339 pt->pt_refcount = 1;
340 rettv->vval.v_partial = pt;
341 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200342 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200343
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200344 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200345 return OK;
346
347errret:
348 ga_clear_strings(&newargs);
349 ga_clear_strings(&newlines);
350 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100351 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200352 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200353 return FAIL;
354}
355
356/*
357 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
358 * name it contains, otherwise return "name".
359 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
360 * "partialp".
361 */
362 char_u *
363deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
364{
365 dictitem_T *v;
366 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200367 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200368
369 if (partialp != NULL)
370 *partialp = NULL;
371
372 cc = name[*lenp];
373 name[*lenp] = NUL;
374 v = find_var(name, NULL, no_autoload);
375 name[*lenp] = cc;
376 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
377 {
378 if (v->di_tv.vval.v_string == NULL)
379 {
380 *lenp = 0;
381 return (char_u *)""; /* just in case */
382 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200383 s = v->di_tv.vval.v_string;
384 *lenp = (int)STRLEN(s);
385 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200386 }
387
388 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
389 {
390 partial_T *pt = v->di_tv.vval.v_partial;
391
392 if (pt == NULL)
393 {
394 *lenp = 0;
395 return (char_u *)""; /* just in case */
396 }
397 if (partialp != NULL)
398 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200399 s = partial_name(pt);
400 *lenp = (int)STRLEN(s);
401 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402 }
403
404 return name;
405}
406
407/*
408 * Give an error message with a function name. Handle <SNR> things.
409 * "ermsg" is to be passed without translation, use N_() instead of _().
410 */
411 static void
412emsg_funcname(char *ermsg, char_u *name)
413{
414 char_u *p;
415
416 if (*name == K_SPECIAL)
417 p = concat_str((char_u *)"<SNR>", name + 3);
418 else
419 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100420 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421 if (p != name)
422 vim_free(p);
423}
424
425/*
426 * Allocate a variable for the result of a function.
427 * Return OK or FAIL.
428 */
429 int
430get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200431 char_u *name, // name of the function
432 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200433 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200434 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200435 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200436{
437 char_u *argp;
438 int ret = OK;
439 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
440 int argcount = 0; /* number of arguments found */
441
442 /*
443 * Get the arguments.
444 */
445 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200446 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
447 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200448 {
449 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
450 if (*argp == ')' || *argp == ',' || *argp == NUL)
451 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200452 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200453 {
454 ret = FAIL;
455 break;
456 }
457 ++argcount;
458 if (*argp != ',')
459 break;
460 }
461 if (*argp == ')')
462 ++argp;
463 else
464 ret = FAIL;
465
466 if (ret == OK)
467 {
468 int i = 0;
469
470 if (get_vim_var_nr(VV_TESTING))
471 {
472 /* Prepare for calling test_garbagecollect_now(), need to know
473 * what variables are used on the call stack. */
474 if (funcargs.ga_itemsize == 0)
475 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
476 for (i = 0; i < argcount; ++i)
477 if (ga_grow(&funcargs, 1) == OK)
478 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
479 &argvars[i];
480 }
481
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200482 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483
484 funcargs.ga_len -= i;
485 }
486 else if (!aborting())
487 {
488 if (argcount == MAX_FUNC_ARGS)
489 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
490 else
491 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
492 }
493
494 while (--argcount >= 0)
495 clear_tv(&argvars[argcount]);
496
497 *arg = skipwhite(argp);
498 return ret;
499}
500
501#define FLEN_FIXED 40
502
503/*
504 * Return TRUE if "p" starts with "<SID>" or "s:".
505 * Only works if eval_fname_script() returned non-zero for "p"!
506 */
507 static int
508eval_fname_sid(char_u *p)
509{
510 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
511}
512
513/*
514 * In a script change <SID>name() and s:name() to K_SNR 123_name().
515 * Change <SNR>123_name() to K_SNR 123_name().
516 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
517 * (slow).
518 */
519 static char_u *
520fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
521{
522 int llen;
523 char_u *fname;
524 int i;
525
526 llen = eval_fname_script(name);
527 if (llen > 0)
528 {
529 fname_buf[0] = K_SPECIAL;
530 fname_buf[1] = KS_EXTRA;
531 fname_buf[2] = (int)KE_SNR;
532 i = 3;
533 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
534 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200535 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200536 *error = ERROR_SCRIPT;
537 else
538 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200539 sprintf((char *)fname_buf + 3, "%ld_",
540 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200541 i = (int)STRLEN(fname_buf);
542 }
543 }
544 if (i + STRLEN(name + llen) < FLEN_FIXED)
545 {
546 STRCPY(fname_buf + i, name + llen);
547 fname = fname_buf;
548 }
549 else
550 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200551 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200552 if (fname == NULL)
553 *error = ERROR_OTHER;
554 else
555 {
556 *tofree = fname;
557 mch_memmove(fname, fname_buf, (size_t)i);
558 STRCPY(fname + i, name + llen);
559 }
560 }
561 }
562 else
563 fname = name;
564 return fname;
565}
566
567/*
568 * Find a function by name, return pointer to it in ufuncs.
569 * Return NULL for unknown function.
570 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200571 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200572find_func(char_u *name)
573{
574 hashitem_T *hi;
575
576 hi = hash_find(&func_hashtab, name);
577 if (!HASHITEM_EMPTY(hi))
578 return HI2UF(hi);
579 return NULL;
580}
581
582/*
583 * Copy the function name of "fp" to buffer "buf".
584 * "buf" must be able to hold the function name plus three bytes.
585 * Takes care of script-local function names.
586 */
587 static void
588cat_func_name(char_u *buf, ufunc_T *fp)
589{
590 if (fp->uf_name[0] == K_SPECIAL)
591 {
592 STRCPY(buf, "<SNR>");
593 STRCAT(buf, fp->uf_name + 3);
594 }
595 else
596 STRCPY(buf, fp->uf_name);
597}
598
599/*
600 * Add a number variable "name" to dict "dp" with value "nr".
601 */
602 static void
603add_nr_var(
604 dict_T *dp,
605 dictitem_T *v,
606 char *name,
607 varnumber_T nr)
608{
609 STRCPY(v->di_key, name);
610 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
611 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
612 v->di_tv.v_type = VAR_NUMBER;
613 v->di_tv.v_lock = VAR_FIXED;
614 v->di_tv.vval.v_number = nr;
615}
616
617/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100618 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200619 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100620 static void
621free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200622{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100623 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200624
625 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
626 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100627 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200628
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100629 // When garbage collecting a funccall_T may be freed before the
630 // function that references it, clear its uf_scoped field.
631 // The function may have been redefined and point to another
632 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200633 if (fp != NULL && fp->uf_scoped == fc)
634 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200635 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200636 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200637
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200638 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200639 vim_free(fc);
640}
641
642/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100643 * Free "fc" and what it contains.
644 * Can be called only when "fc" is kept beyond the period of it called,
645 * i.e. after cleanup_function_call(fc).
646 */
647 static void
648free_funccal_contents(funccall_T *fc)
649{
650 listitem_T *li;
651
652 // Free all l: variables.
653 vars_clear(&fc->l_vars.dv_hashtab);
654
655 // Free all a: variables.
656 vars_clear(&fc->l_avars.dv_hashtab);
657
658 // Free the a:000 variables.
659 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
660 clear_tv(&li->li_tv);
661
662 free_funccal(fc);
663}
664
665/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200666 * Handle the last part of returning from a function: free the local hashtable.
667 * Unless it is still in use by a closure.
668 */
669 static void
670cleanup_function_call(funccall_T *fc)
671{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100672 int may_free_fc = fc->fc_refcount <= 0;
673 int free_fc = TRUE;
674
Bram Moolenaar6914c642017-04-01 21:21:30 +0200675 current_funccal = fc->caller;
676
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100677 // Free all l: variables if not referred.
678 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
679 vars_clear(&fc->l_vars.dv_hashtab);
680 else
681 free_fc = FALSE;
682
683 // If the a:000 list and the l: and a: dicts are not referenced and
684 // there is no closure using it, we can free the funccall_T and what's
685 // in it.
686 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
687 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200688 else
689 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100690 int todo;
691 hashitem_T *hi;
692 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200693
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100694 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200695
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100696 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200697 todo = (int)fc->l_avars.dv_hashtab.ht_used;
698 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
699 {
700 if (!HASHITEM_EMPTY(hi))
701 {
702 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100703 di = HI2DI(hi);
704 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200705 }
706 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100707 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200708
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100709 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
710 fc->l_varlist.lv_first = NULL;
711 else
712 {
713 listitem_T *li;
714
715 free_fc = FALSE;
716
717 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200718 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
719 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100720 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100721
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100722 if (free_fc)
723 free_funccal(fc);
724 else
725 {
726 static int made_copy = 0;
727
728 // "fc" is still in use. This can happen when returning "a:000",
729 // assigning "l:" to a global variable or defining a closure.
730 // Link "fc" in the list for garbage collection later.
731 fc->caller = previous_funccal;
732 previous_funccal = fc;
733
734 if (want_garbage_collect)
735 // If garbage collector is ready, clear count.
736 made_copy = 0;
737 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100738 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100739 // We have made a lot of copies, worth 4 Mbyte. This can happen
740 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100741 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100742 // too much memory.
743 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100744 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100745 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200746 }
747}
748
749/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 * Call a user function.
751 */
752 static void
753call_user_func(
754 ufunc_T *fp, /* pointer to function */
755 int argcount, /* nr of args */
756 typval_T *argvars, /* arguments */
757 typval_T *rettv, /* return value */
758 linenr_T firstline, /* first line of range */
759 linenr_T lastline, /* last line of range */
760 dict_T *selfdict) /* Dictionary for "self" */
761{
762 char_u *save_sourcing_name;
763 linenr_T save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200764 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +0200765 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766 funccall_T *fc;
767 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200768 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769 static int depth = 0;
770 dictitem_T *v;
771 int fixvar_idx = 0; /* index in fixvar[] */
772 int i;
773 int ai;
774 int islambda = FALSE;
775 char_u numbuf[NUMBUFLEN];
776 char_u *name;
777 size_t len;
778#ifdef FEAT_PROFILE
779 proftime_T wait_start;
780 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +0200781 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200782#endif
783
784 /* If depth of calling is getting too high, don't execute the function */
785 if (depth >= p_mfd)
786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200788 rettv->v_type = VAR_NUMBER;
789 rettv->vval.v_number = -1;
790 return;
791 }
792 ++depth;
793
794 line_breakcheck(); /* check for CTRL-C hit */
795
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200796 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100797 if (fc == NULL)
798 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 fc->caller = current_funccal;
800 current_funccal = fc;
801 fc->func = fp;
802 fc->rettv = rettv;
803 rettv->vval.v_number = 0;
804 fc->linenr = 0;
805 fc->returned = FALSE;
806 fc->level = ex_nesting_level;
807 /* Check if this function has a breakpoint. */
808 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
809 fc->dbg_tick = debug_tick;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200810 /* Set up fields for closure. */
811 fc->fc_refcount = 0;
812 fc->fc_copyID = 0;
813 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200814 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200815
816 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
817 islambda = TRUE;
818
819 /*
820 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
821 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
822 * each argument variable and saves a lot of time.
823 */
824 /*
825 * Init l: variables.
826 */
827 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
828 if (selfdict != NULL)
829 {
830 /* Set l:self to "selfdict". Use "name" to avoid a warning from
831 * some compiler that checks the destination size. */
832 v = &fc->fixvar[fixvar_idx++].var;
833 name = v->di_key;
834 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +0100835 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200836 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
837 v->di_tv.v_type = VAR_DICT;
838 v->di_tv.v_lock = 0;
839 v->di_tv.vval.v_dict = selfdict;
840 ++selfdict->dv_refcount;
841 }
842
843 /*
844 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200845 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200846 * Set a:000 to a list with room for the "..." arguments.
847 */
848 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
849 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200850 (varnumber_T)(argcount >= fp->uf_args.ga_len
851 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +0100852 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200853 /* Use "name" to avoid a warning from some compiler that checks the
854 * destination size. */
855 v = &fc->fixvar[fixvar_idx++].var;
856 name = v->di_key;
857 STRCPY(name, "000");
858 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
859 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
860 v->di_tv.v_type = VAR_LIST;
861 v->di_tv.v_lock = VAR_FIXED;
862 v->di_tv.vval.v_list = &fc->l_varlist;
863 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
864 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
865 fc->l_varlist.lv_lock = VAR_FIXED;
866
867 /*
868 * Set a:firstline to "firstline" and a:lastline to "lastline".
869 * Set a:name to named arguments.
870 * Set a:N to the "..." arguments.
871 */
872 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
873 (varnumber_T)firstline);
874 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
875 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200876 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200877 {
878 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200879 typval_T def_rettv;
880 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200881
882 ai = i - fp->uf_args.ga_len;
883 if (ai < 0)
884 {
885 /* named argument a:name */
886 name = FUNCARG(fp, i);
887 if (islambda)
888 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200889
890 // evaluate named argument default expression
891 isdefault = ai + fp->uf_def_args.ga_len >= 0
892 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
893 && argvars[i].vval.v_number == VVAL_NONE));
894 if (isdefault)
895 {
896 char_u *default_expr = NULL;
897 def_rettv.v_type = VAR_NUMBER;
898 def_rettv.vval.v_number = -1;
899
900 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
901 [ai + fp->uf_def_args.ga_len];
902 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
903 {
904 default_arg_err = 1;
905 break;
906 }
907 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200908 }
909 else
910 {
911 /* "..." argument a:1, a:2, etc. */
912 sprintf((char *)numbuf, "%d", ai + 1);
913 name = numbuf;
914 }
915 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
916 {
917 v = &fc->fixvar[fixvar_idx++].var;
918 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100919 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200920 }
921 else
922 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100923 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200924 if (v == NULL)
925 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100926 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200927 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200928
Bram Moolenaar6e5000d2019-06-17 21:18:41 +0200929 // Note: the values are copied directly to avoid alloc/free.
930 // "argvars" must have VAR_FIXED for v_lock.
931 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200932 v->di_tv.v_lock = VAR_FIXED;
933
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200934 if (addlocal)
935 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200936 /* Named arguments should be accessed without the "a:" prefix in
937 * lambda expressions. Add to the l: dict. */
938 copy_tv(&v->di_tv, &v->di_tv);
939 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200940 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200941 else
942 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200943
944 if (ai >= 0 && ai < MAX_FUNC_ARGS)
945 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100946 listitem_T *li = &fc->l_listitems[ai];
947
948 li->li_tv = argvars[i];
949 li->li_tv.v_lock = VAR_FIXED;
950 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200951 }
952 }
953
954 /* Don't redraw while executing the function. */
955 ++RedrawingDisabled;
956 save_sourcing_name = sourcing_name;
957 save_sourcing_lnum = sourcing_lnum;
958 sourcing_lnum = 1;
Bram Moolenaar93343722018-07-10 19:39:18 +0200959
960 if (fp->uf_flags & FC_SANDBOX)
961 {
962 using_sandbox = TRUE;
963 ++sandbox;
964 }
965
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200966 /* need space for function name + ("function " + 3) or "[number]" */
967 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
968 + STRLEN(fp->uf_name) + 20;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200969 sourcing_name = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200970 if (sourcing_name != NULL)
971 {
972 if (save_sourcing_name != NULL
973 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
974 sprintf((char *)sourcing_name, "%s[%d]..",
975 save_sourcing_name, (int)save_sourcing_lnum);
976 else
977 STRCPY(sourcing_name, "function ");
978 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
979
980 if (p_verbose >= 12)
981 {
982 ++no_wait_return;
983 verbose_enter_scroll();
984
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100985 smsg(_("calling %s"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200986 if (p_verbose >= 14)
987 {
988 char_u buf[MSG_BUF_LEN];
989 char_u numbuf2[NUMBUFLEN];
990 char_u *tofree;
991 char_u *s;
992
Bram Moolenaar32526b32019-01-19 17:43:09 +0100993 msg_puts("(");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200994 for (i = 0; i < argcount; ++i)
995 {
996 if (i > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100997 msg_puts(", ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200998 if (argvars[i].v_type == VAR_NUMBER)
999 msg_outnum((long)argvars[i].vval.v_number);
1000 else
1001 {
1002 /* Do not want errors such as E724 here. */
1003 ++emsg_off;
1004 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1005 --emsg_off;
1006 if (s != NULL)
1007 {
1008 if (vim_strsize(s) > MSG_BUF_CLEN)
1009 {
1010 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1011 s = buf;
1012 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001013 msg_puts((char *)s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001014 vim_free(tofree);
1015 }
1016 }
1017 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001018 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001019 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001020 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001021
1022 verbose_leave_scroll();
1023 --no_wait_return;
1024 }
1025 }
1026#ifdef FEAT_PROFILE
1027 if (do_profiling == PROF_YES)
1028 {
1029 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001030 {
1031 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001032 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001033 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001034 if (fp->uf_profiling
1035 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1036 {
1037 ++fp->uf_tm_count;
1038 profile_start(&call_start);
1039 profile_zero(&fp->uf_tm_children);
1040 }
1041 script_prof_save(&wait_start);
1042 }
1043#endif
1044
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001045 save_current_sctx = current_sctx;
1046 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001047 save_did_emsg = did_emsg;
1048 did_emsg = FALSE;
1049
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001050 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1051 did_emsg = TRUE;
1052 else
1053 // call do_cmdline() to execute the lines
1054 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001055 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1056
1057 --RedrawingDisabled;
1058
1059 /* when the function was aborted because of an error, return -1 */
1060 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1061 {
1062 clear_tv(rettv);
1063 rettv->v_type = VAR_NUMBER;
1064 rettv->vval.v_number = -1;
1065 }
1066
1067#ifdef FEAT_PROFILE
1068 if (do_profiling == PROF_YES && (fp->uf_profiling
1069 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1070 {
1071 profile_end(&call_start);
1072 profile_sub_wait(&wait_start, &call_start);
1073 profile_add(&fp->uf_tm_total, &call_start);
1074 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1075 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1076 {
1077 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1078 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1079 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001080 if (started_profiling)
1081 // make a ":profdel func" stop profiling the function
1082 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001083 }
1084#endif
1085
1086 /* when being verbose, mention the return value */
1087 if (p_verbose >= 12)
1088 {
1089 ++no_wait_return;
1090 verbose_enter_scroll();
1091
1092 if (aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001093 smsg(_("%s aborted"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001094 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001095 smsg(_("%s returning #%ld"), sourcing_name,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001096 (long)fc->rettv->vval.v_number);
1097 else
1098 {
1099 char_u buf[MSG_BUF_LEN];
1100 char_u numbuf2[NUMBUFLEN];
1101 char_u *tofree;
1102 char_u *s;
1103
1104 /* The value may be very long. Skip the middle part, so that we
1105 * have some idea how it starts and ends. smsg() would always
1106 * truncate it at the end. Don't want errors such as E724 here. */
1107 ++emsg_off;
1108 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1109 --emsg_off;
1110 if (s != NULL)
1111 {
1112 if (vim_strsize(s) > MSG_BUF_CLEN)
1113 {
1114 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1115 s = buf;
1116 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001117 smsg(_("%s returning %s"), sourcing_name, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001118 vim_free(tofree);
1119 }
1120 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001121 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001122
1123 verbose_leave_scroll();
1124 --no_wait_return;
1125 }
1126
1127 vim_free(sourcing_name);
1128 sourcing_name = save_sourcing_name;
1129 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001130 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001131#ifdef FEAT_PROFILE
1132 if (do_profiling == PROF_YES)
1133 script_prof_restore(&wait_start);
1134#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001135 if (using_sandbox)
1136 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001137
1138 if (p_verbose >= 12 && sourcing_name != NULL)
1139 {
1140 ++no_wait_return;
1141 verbose_enter_scroll();
1142
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001143 smsg(_("continuing in %s"), sourcing_name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001144 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001145
1146 verbose_leave_scroll();
1147 --no_wait_return;
1148 }
1149
1150 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001151 --depth;
1152
Bram Moolenaar6914c642017-04-01 21:21:30 +02001153 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001154}
1155
1156/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001157 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001158 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001159 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001160 */
1161 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001162funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001163{
1164 funccall_T **pfc;
1165 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001166
1167 if (fc == NULL)
1168 return;
1169
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001170 if (--fc->fc_refcount <= 0 && (force || (
1171 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001172 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001173 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001174 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001175 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001176 if (fc == *pfc)
1177 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001178 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001179 free_funccal_contents(fc);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001180 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001181 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001182 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001183 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001184 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001185 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001186}
1187
1188/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001189 * Remove the function from the function hashtable. If the function was
1190 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001191 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001192 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001193 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001194func_remove(ufunc_T *fp)
1195{
1196 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1197
1198 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001199 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001200 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001201 return TRUE;
1202 }
1203 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001204}
1205
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001206 static void
1207func_clear_items(ufunc_T *fp)
1208{
1209 ga_clear_strings(&(fp->uf_args));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001210 ga_clear_strings(&(fp->uf_def_args));
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001211 ga_clear_strings(&(fp->uf_lines));
1212#ifdef FEAT_PROFILE
1213 vim_free(fp->uf_tml_count);
1214 fp->uf_tml_count = NULL;
1215 vim_free(fp->uf_tml_total);
1216 fp->uf_tml_total = NULL;
1217 vim_free(fp->uf_tml_self);
1218 fp->uf_tml_self = NULL;
1219#endif
1220}
1221
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001222/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001223 * Free all things that a function contains. Does not free the function
1224 * itself, use func_free() for that.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001225 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001226 */
1227 static void
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001228func_clear(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001229{
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001230 if (fp->uf_cleared)
1231 return;
1232 fp->uf_cleared = TRUE;
1233
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234 /* clear this function */
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001235 func_clear_items(fp);
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001236 funccal_unref(fp->uf_scoped, fp, force);
1237}
1238
1239/*
1240 * Free a function and remove it from the list of functions. Does not free
1241 * what a function contains, call func_clear() first.
1242 */
1243 static void
1244func_free(ufunc_T *fp)
1245{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001246 /* only remove it when not done already, otherwise we would remove a newer
1247 * version of the function */
1248 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1249 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001250
1251 vim_free(fp);
1252}
1253
Bram Moolenaarc2574872016-08-11 22:51:05 +02001254/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001255 * Free all things that a function contains and free the function itself.
1256 * When "force" is TRUE we are exiting.
1257 */
1258 static void
1259func_clear_free(ufunc_T *fp, int force)
1260{
1261 func_clear(fp, force);
1262 func_free(fp);
1263}
1264
1265/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001266 * There are two kinds of function names:
1267 * 1. ordinary names, function defined with :function
1268 * 2. numbered functions and lambdas
1269 * For the first we only count the name stored in func_hashtab as a reference,
1270 * using function() does not count as a reference, because the function is
1271 * looked up by name.
1272 */
1273 static int
1274func_name_refcount(char_u *name)
1275{
1276 return isdigit(*name) || *name == '<';
1277}
1278
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001279static funccal_entry_T *funccal_stack = NULL;
1280
1281/*
1282 * Save the current function call pointer, and set it to NULL.
1283 * Used when executing autocommands and for ":source".
1284 */
1285 void
1286save_funccal(funccal_entry_T *entry)
1287{
1288 entry->top_funccal = current_funccal;
1289 entry->next = funccal_stack;
1290 funccal_stack = entry;
1291 current_funccal = NULL;
1292}
1293
1294 void
1295restore_funccal(void)
1296{
1297 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001298 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001299 else
1300 {
1301 current_funccal = funccal_stack->top_funccal;
1302 funccal_stack = funccal_stack->next;
1303 }
1304}
1305
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001306 funccall_T *
1307get_current_funccal(void)
1308{
1309 return current_funccal;
1310}
1311
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001312#if defined(EXITFREE) || defined(PROTO)
1313 void
1314free_all_functions(void)
1315{
1316 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001317 ufunc_T *fp;
1318 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001319 long_u todo = 1;
1320 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001321
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001322 /* Clean up the current_funccal chain and the funccal stack. */
Bram Moolenaar6914c642017-04-01 21:21:30 +02001323 while (current_funccal != NULL)
1324 {
1325 clear_tv(current_funccal->rettv);
1326 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001327 if (current_funccal == NULL && funccal_stack != NULL)
1328 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001329 }
1330
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001331 /* First clear what the functions contain. Since this may lower the
1332 * reference count of a function, it may also free a function and change
1333 * the hash table. Restart if that happens. */
1334 while (todo > 0)
1335 {
1336 todo = func_hashtab.ht_used;
1337 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1338 if (!HASHITEM_EMPTY(hi))
1339 {
1340 /* Only free functions that are not refcounted, those are
1341 * supposed to be freed when no longer referenced. */
1342 fp = HI2UF(hi);
1343 if (func_name_refcount(fp->uf_name))
1344 ++skipped;
1345 else
1346 {
1347 used = func_hashtab.ht_used;
1348 func_clear(fp, TRUE);
1349 if (used != func_hashtab.ht_used)
1350 {
1351 skipped = 0;
1352 break;
1353 }
1354 }
1355 --todo;
1356 }
1357 }
1358
1359 /* Now actually free the functions. Need to start all over every time,
1360 * because func_free() may change the hash table. */
1361 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001362 while (func_hashtab.ht_used > skipped)
1363 {
1364 todo = func_hashtab.ht_used;
1365 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001366 if (!HASHITEM_EMPTY(hi))
1367 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001368 --todo;
1369 /* Only free functions that are not refcounted, those are
1370 * supposed to be freed when no longer referenced. */
1371 fp = HI2UF(hi);
1372 if (func_name_refcount(fp->uf_name))
1373 ++skipped;
1374 else
1375 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001376 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001377 skipped = 0;
1378 break;
1379 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001380 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001381 }
1382 if (skipped == 0)
1383 hash_clear(&func_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001384}
1385#endif
1386
1387/*
1388 * Return TRUE if "name" looks like a builtin function name: starts with a
1389 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1390 * "len" is the length of "name", or -1 for NUL terminated.
1391 */
1392 static int
1393builtin_function(char_u *name, int len)
1394{
1395 char_u *p;
1396
1397 if (!ASCII_ISLOWER(name[0]))
1398 return FALSE;
1399 p = vim_strchr(name, AUTOLOAD_CHAR);
1400 return p == NULL || (len > 0 && p > name + len);
1401}
1402
1403 int
1404func_call(
1405 char_u *name,
1406 typval_T *args,
1407 partial_T *partial,
1408 dict_T *selfdict,
1409 typval_T *rettv)
1410{
1411 listitem_T *item;
1412 typval_T argv[MAX_FUNC_ARGS + 1];
1413 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001414 int r = 0;
1415
1416 for (item = args->vval.v_list->lv_first; item != NULL;
1417 item = item->li_next)
1418 {
1419 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1420 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001421 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001422 break;
1423 }
1424 /* Make a copy of each argument. This is needed to be able to set
1425 * v_lock to VAR_FIXED in the copy without changing the original list.
1426 */
1427 copy_tv(&item->li_tv, &argv[argc++]);
1428 }
1429
1430 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001431 {
1432 funcexe_T funcexe;
1433
Bram Moolenaarac92e252019-08-03 21:58:38 +02001434 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001435 funcexe.firstline = curwin->w_cursor.lnum;
1436 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001437 funcexe.evaluate = TRUE;
1438 funcexe.partial = partial;
1439 funcexe.selfdict = selfdict;
1440 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1441 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001442
1443 /* Free the arguments. */
1444 while (argc > 0)
1445 clear_tv(&argv[--argc]);
1446
1447 return r;
1448}
1449
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001450static int callback_depth = 0;
1451
1452 int
1453get_callback_depth(void)
1454{
1455 return callback_depth;
1456}
1457
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001458/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001459 * Invoke call_func() with a callback.
1460 */
1461 int
1462call_callback(
1463 callback_T *callback,
1464 int len, // length of "name" or -1 to use strlen()
1465 typval_T *rettv, // return value goes here
1466 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001467 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001468 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001469{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001470 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001471 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001472
1473 vim_memset(&funcexe, 0, sizeof(funcexe));
1474 funcexe.evaluate = TRUE;
1475 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001476 ++callback_depth;
1477 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1478 --callback_depth;
1479 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001480}
1481
1482/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001483 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001484 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001485 * Return FAIL when the function can't be called, OK otherwise.
1486 * Also returns OK when an error was encountered while executing the function.
1487 */
1488 int
1489call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001490 char_u *funcname, // name of the function
1491 int len, // length of "name" or -1 to use strlen()
1492 typval_T *rettv, // return value goes here
1493 int argcount_in, // number of "argvars"
1494 typval_T *argvars_in, // vars for arguments, must have "argcount"
1495 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001496 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001497{
1498 int ret = FAIL;
1499 int error = ERROR_NONE;
1500 int i;
1501 ufunc_T *fp;
1502 char_u fname_buf[FLEN_FIXED + 1];
1503 char_u *tofree = NULL;
1504 char_u *fname;
1505 char_u *name;
1506 int argcount = argcount_in;
1507 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001508 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001509 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1510 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001512 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001513 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001514
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001515 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1516 // even when call_func() returns FAIL.
1517 rettv->v_type = VAR_UNKNOWN;
1518
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001519 // Make a copy of the name, if it comes from a funcref variable it could
1520 // be changed or deleted in the called function.
1521 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001522 if (name == NULL)
1523 return ret;
1524
1525 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1526
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001527 if (funcexe->doesrange != NULL)
1528 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001529
1530 if (partial != NULL)
1531 {
1532 /* When the function has a partial with a dict and there is a dict
1533 * argument, use the dict argument. That is backwards compatible.
1534 * When the dict was bound explicitly use the one from the partial. */
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001535 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001536 selfdict = partial->pt_dict;
1537 if (error == ERROR_NONE && partial->pt_argc > 0)
1538 {
1539 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
1540 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
1541 for (i = 0; i < argcount_in; ++i)
1542 argv[i + argv_clear] = argvars_in[i];
1543 argvars = argv;
1544 argcount = partial->pt_argc + argcount_in;
1545 }
1546 }
1547
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001548 if (error == ERROR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001549 {
1550 char_u *rfname = fname;
1551
1552 /* Ignore "g:" before a function name. */
1553 if (fname[0] == 'g' && fname[1] == ':')
1554 rfname = fname + 2;
1555
1556 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
1557 rettv->vval.v_number = 0;
1558 error = ERROR_UNKNOWN;
1559
1560 if (!builtin_function(rfname, -1))
1561 {
1562 /*
1563 * User defined function.
1564 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001565 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001566 fp = partial->pt_func;
1567 else
1568 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001569
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001570 /* Trigger FuncUndefined event, may load the function. */
1571 if (fp == NULL
1572 && apply_autocmds(EVENT_FUNCUNDEFINED,
1573 rfname, rfname, TRUE, NULL)
1574 && !aborting())
1575 {
1576 /* executed an autocommand, search for the function again */
1577 fp = find_func(rfname);
1578 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579 /* Try loading a package. */
1580 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1581 {
1582 /* loaded a package, search for the function again */
1583 fp = find_func(rfname);
1584 }
1585
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001586 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1587 error = ERROR_DELETED;
1588 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001589 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001590 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001591 // postponed filling in the arguments, do it now
1592 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001593 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001594
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001595 if (funcexe->basetv != NULL)
1596 {
1597 // Method call: base->Method()
1598 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1599 argv[0] = *funcexe->basetv;
1600 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001601 argvars = argv;
1602 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001603 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001604
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001605 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1606 *funcexe->doesrange = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001607 if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608 error = ERROR_TOOFEW;
1609 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1610 error = ERROR_TOOMANY;
1611 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1612 error = ERROR_DICT;
1613 else
1614 {
1615 int did_save_redo = FALSE;
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001616 save_redo_T save_redo;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001617
1618 /*
1619 * Call the user function.
1620 * Save and restore search patterns, script variables and
1621 * redo buffer.
1622 */
1623 save_search_patterns();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001624 if (!ins_compl_active())
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 {
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001626 saveRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 did_save_redo = TRUE;
1628 }
1629 ++fp->uf_calls;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001630 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001631 funcexe->firstline, funcexe->lastline,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001633 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001634 /* Function was unreferenced while being used, free it
1635 * now. */
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001636 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637 if (did_save_redo)
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001638 restoreRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001639 restore_search_patterns();
1640 error = ERROR_NONE;
1641 }
1642 }
1643 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001644 else if (funcexe->basetv != NULL)
1645 {
1646 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001647 * expr->method(): Find the method name in the table, call its
1648 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001649 */
1650 error = call_internal_method(fname, argcount, argvars, rettv,
1651 funcexe->basetv);
1652 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001653 else
1654 {
1655 /*
1656 * Find the function name in the table, call its implementation.
1657 */
1658 error = call_internal_func(fname, argcount, argvars, rettv);
1659 }
1660 /*
1661 * The function call (or "FuncUndefined" autocommand sequence) might
1662 * have been aborted by an error, an interrupt, or an explicitly thrown
1663 * exception that has not been caught so far. This situation can be
1664 * tested for by calling aborting(). For an error in an internal
1665 * function or for the "E132" error in call_user_func(), however, the
1666 * throw point at which the "force_abort" flag (temporarily reset by
1667 * emsg()) is normally updated has not been reached yet. We need to
1668 * update that flag first to make aborting() reliable.
1669 */
1670 update_force_abort();
1671 }
1672 if (error == ERROR_NONE)
1673 ret = OK;
1674
1675 /*
1676 * Report an error unless the argument evaluation or function call has been
1677 * cancelled due to an aborting error, an interrupt, or an exception.
1678 */
1679 if (!aborting())
1680 {
1681 switch (error)
1682 {
1683 case ERROR_UNKNOWN:
1684 emsg_funcname(N_("E117: Unknown function: %s"), name);
1685 break;
Bram Moolenaar91746392019-08-16 22:22:31 +02001686 case ERROR_NOTMETHOD:
1687 emsg_funcname(
1688 N_("E276: Cannot use function as a method: %s"),
1689 name);
1690 break;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001691 case ERROR_DELETED:
1692 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1693 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001694 case ERROR_TOOMANY:
1695 emsg_funcname((char *)e_toomanyarg, name);
1696 break;
1697 case ERROR_TOOFEW:
Bram Moolenaar91746392019-08-16 22:22:31 +02001698 emsg_funcname(
1699 N_("E119: Not enough arguments for function: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001700 name);
1701 break;
1702 case ERROR_SCRIPT:
Bram Moolenaar91746392019-08-16 22:22:31 +02001703 emsg_funcname(
1704 N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001705 name);
1706 break;
1707 case ERROR_DICT:
Bram Moolenaar91746392019-08-16 22:22:31 +02001708 emsg_funcname(
1709 N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001710 name);
1711 break;
1712 }
1713 }
1714
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001715 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001717 clear_tv(&argv[--argv_clear + argv_base]);
1718
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001719 vim_free(tofree);
1720 vim_free(name);
1721
1722 return ret;
1723}
1724
1725/*
1726 * List the head of the function: "name(arg1, arg2)".
1727 */
1728 static void
1729list_func_head(ufunc_T *fp, int indent)
1730{
1731 int j;
1732
1733 msg_start();
1734 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001735 msg_puts(" ");
1736 msg_puts("function ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001737 if (fp->uf_name[0] == K_SPECIAL)
1738 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001739 msg_puts_attr("<SNR>", HL_ATTR(HLF_8));
1740 msg_puts((char *)fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001741 }
1742 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001743 msg_puts((char *)fp->uf_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001744 msg_putchar('(');
1745 for (j = 0; j < fp->uf_args.ga_len; ++j)
1746 {
1747 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001748 msg_puts(", ");
1749 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001750 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1751 {
1752 msg_puts(" = ");
1753 msg_puts(((char **)(fp->uf_def_args.ga_data))
1754 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1755 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 }
1757 if (fp->uf_varargs)
1758 {
1759 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001760 msg_puts(", ");
1761 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 }
1763 msg_putchar(')');
1764 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001765 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001767 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001769 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001770 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001771 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001772 msg_clr_eos();
1773 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001774 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001775}
1776
1777/*
1778 * Get a function name, translating "<SID>" and "<SNR>".
1779 * Also handles a Funcref in a List or Dictionary.
1780 * Returns the function name in allocated memory, or NULL for failure.
1781 * flags:
1782 * TFN_INT: internal function name OK
1783 * TFN_QUIET: be quiet
1784 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001785 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 * Advances "pp" to just after the function name (if no error).
1787 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001788 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001789trans_function_name(
1790 char_u **pp,
1791 int skip, /* only find the end, don't evaluate */
1792 int flags,
1793 funcdict_T *fdp, /* return: info about dictionary used */
1794 partial_T **partial) /* return: partial of a FuncRef */
1795{
1796 char_u *name = NULL;
1797 char_u *start;
1798 char_u *end;
1799 int lead;
1800 char_u sid_buf[20];
1801 int len;
1802 lval_T lv;
1803
1804 if (fdp != NULL)
1805 vim_memset(fdp, 0, sizeof(funcdict_T));
1806 start = *pp;
1807
1808 /* Check for hard coded <SNR>: already translated function ID (from a user
1809 * command). */
1810 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1811 && (*pp)[2] == (int)KE_SNR)
1812 {
1813 *pp += 3;
1814 len = get_id_len(pp) + 3;
1815 return vim_strnsave(start, len);
1816 }
1817
1818 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
1819 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
1820 lead = eval_fname_script(start);
1821 if (lead > 2)
1822 start += lead;
1823
1824 /* Note that TFN_ flags use the same values as GLV_ flags. */
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001825 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001826 lead > 2 ? 0 : FNE_CHECK_START);
1827 if (end == start)
1828 {
1829 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001830 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001831 goto theend;
1832 }
1833 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1834 {
1835 /*
1836 * Report an invalid expression in braces, unless the expression
1837 * evaluation has been cancelled due to an aborting error, an
1838 * interrupt, or an exception.
1839 */
1840 if (!aborting())
1841 {
1842 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001843 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001844 }
1845 else
1846 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1847 goto theend;
1848 }
1849
1850 if (lv.ll_tv != NULL)
1851 {
1852 if (fdp != NULL)
1853 {
1854 fdp->fd_dict = lv.ll_dict;
1855 fdp->fd_newkey = lv.ll_newkey;
1856 lv.ll_newkey = NULL;
1857 fdp->fd_di = lv.ll_di;
1858 }
1859 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1860 {
1861 name = vim_strsave(lv.ll_tv->vval.v_string);
1862 *pp = end;
1863 }
1864 else if (lv.ll_tv->v_type == VAR_PARTIAL
1865 && lv.ll_tv->vval.v_partial != NULL)
1866 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001867 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001868 *pp = end;
1869 if (partial != NULL)
1870 *partial = lv.ll_tv->vval.v_partial;
1871 }
1872 else
1873 {
1874 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1875 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001876 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001877 else
1878 *pp = end;
1879 name = NULL;
1880 }
1881 goto theend;
1882 }
1883
1884 if (lv.ll_name == NULL)
1885 {
1886 /* Error found, but continue after the function name. */
1887 *pp = end;
1888 goto theend;
1889 }
1890
1891 /* Check if the name is a Funcref. If so, use the value. */
1892 if (lv.ll_exp_name != NULL)
1893 {
1894 len = (int)STRLEN(lv.ll_exp_name);
1895 name = deref_func_name(lv.ll_exp_name, &len, partial,
1896 flags & TFN_NO_AUTOLOAD);
1897 if (name == lv.ll_exp_name)
1898 name = NULL;
1899 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001900 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001901 {
1902 len = (int)(end - *pp);
1903 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1904 if (name == *pp)
1905 name = NULL;
1906 }
1907 if (name != NULL)
1908 {
1909 name = vim_strsave(name);
1910 *pp = end;
1911 if (STRNCMP(name, "<SNR>", 5) == 0)
1912 {
1913 /* Change "<SNR>" to the byte sequence. */
1914 name[0] = K_SPECIAL;
1915 name[1] = KS_EXTRA;
1916 name[2] = (int)KE_SNR;
1917 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1918 }
1919 goto theend;
1920 }
1921
1922 if (lv.ll_exp_name != NULL)
1923 {
1924 len = (int)STRLEN(lv.ll_exp_name);
1925 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1926 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1927 {
1928 /* When there was "s:" already or the name expanded to get a
1929 * leading "s:" then remove it. */
1930 lv.ll_name += 2;
1931 len -= 2;
1932 lead = 2;
1933 }
1934 }
1935 else
1936 {
1937 /* skip over "s:" and "g:" */
1938 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1939 lv.ll_name += 2;
1940 len = (int)(end - lv.ll_name);
1941 }
1942
1943 /*
1944 * Copy the function name to allocated memory.
1945 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1946 * Accept <SNR>123_name() outside a script.
1947 */
1948 if (skip)
1949 lead = 0; /* do nothing */
1950 else if (lead > 0)
1951 {
1952 lead = 3;
1953 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1954 || eval_fname_sid(*pp))
1955 {
1956 /* It's "s:" or "<SID>" */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001957 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001958 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001959 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960 goto theend;
1961 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001962 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 lead += (int)STRLEN(sid_buf);
1964 }
1965 }
1966 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1967 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001968 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001969 start);
1970 goto theend;
1971 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001972 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973 {
1974 char_u *cp = vim_strchr(lv.ll_name, ':');
1975
1976 if (cp != NULL && cp < end)
1977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001978 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001979 goto theend;
1980 }
1981 }
1982
Bram Moolenaar964b3742019-05-24 18:54:09 +02001983 name = alloc(len + lead + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001984 if (name != NULL)
1985 {
1986 if (lead > 0)
1987 {
1988 name[0] = K_SPECIAL;
1989 name[1] = KS_EXTRA;
1990 name[2] = (int)KE_SNR;
1991 if (lead > 3) /* If it's "<SID>" */
1992 STRCPY(name + 3, sid_buf);
1993 }
1994 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1995 name[lead + len] = NUL;
1996 }
1997 *pp = end;
1998
1999theend:
2000 clear_lval(&lv);
2001 return name;
2002}
2003
2004/*
2005 * ":function"
2006 */
2007 void
2008ex_function(exarg_T *eap)
2009{
2010 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002011 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002012 int j;
2013 int c;
2014 int saved_did_emsg;
2015 int saved_wait_return = need_wait_return;
2016 char_u *name = NULL;
2017 char_u *p;
2018 char_u *arg;
2019 char_u *line_arg = NULL;
2020 garray_T newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002021 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002022 garray_T newlines;
2023 int varargs = FALSE;
2024 int flags = 0;
2025 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002026 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002027 int indent;
2028 int nesting;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 dictitem_T *v;
2030 funcdict_T fudi;
2031 static int func_nr = 0; /* number for nameless function */
2032 int paren;
2033 hashtab_T *ht;
2034 int todo;
2035 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002036 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002037 linenr_T sourcing_lnum_off;
2038 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002039 int is_heredoc = FALSE;
2040 char_u *skip_until = NULL;
2041 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002042
2043 /*
2044 * ":function" without argument: list functions.
2045 */
2046 if (ends_excmd(*eap->arg))
2047 {
2048 if (!eap->skip)
2049 {
2050 todo = (int)func_hashtab.ht_used;
2051 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2052 {
2053 if (!HASHITEM_EMPTY(hi))
2054 {
2055 --todo;
2056 fp = HI2UF(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02002057 if (message_filtered(fp->uf_name))
2058 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002059 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002060 list_func_head(fp, FALSE);
2061 }
2062 }
2063 }
2064 eap->nextcmd = check_nextcmd(eap->arg);
2065 return;
2066 }
2067
2068 /*
2069 * ":function /pat": list functions matching pattern.
2070 */
2071 if (*eap->arg == '/')
2072 {
2073 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2074 if (!eap->skip)
2075 {
2076 regmatch_T regmatch;
2077
2078 c = *p;
2079 *p = NUL;
2080 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2081 *p = c;
2082 if (regmatch.regprog != NULL)
2083 {
2084 regmatch.rm_ic = p_ic;
2085
2086 todo = (int)func_hashtab.ht_used;
2087 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2088 {
2089 if (!HASHITEM_EMPTY(hi))
2090 {
2091 --todo;
2092 fp = HI2UF(hi);
2093 if (!isdigit(*fp->uf_name)
2094 && vim_regexec(&regmatch, fp->uf_name, 0))
2095 list_func_head(fp, FALSE);
2096 }
2097 }
2098 vim_regfree(regmatch.regprog);
2099 }
2100 }
2101 if (*p == '/')
2102 ++p;
2103 eap->nextcmd = check_nextcmd(p);
2104 return;
2105 }
2106
2107 /*
2108 * Get the function name. There are these situations:
2109 * func normal function name
2110 * "name" == func, "fudi.fd_dict" == NULL
2111 * dict.func new dictionary entry
2112 * "name" == NULL, "fudi.fd_dict" set,
2113 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2114 * dict.func existing dict entry with a Funcref
2115 * "name" == func, "fudi.fd_dict" set,
2116 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2117 * dict.func existing dict entry that's not a Funcref
2118 * "name" == NULL, "fudi.fd_dict" set,
2119 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2120 * s:func script-local function name
2121 * g:func global function name, same as "func"
2122 */
2123 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002124 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002125 paren = (vim_strchr(p, '(') != NULL);
2126 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2127 {
2128 /*
2129 * Return on an invalid expression in braces, unless the expression
2130 * evaluation has been cancelled due to an aborting error, an
2131 * interrupt, or an exception.
2132 */
2133 if (!aborting())
2134 {
2135 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002136 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002137 vim_free(fudi.fd_newkey);
2138 return;
2139 }
2140 else
2141 eap->skip = TRUE;
2142 }
2143
2144 /* An error in a function call during evaluation of an expression in magic
2145 * braces should not cause the function not to be defined. */
2146 saved_did_emsg = did_emsg;
2147 did_emsg = FALSE;
2148
2149 /*
2150 * ":function func" with only function name: list function.
2151 */
2152 if (!paren)
2153 {
2154 if (!ends_excmd(*skipwhite(p)))
2155 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002156 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002157 goto ret_free;
2158 }
2159 eap->nextcmd = check_nextcmd(p);
2160 if (eap->nextcmd != NULL)
2161 *p = NUL;
2162 if (!eap->skip && !got_int)
2163 {
2164 fp = find_func(name);
2165 if (fp != NULL)
2166 {
2167 list_func_head(fp, TRUE);
2168 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2169 {
2170 if (FUNCLINE(fp, j) == NULL)
2171 continue;
2172 msg_putchar('\n');
2173 msg_outnum((long)(j + 1));
2174 if (j < 9)
2175 msg_putchar(' ');
2176 if (j < 99)
2177 msg_putchar(' ');
2178 msg_prt_line(FUNCLINE(fp, j), FALSE);
2179 out_flush(); /* show a line at a time */
2180 ui_breakcheck();
2181 }
2182 if (!got_int)
2183 {
2184 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01002185 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002186 }
2187 }
2188 else
2189 emsg_funcname(N_("E123: Undefined function: %s"), name);
2190 }
2191 goto ret_free;
2192 }
2193
2194 /*
2195 * ":function name(arg1, arg2)" Define function.
2196 */
2197 p = skipwhite(p);
2198 if (*p != '(')
2199 {
2200 if (!eap->skip)
2201 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002202 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002203 goto ret_free;
2204 }
2205 /* attempt to continue by skipping some text */
2206 if (vim_strchr(p, '(') != NULL)
2207 p = vim_strchr(p, '(');
2208 }
2209 p = skipwhite(p + 1);
2210
2211 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2212
2213 if (!eap->skip)
2214 {
2215 /* Check the name of the function. Unless it's a dictionary function
2216 * (that we are overwriting). */
2217 if (name != NULL)
2218 arg = name;
2219 else
2220 arg = fudi.fd_newkey;
2221 if (arg != NULL && (fudi.fd_di == NULL
2222 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2223 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2224 {
2225 if (*arg == K_SPECIAL)
2226 j = 3;
2227 else
2228 j = 0;
2229 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2230 : eval_isnamec(arg[j])))
2231 ++j;
2232 if (arg[j] != NUL)
2233 emsg_funcname((char *)e_invarg2, arg);
2234 }
2235 /* Disallow using the g: dict. */
2236 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002237 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 }
2239
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002240 if (get_function_args(&p, ')', &newargs, &varargs,
2241 &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002242 goto errret_2;
2243
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002244 /* find extra arguments "range", "dict", "abort" and "closure" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002245 for (;;)
2246 {
2247 p = skipwhite(p);
2248 if (STRNCMP(p, "range", 5) == 0)
2249 {
2250 flags |= FC_RANGE;
2251 p += 5;
2252 }
2253 else if (STRNCMP(p, "dict", 4) == 0)
2254 {
2255 flags |= FC_DICT;
2256 p += 4;
2257 }
2258 else if (STRNCMP(p, "abort", 5) == 0)
2259 {
2260 flags |= FC_ABORT;
2261 p += 5;
2262 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002263 else if (STRNCMP(p, "closure", 7) == 0)
2264 {
2265 flags |= FC_CLOSURE;
2266 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02002267 if (current_funccal == NULL)
2268 {
Bram Moolenaarba209902016-08-24 22:06:38 +02002269 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
Bram Moolenaar58016442016-07-31 18:30:22 +02002270 name == NULL ? (char_u *)"" : name);
2271 goto erret;
2272 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002273 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002274 else
2275 break;
2276 }
2277
2278 /* When there is a line break use what follows for the function body.
2279 * Makes 'exe "func Test()\n...\nendfunc"' work. */
2280 if (*p == '\n')
2281 line_arg = p + 1;
2282 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002283 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002284
2285 /*
2286 * Read the body of the function, until ":endfunction" is found.
2287 */
2288 if (KeyTyped)
2289 {
2290 /* Check if the function already exists, don't let the user type the
2291 * whole function before telling him it doesn't work! For a script we
2292 * need to skip the body to be able to find what follows. */
2293 if (!eap->skip && !eap->forceit)
2294 {
2295 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002296 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002297 else if (name != NULL && find_func(name) != NULL)
2298 emsg_funcname(e_funcexts, name);
2299 }
2300
2301 if (!eap->skip && did_emsg)
2302 goto erret;
2303
2304 msg_putchar('\n'); /* don't overwrite the function name */
2305 cmdline_row = msg_row;
2306 }
2307
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002308 // Save the starting line number.
2309 sourcing_lnum_top = sourcing_lnum;
2310
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002311 indent = 2;
2312 nesting = 0;
2313 for (;;)
2314 {
2315 if (KeyTyped)
2316 {
2317 msg_scroll = TRUE;
2318 saved_wait_return = FALSE;
2319 }
2320 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002321
2322 if (line_arg != NULL)
2323 {
2324 /* Use eap->arg, split up in parts by line breaks. */
2325 theline = line_arg;
2326 p = vim_strchr(theline, '\n');
2327 if (p == NULL)
2328 line_arg += STRLEN(line_arg);
2329 else
2330 {
2331 *p = NUL;
2332 line_arg = p + 1;
2333 }
2334 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002335 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002336 {
2337 vim_free(line_to_free);
2338 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002339 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002340 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002341 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002342 line_to_free = theline;
2343 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002344 if (KeyTyped)
2345 lines_left = Rows - 1;
2346 if (theline == NULL)
2347 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002348 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002349 goto erret;
2350 }
2351
2352 /* Detect line continuation: sourcing_lnum increased more than one. */
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002353 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
2354 if (sourcing_lnum < sourcing_lnum_off)
2355 sourcing_lnum_off -= sourcing_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002356 else
2357 sourcing_lnum_off = 0;
2358
2359 if (skip_until != NULL)
2360 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002361 // Don't check for ":endfunc" between
2362 // * ":append" and "."
2363 // * ":python <<EOF" and "EOF"
2364 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2365 if (heredoc_trimmed == NULL
2366 || (is_heredoc && skipwhite(theline) == theline)
2367 || STRNCMP(theline, heredoc_trimmed,
2368 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002369 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002370 if (heredoc_trimmed == NULL)
2371 p = theline;
2372 else if (is_heredoc)
2373 p = skipwhite(theline) == theline
2374 ? theline : theline + STRLEN(heredoc_trimmed);
2375 else
2376 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002377 if (STRCMP(p, skip_until) == 0)
2378 {
2379 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002380 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002381 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002382 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002383 }
2384 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002385 }
2386 else
2387 {
2388 /* skip ':' and blanks*/
Bram Moolenaar1c465442017-03-12 20:10:05 +01002389 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002390 ;
2391
2392 /* Check for "endfunction". */
2393 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2394 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002395 char_u *nextcmd = NULL;
2396
Bram Moolenaar663bb232017-06-22 19:12:10 +02002397 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002398 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002399 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002400 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002401 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002402 give_warning2(
2403 (char_u *)_("W22: Text found after :endfunction: %s"),
2404 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002405 if (nextcmd != NULL)
2406 {
2407 /* Another command follows. If the line came from "eap" we
2408 * can simply point into it, otherwise we need to change
2409 * "eap->cmdlinep". */
2410 eap->nextcmd = nextcmd;
2411 if (line_to_free != NULL)
2412 {
2413 vim_free(*eap->cmdlinep);
2414 *eap->cmdlinep = line_to_free;
2415 line_to_free = NULL;
2416 }
2417 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002418 break;
2419 }
2420
2421 /* Increase indent inside "if", "while", "for" and "try", decrease
2422 * at "end". */
2423 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2424 indent -= 2;
2425 else if (STRNCMP(p, "if", 2) == 0
2426 || STRNCMP(p, "wh", 2) == 0
2427 || STRNCMP(p, "for", 3) == 0
2428 || STRNCMP(p, "try", 3) == 0)
2429 indent += 2;
2430
2431 /* Check for defining a function inside this function. */
2432 if (checkforcmd(&p, "function", 2))
2433 {
2434 if (*p == '!')
2435 p = skipwhite(p + 1);
2436 p += eval_fname_script(p);
2437 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2438 if (*skipwhite(p) == '(')
2439 {
2440 ++nesting;
2441 indent += 2;
2442 }
2443 }
2444
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002445 /* Check for ":append", ":change", ":insert". */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002446 p = skip_range(p, NULL);
2447 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002448 || (p[0] == 'c'
2449 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2450 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2451 && (STRNCMP(&p[3], "nge", 3) != 0
2452 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453 || (p[0] == 'i'
2454 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2455 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2456 skip_until = vim_strsave((char_u *)".");
2457
2458 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
2459 arg = skipwhite(skiptowhite(p));
2460 if (arg[0] == '<' && arg[1] =='<'
2461 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002462 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2463 || ((p[2] == '3' || p[2] == 'x')
2464 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002465 || (p[0] == 'p' && p[1] == 'e'
2466 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2467 || (p[0] == 't' && p[1] == 'c'
2468 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2469 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2470 && !ASCII_ISALPHA(p[3]))
2471 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2472 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2473 || (p[0] == 'm' && p[1] == 'z'
2474 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2475 ))
2476 {
2477 /* ":python <<" continues until a dot, like ":append" */
2478 p = skipwhite(arg + 2);
2479 if (*p == NUL)
2480 skip_until = vim_strsave((char_u *)".");
2481 else
2482 skip_until = vim_strsave(p);
2483 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002484
2485 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002486 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002487 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002488 if (*arg == '[')
2489 arg = vim_strchr(arg, ']');
2490 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002491 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002492 arg = skipwhite(skiptowhite(arg));
2493 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2494 && ((p[0] == 'l'
2495 && p[1] == 'e'
2496 && (!ASCII_ISALNUM(p[2])
2497 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002498 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002499 p = skipwhite(arg + 3);
2500 if (STRNCMP(p, "trim", 4) == 0)
2501 {
2502 // Ignore leading white space.
2503 p = skipwhite(p + 4);
2504 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002505 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002506 }
2507 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2508 do_concat = FALSE;
2509 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002510 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002511 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002512 }
2513
2514 /* Add the line to the function. */
2515 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002516 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002517
2518 /* Copy the line to newly allocated memory. get_one_sourceline()
2519 * allocates 250 bytes per line, this saves 80% on average. The cost
2520 * is an extra alloc/free. */
2521 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002522 if (p == NULL)
2523 goto erret;
2524 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002525
2526 /* Add NULL lines for continuation lines, so that the line count is
2527 * equal to the index in the growarray. */
2528 while (sourcing_lnum_off-- > 0)
2529 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2530
2531 /* Check for end of eap->arg. */
2532 if (line_arg != NULL && *line_arg == NUL)
2533 line_arg = NULL;
2534 }
2535
2536 /* Don't define the function when skipping commands or when an error was
2537 * detected. */
2538 if (eap->skip || did_emsg)
2539 goto erret;
2540
2541 /*
2542 * If there are no errors, add the function
2543 */
2544 if (fudi.fd_dict == NULL)
2545 {
2546 v = find_var(name, &ht, FALSE);
2547 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2548 {
2549 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2550 name);
2551 goto erret;
2552 }
2553
2554 fp = find_func(name);
2555 if (fp != NULL)
2556 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002557 // Function can be replaced with "function!" and when sourcing the
2558 // same script again, but only once.
2559 if (!eap->forceit
2560 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2561 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002562 {
2563 emsg_funcname(e_funcexts, name);
2564 goto erret;
2565 }
2566 if (fp->uf_calls > 0)
2567 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002568 emsg_funcname(
2569 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002570 name);
2571 goto erret;
2572 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002573 if (fp->uf_refcount > 1)
2574 {
2575 /* This function is referenced somewhere, don't redefine it but
2576 * create a new one. */
2577 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002578 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002579 fp = NULL;
2580 overwrite = TRUE;
2581 }
2582 else
2583 {
2584 /* redefine existing function */
Bram Moolenaard23a8232018-02-10 18:45:26 +01002585 VIM_CLEAR(name);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002586 func_clear_items(fp);
2587#ifdef FEAT_PROFILE
2588 fp->uf_profiling = FALSE;
2589 fp->uf_prof_initialized = FALSE;
2590#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002591 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002592 }
2593 }
2594 else
2595 {
2596 char numbuf[20];
2597
2598 fp = NULL;
2599 if (fudi.fd_newkey == NULL && !eap->forceit)
2600 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002601 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002602 goto erret;
2603 }
2604 if (fudi.fd_di == NULL)
2605 {
2606 /* Can't add a function to a locked dictionary */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002607 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608 goto erret;
2609 }
2610 /* Can't change an existing function if it is locked */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002611 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002612 goto erret;
2613
2614 /* Give the function a sequential number. Can only be used with a
2615 * Funcref! */
2616 vim_free(name);
2617 sprintf(numbuf, "%d", ++func_nr);
2618 name = vim_strsave((char_u *)numbuf);
2619 if (name == NULL)
2620 goto erret;
2621 }
2622
2623 if (fp == NULL)
2624 {
2625 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2626 {
2627 int slen, plen;
2628 char_u *scriptname;
2629
2630 /* Check that the autoload name matches the script name. */
2631 j = FAIL;
2632 if (sourcing_name != NULL)
2633 {
2634 scriptname = autoload_name(name);
2635 if (scriptname != NULL)
2636 {
2637 p = vim_strchr(scriptname, '/');
2638 plen = (int)STRLEN(p);
2639 slen = (int)STRLEN(sourcing_name);
2640 if (slen > plen && fnamecmp(p,
2641 sourcing_name + slen - plen) == 0)
2642 j = OK;
2643 vim_free(scriptname);
2644 }
2645 }
2646 if (j == FAIL)
2647 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002648 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002649 goto erret;
2650 }
2651 }
2652
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002653 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002654 if (fp == NULL)
2655 goto erret;
2656
2657 if (fudi.fd_dict != NULL)
2658 {
2659 if (fudi.fd_di == NULL)
2660 {
2661 /* add new dict entry */
2662 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2663 if (fudi.fd_di == NULL)
2664 {
2665 vim_free(fp);
2666 goto erret;
2667 }
2668 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2669 {
2670 vim_free(fudi.fd_di);
2671 vim_free(fp);
2672 goto erret;
2673 }
2674 }
2675 else
2676 /* overwrite existing dict entry */
2677 clear_tv(&fudi.fd_di->di_tv);
2678 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680
2681 /* behave like "dict" was used */
2682 flags |= FC_DICT;
2683 }
2684
2685 /* insert the new function in the function list */
2686 STRCPY(fp->uf_name, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002687 if (overwrite)
2688 {
2689 hi = hash_find(&func_hashtab, name);
2690 hi->hi_key = UF2HIKEY(fp);
2691 }
2692 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002693 {
2694 vim_free(fp);
2695 goto erret;
2696 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002697 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002698 }
2699 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002700 fp->uf_def_args = default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002701 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002702 if ((flags & FC_CLOSURE) != 0)
2703 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002704 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002705 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002706 }
2707 else
2708 fp->uf_scoped = NULL;
2709
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002710#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002711 if (prof_def_func())
2712 func_do_profile(fp);
2713#endif
2714 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02002715 if (sandbox)
2716 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002717 fp->uf_flags = flags;
2718 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002719 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002720 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002721 goto ret_free;
2722
2723erret:
2724 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002725 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002726errret_2:
2727 ga_clear_strings(&newlines);
2728ret_free:
2729 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002730 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002731 vim_free(fudi.fd_newkey);
2732 vim_free(name);
2733 did_emsg |= saved_did_emsg;
2734 need_wait_return |= saved_wait_return;
2735}
2736
2737/*
2738 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2739 * Return 2 if "p" starts with "s:".
2740 * Return 0 otherwise.
2741 */
2742 int
2743eval_fname_script(char_u *p)
2744{
2745 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2746 * the standard library function. */
2747 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2748 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2749 return 5;
2750 if (p[0] == 's' && p[1] == ':')
2751 return 2;
2752 return 0;
2753}
2754
2755 int
2756translated_function_exists(char_u *name)
2757{
2758 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02002759 return has_internal_func(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002760 return find_func(name) != NULL;
2761}
2762
2763/*
2764 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002765 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766 */
2767 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002768function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002769{
2770 char_u *nm = name;
2771 char_u *p;
2772 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002773 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002774
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002775 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2776 if (no_deref)
2777 flag |= TFN_NO_DEREF;
2778 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002779 nm = skipwhite(nm);
2780
2781 /* Only accept "funcname", "funcname ", "funcname (..." and
2782 * "funcname(...", not "funcname!...". */
2783 if (p != NULL && (*nm == NUL || *nm == '('))
2784 n = translated_function_exists(p);
2785 vim_free(p);
2786 return n;
2787}
2788
Bram Moolenaar113e1072019-01-20 15:30:40 +01002789#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002790 char_u *
2791get_expanded_name(char_u *name, int check)
2792{
2793 char_u *nm = name;
2794 char_u *p;
2795
2796 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2797
2798 if (p != NULL && *nm == NUL)
2799 if (!check || translated_function_exists(p))
2800 return p;
2801
2802 vim_free(p);
2803 return NULL;
2804}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002805#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002806
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002807/*
2808 * Function given to ExpandGeneric() to obtain the list of user defined
2809 * function names.
2810 */
2811 char_u *
2812get_user_func_name(expand_T *xp, int idx)
2813{
2814 static long_u done;
2815 static hashitem_T *hi;
2816 ufunc_T *fp;
2817
2818 if (idx == 0)
2819 {
2820 done = 0;
2821 hi = func_hashtab.ht_array;
2822 }
2823 if (done < func_hashtab.ht_used)
2824 {
2825 if (done++ > 0)
2826 ++hi;
2827 while (HASHITEM_EMPTY(hi))
2828 ++hi;
2829 fp = HI2UF(hi);
2830
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002831 if ((fp->uf_flags & FC_DICT)
2832 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2833 return (char_u *)""; /* don't show dict and lambda functions */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002834
2835 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
2836 return fp->uf_name; /* prevents overflow */
2837
2838 cat_func_name(IObuff, fp);
2839 if (xp->xp_context != EXPAND_USER_FUNC)
2840 {
2841 STRCAT(IObuff, "(");
2842 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2843 STRCAT(IObuff, ")");
2844 }
2845 return IObuff;
2846 }
2847 return NULL;
2848}
2849
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002850/*
2851 * ":delfunction {name}"
2852 */
2853 void
2854ex_delfunction(exarg_T *eap)
2855{
2856 ufunc_T *fp = NULL;
2857 char_u *p;
2858 char_u *name;
2859 funcdict_T fudi;
2860
2861 p = eap->arg;
2862 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2863 vim_free(fudi.fd_newkey);
2864 if (name == NULL)
2865 {
2866 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002867 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002868 return;
2869 }
2870 if (!ends_excmd(*skipwhite(p)))
2871 {
2872 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002873 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002874 return;
2875 }
2876 eap->nextcmd = check_nextcmd(p);
2877 if (eap->nextcmd != NULL)
2878 *p = NUL;
2879
2880 if (!eap->skip)
2881 fp = find_func(name);
2882 vim_free(name);
2883
2884 if (!eap->skip)
2885 {
2886 if (fp == NULL)
2887 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02002888 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002889 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002890 return;
2891 }
2892 if (fp->uf_calls > 0)
2893 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002894 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002895 return;
2896 }
2897
2898 if (fudi.fd_dict != NULL)
2899 {
2900 /* Delete the dict item that refers to the function, it will
2901 * invoke func_unref() and possibly delete the function. */
2902 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2903 }
2904 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002905 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002906 /* A normal function (not a numbered function or lambda) has a
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002907 * refcount of 1 for the entry in the hashtable. When deleting
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002908 * it and the refcount is more than one, it should be kept.
Bram Moolenaarba209902016-08-24 22:06:38 +02002909 * A numbered function and lambda should be kept if the refcount is
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002910 * one or more. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002911 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002912 {
2913 /* Function is still referenced somewhere. Don't free it but
2914 * do remove it from the hashtable. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002915 if (func_remove(fp))
2916 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002917 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002918 }
2919 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002920 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002921 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002922 }
2923}
2924
2925/*
2926 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002927 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002928 */
2929 void
2930func_unref(char_u *name)
2931{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002932 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002934 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002936 fp = find_func(name);
2937 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002938 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002940 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002942 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002944 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002946 /* Only delete it when it's not being used. Otherwise it's done
2947 * when "uf_calls" becomes zero. */
2948 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002949 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02002950 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002951}
2952
2953/*
2954 * Unreference a Function: decrement the reference count and free it when it
2955 * becomes zero.
2956 */
2957 void
2958func_ptr_unref(ufunc_T *fp)
2959{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002960 if (fp != NULL && --fp->uf_refcount <= 0)
2961 {
2962 /* Only delete it when it's not being used. Otherwise it's done
2963 * when "uf_calls" becomes zero. */
2964 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002965 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002966 }
2967}
2968
2969/*
2970 * Count a reference to a Function.
2971 */
2972 void
2973func_ref(char_u *name)
2974{
2975 ufunc_T *fp;
2976
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002977 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002978 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002979 fp = find_func(name);
2980 if (fp != NULL)
2981 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002982 else if (isdigit(*name))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002983 /* Only give an error for a numbered function.
2984 * Fail silently, when named or lambda function isn't found. */
Bram Moolenaar95f09602016-11-10 20:01:45 +01002985 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002986}
2987
2988/*
2989 * Count a reference to a Function.
2990 */
2991 void
2992func_ptr_ref(ufunc_T *fp)
2993{
2994 if (fp != NULL)
2995 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002996}
2997
2998/*
2999 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3000 * referenced from anywhere that is in use.
3001 */
3002 static int
3003can_free_funccal(funccall_T *fc, int copyID)
3004{
3005 return (fc->l_varlist.lv_copyID != copyID
3006 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003007 && fc->l_avars.dv_copyID != copyID
3008 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003009}
3010
3011/*
3012 * ":return [expr]"
3013 */
3014 void
3015ex_return(exarg_T *eap)
3016{
3017 char_u *arg = eap->arg;
3018 typval_T rettv;
3019 int returning = FALSE;
3020
3021 if (current_funccal == NULL)
3022 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003023 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003024 return;
3025 }
3026
3027 if (eap->skip)
3028 ++emsg_skip;
3029
3030 eap->nextcmd = NULL;
3031 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3032 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3033 {
3034 if (!eap->skip)
3035 returning = do_return(eap, FALSE, TRUE, &rettv);
3036 else
3037 clear_tv(&rettv);
3038 }
3039 /* It's safer to return also on error. */
3040 else if (!eap->skip)
3041 {
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003042 /* In return statement, cause_abort should be force_abort. */
3043 update_force_abort();
3044
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003045 /*
3046 * Return unless the expression evaluation has been cancelled due to an
3047 * aborting error, an interrupt, or an exception.
3048 */
3049 if (!aborting())
3050 returning = do_return(eap, FALSE, TRUE, NULL);
3051 }
3052
3053 /* When skipping or the return gets pending, advance to the next command
3054 * in this line (!returning). Otherwise, ignore the rest of the line.
3055 * Following lines will be ignored by get_func_line(). */
3056 if (returning)
3057 eap->nextcmd = NULL;
3058 else if (eap->nextcmd == NULL) /* no argument */
3059 eap->nextcmd = check_nextcmd(arg);
3060
3061 if (eap->skip)
3062 --emsg_skip;
3063}
3064
3065/*
3066 * ":1,25call func(arg1, arg2)" function call.
3067 */
3068 void
3069ex_call(exarg_T *eap)
3070{
3071 char_u *arg = eap->arg;
3072 char_u *startarg;
3073 char_u *name;
3074 char_u *tofree;
3075 int len;
3076 typval_T rettv;
3077 linenr_T lnum;
3078 int doesrange;
3079 int failed = FALSE;
3080 funcdict_T fudi;
3081 partial_T *partial = NULL;
3082
3083 if (eap->skip)
3084 {
3085 /* trans_function_name() doesn't work well when skipping, use eval0()
3086 * instead to skip to any following command, e.g. for:
3087 * :if 0 | call dict.foo().bar() | endif */
3088 ++emsg_skip;
3089 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3090 clear_tv(&rettv);
3091 --emsg_skip;
3092 return;
3093 }
3094
3095 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3096 if (fudi.fd_newkey != NULL)
3097 {
3098 /* Still need to give an error message for missing key. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003099 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003100 vim_free(fudi.fd_newkey);
3101 }
3102 if (tofree == NULL)
3103 return;
3104
3105 /* Increase refcount on dictionary, it could get deleted when evaluating
3106 * the arguments. */
3107 if (fudi.fd_dict != NULL)
3108 ++fudi.fd_dict->dv_refcount;
3109
3110 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3111 * contents. For VAR_PARTIAL get its partial, unless we already have one
3112 * from trans_function_name(). */
3113 len = (int)STRLEN(tofree);
3114 name = deref_func_name(tofree, &len,
3115 partial != NULL ? NULL : &partial, FALSE);
3116
3117 /* Skip white space to allow ":call func ()". Not good, but required for
3118 * backward compatibility. */
3119 startarg = skipwhite(arg);
3120 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
3121
3122 if (*startarg != '(')
3123 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02003124 semsg(_(e_missingparen), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003125 goto end;
3126 }
3127
3128 /*
3129 * When skipping, evaluate the function once, to find the end of the
3130 * arguments.
3131 * When the function takes a range, this is discovered after the first
3132 * call, and the loop is broken.
3133 */
3134 if (eap->skip)
3135 {
3136 ++emsg_skip;
3137 lnum = eap->line2; /* do it once, also with an invalid range */
3138 }
3139 else
3140 lnum = eap->line1;
3141 for ( ; lnum <= eap->line2; ++lnum)
3142 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003143 funcexe_T funcexe;
3144
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003145 if (!eap->skip && eap->addr_count > 0)
3146 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003147 if (lnum > curbuf->b_ml.ml_line_count)
3148 {
3149 // If the function deleted lines or switched to another buffer
3150 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003151 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003152 break;
3153 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 curwin->w_cursor.lnum = lnum;
3155 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003157 }
3158 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003159
Bram Moolenaarac92e252019-08-03 21:58:38 +02003160 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003161 funcexe.firstline = eap->line1;
3162 funcexe.lastline = eap->line2;
3163 funcexe.doesrange = &doesrange;
3164 funcexe.evaluate = !eap->skip;
3165 funcexe.partial = partial;
3166 funcexe.selfdict = fudi.fd_dict;
3167 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003168 {
3169 failed = TRUE;
3170 break;
3171 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003172 if (has_watchexpr())
3173 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003174
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003175 // Handle a function returning a Funcref, Dictionary or List.
3176 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3177 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003178 {
3179 failed = TRUE;
3180 break;
3181 }
3182
3183 clear_tv(&rettv);
3184 if (doesrange || eap->skip)
3185 break;
3186
3187 /* Stop when immediately aborting on error, or when an interrupt
3188 * occurred or an exception was thrown but not caught.
3189 * get_func_tv() returned OK, so that the check for trailing
3190 * characters below is executed. */
3191 if (aborting())
3192 break;
3193 }
3194 if (eap->skip)
3195 --emsg_skip;
3196
3197 if (!failed)
3198 {
3199 /* Check for trailing illegal characters and a following command. */
3200 if (!ends_excmd(*arg))
3201 {
3202 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003203 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003204 }
3205 else
3206 eap->nextcmd = check_nextcmd(arg);
3207 }
3208
3209end:
3210 dict_unref(fudi.fd_dict);
3211 vim_free(tofree);
3212}
3213
3214/*
3215 * Return from a function. Possibly makes the return pending. Also called
3216 * for a pending return at the ":endtry" or after returning from an extra
3217 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3218 * when called due to a ":return" command. "rettv" may point to a typval_T
3219 * with the return rettv. Returns TRUE when the return can be carried out,
3220 * FALSE when the return gets pending.
3221 */
3222 int
3223do_return(
3224 exarg_T *eap,
3225 int reanimate,
3226 int is_cmd,
3227 void *rettv)
3228{
3229 int idx;
3230 struct condstack *cstack = eap->cstack;
3231
3232 if (reanimate)
3233 /* Undo the return. */
3234 current_funccal->returned = FALSE;
3235
3236 /*
3237 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3238 * not in its finally clause (which then is to be executed next) is found.
3239 * In this case, make the ":return" pending for execution at the ":endtry".
3240 * Otherwise, return normally.
3241 */
3242 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3243 if (idx >= 0)
3244 {
3245 cstack->cs_pending[idx] = CSTP_RETURN;
3246
3247 if (!is_cmd && !reanimate)
3248 /* A pending return again gets pending. "rettv" points to an
3249 * allocated variable with the rettv of the original ":return"'s
3250 * argument if present or is NULL else. */
3251 cstack->cs_rettv[idx] = rettv;
3252 else
3253 {
3254 /* When undoing a return in order to make it pending, get the stored
3255 * return rettv. */
3256 if (reanimate)
3257 rettv = current_funccal->rettv;
3258
3259 if (rettv != NULL)
3260 {
3261 /* Store the value of the pending return. */
3262 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3263 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3264 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003265 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003266 }
3267 else
3268 cstack->cs_rettv[idx] = NULL;
3269
3270 if (reanimate)
3271 {
3272 /* The pending return value could be overwritten by a ":return"
3273 * without argument in a finally clause; reset the default
3274 * return value. */
3275 current_funccal->rettv->v_type = VAR_NUMBER;
3276 current_funccal->rettv->vval.v_number = 0;
3277 }
3278 }
3279 report_make_pending(CSTP_RETURN, rettv);
3280 }
3281 else
3282 {
3283 current_funccal->returned = TRUE;
3284
3285 /* If the return is carried out now, store the return value. For
3286 * a return immediately after reanimation, the value is already
3287 * there. */
3288 if (!reanimate && rettv != NULL)
3289 {
3290 clear_tv(current_funccal->rettv);
3291 *current_funccal->rettv = *(typval_T *)rettv;
3292 if (!is_cmd)
3293 vim_free(rettv);
3294 }
3295 }
3296
3297 return idx < 0;
3298}
3299
3300/*
3301 * Free the variable with a pending return value.
3302 */
3303 void
3304discard_pending_return(void *rettv)
3305{
3306 free_tv((typval_T *)rettv);
3307}
3308
3309/*
3310 * Generate a return command for producing the value of "rettv". The result
3311 * is an allocated string. Used by report_pending() for verbose messages.
3312 */
3313 char_u *
3314get_return_cmd(void *rettv)
3315{
3316 char_u *s = NULL;
3317 char_u *tofree = NULL;
3318 char_u numbuf[NUMBUFLEN];
3319
3320 if (rettv != NULL)
3321 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3322 if (s == NULL)
3323 s = (char_u *)"";
3324
3325 STRCPY(IObuff, ":return ");
3326 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3327 if (STRLEN(s) + 8 >= IOSIZE)
3328 STRCPY(IObuff + IOSIZE - 4, "...");
3329 vim_free(tofree);
3330 return vim_strsave(IObuff);
3331}
3332
3333/*
3334 * Get next function line.
3335 * Called by do_cmdline() to get the next line.
3336 * Returns allocated string, or NULL for end of function.
3337 */
3338 char_u *
3339get_func_line(
3340 int c UNUSED,
3341 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003342 int indent UNUSED,
3343 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003344{
3345 funccall_T *fcp = (funccall_T *)cookie;
3346 ufunc_T *fp = fcp->func;
3347 char_u *retval;
3348 garray_T *gap; /* growarray with function lines */
3349
3350 /* If breakpoints have been added/deleted need to check for it. */
3351 if (fcp->dbg_tick != debug_tick)
3352 {
3353 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3354 sourcing_lnum);
3355 fcp->dbg_tick = debug_tick;
3356 }
3357#ifdef FEAT_PROFILE
3358 if (do_profiling == PROF_YES)
3359 func_line_end(cookie);
3360#endif
3361
3362 gap = &fp->uf_lines;
3363 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3364 || fcp->returned)
3365 retval = NULL;
3366 else
3367 {
3368 /* Skip NULL lines (continuation lines). */
3369 while (fcp->linenr < gap->ga_len
3370 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3371 ++fcp->linenr;
3372 if (fcp->linenr >= gap->ga_len)
3373 retval = NULL;
3374 else
3375 {
3376 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
3377 sourcing_lnum = fcp->linenr;
3378#ifdef FEAT_PROFILE
3379 if (do_profiling == PROF_YES)
3380 func_line_start(cookie);
3381#endif
3382 }
3383 }
3384
3385 /* Did we encounter a breakpoint? */
3386 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
3387 {
3388 dbg_breakpoint(fp->uf_name, sourcing_lnum);
3389 /* Find next breakpoint. */
3390 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3391 sourcing_lnum);
3392 fcp->dbg_tick = debug_tick;
3393 }
3394
3395 return retval;
3396}
3397
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003398/*
3399 * Return TRUE if the currently active function should be ended, because a
3400 * return was encountered or an error occurred. Used inside a ":while".
3401 */
3402 int
3403func_has_ended(void *cookie)
3404{
3405 funccall_T *fcp = (funccall_T *)cookie;
3406
3407 /* Ignore the "abort" flag if the abortion behavior has been changed due to
3408 * an error inside a try conditional. */
3409 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3410 || fcp->returned);
3411}
3412
3413/*
3414 * return TRUE if cookie indicates a function which "abort"s on errors.
3415 */
3416 int
3417func_has_abort(
3418 void *cookie)
3419{
3420 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3421}
3422
3423
3424/*
3425 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3426 * Don't do this when "Func" is already a partial that was bound
3427 * explicitly (pt_auto is FALSE).
3428 * Changes "rettv" in-place.
3429 * Returns the updated "selfdict_in".
3430 */
3431 dict_T *
3432make_partial(dict_T *selfdict_in, typval_T *rettv)
3433{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003434 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003435 char_u *tofree = NULL;
3436 ufunc_T *fp;
3437 char_u fname_buf[FLEN_FIXED + 1];
3438 int error;
3439 dict_T *selfdict = selfdict_in;
3440
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003441 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3442 fp = rettv->vval.v_partial->pt_func;
3443 else
3444 {
3445 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3446 : rettv->vval.v_partial->pt_name;
3447 /* Translate "s:func" to the stored function name. */
3448 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3449 fp = find_func(fname);
3450 vim_free(tofree);
3451 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003452
3453 if (fp != NULL && (fp->uf_flags & FC_DICT))
3454 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003455 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003456
3457 if (pt != NULL)
3458 {
3459 pt->pt_refcount = 1;
3460 pt->pt_dict = selfdict;
3461 pt->pt_auto = TRUE;
3462 selfdict = NULL;
3463 if (rettv->v_type == VAR_FUNC)
3464 {
3465 /* Just a function: Take over the function name and use
3466 * selfdict. */
3467 pt->pt_name = rettv->vval.v_string;
3468 }
3469 else
3470 {
3471 partial_T *ret_pt = rettv->vval.v_partial;
3472 int i;
3473
3474 /* Partial: copy the function name, use selfdict and copy
3475 * args. Can't take over name or args, the partial might
3476 * be referenced elsewhere. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003477 if (ret_pt->pt_name != NULL)
3478 {
3479 pt->pt_name = vim_strsave(ret_pt->pt_name);
3480 func_ref(pt->pt_name);
3481 }
3482 else
3483 {
3484 pt->pt_func = ret_pt->pt_func;
3485 func_ptr_ref(pt->pt_func);
3486 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003487 if (ret_pt->pt_argc > 0)
3488 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003489 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003490 if (pt->pt_argv == NULL)
3491 /* out of memory: drop the arguments */
3492 pt->pt_argc = 0;
3493 else
3494 {
3495 pt->pt_argc = ret_pt->pt_argc;
3496 for (i = 0; i < pt->pt_argc; i++)
3497 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3498 }
3499 }
3500 partial_unref(ret_pt);
3501 }
3502 rettv->v_type = VAR_PARTIAL;
3503 rettv->vval.v_partial = pt;
3504 }
3505 }
3506 return selfdict;
3507}
3508
3509/*
3510 * Return the name of the executed function.
3511 */
3512 char_u *
3513func_name(void *cookie)
3514{
3515 return ((funccall_T *)cookie)->func->uf_name;
3516}
3517
3518/*
3519 * Return the address holding the next breakpoint line for a funccall cookie.
3520 */
3521 linenr_T *
3522func_breakpoint(void *cookie)
3523{
3524 return &((funccall_T *)cookie)->breakpoint;
3525}
3526
3527/*
3528 * Return the address holding the debug tick for a funccall cookie.
3529 */
3530 int *
3531func_dbg_tick(void *cookie)
3532{
3533 return &((funccall_T *)cookie)->dbg_tick;
3534}
3535
3536/*
3537 * Return the nesting level for a funccall cookie.
3538 */
3539 int
3540func_level(void *cookie)
3541{
3542 return ((funccall_T *)cookie)->level;
3543}
3544
3545/*
3546 * Return TRUE when a function was ended by a ":return" command.
3547 */
3548 int
3549current_func_returned(void)
3550{
3551 return current_funccal->returned;
3552}
3553
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003554 int
3555free_unref_funccal(int copyID, int testing)
3556{
3557 int did_free = FALSE;
3558 int did_free_funccal = FALSE;
3559 funccall_T *fc, **pfc;
3560
3561 for (pfc = &previous_funccal; *pfc != NULL; )
3562 {
3563 if (can_free_funccal(*pfc, copyID))
3564 {
3565 fc = *pfc;
3566 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003567 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003568 did_free = TRUE;
3569 did_free_funccal = TRUE;
3570 }
3571 else
3572 pfc = &(*pfc)->caller;
3573 }
3574 if (did_free_funccal)
3575 /* When a funccal was freed some more items might be garbage
3576 * collected, so run again. */
3577 (void)garbage_collect(testing);
3578
3579 return did_free;
3580}
3581
3582/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003583 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003584 */
3585 static funccall_T *
3586get_funccal(void)
3587{
3588 int i;
3589 funccall_T *funccal;
3590 funccall_T *temp_funccal;
3591
3592 funccal = current_funccal;
3593 if (debug_backtrace_level > 0)
3594 {
3595 for (i = 0; i < debug_backtrace_level; i++)
3596 {
3597 temp_funccal = funccal->caller;
3598 if (temp_funccal)
3599 funccal = temp_funccal;
3600 else
3601 /* backtrace level overflow. reset to max */
3602 debug_backtrace_level = i;
3603 }
3604 }
3605 return funccal;
3606}
3607
3608/*
3609 * Return the hashtable used for local variables in the current funccal.
3610 * Return NULL if there is no current funccal.
3611 */
3612 hashtab_T *
3613get_funccal_local_ht()
3614{
3615 if (current_funccal == NULL)
3616 return NULL;
3617 return &get_funccal()->l_vars.dv_hashtab;
3618}
3619
3620/*
3621 * Return the l: scope variable.
3622 * Return NULL if there is no current funccal.
3623 */
3624 dictitem_T *
3625get_funccal_local_var()
3626{
3627 if (current_funccal == NULL)
3628 return NULL;
3629 return &get_funccal()->l_vars_var;
3630}
3631
3632/*
3633 * Return the hashtable used for argument in the current funccal.
3634 * Return NULL if there is no current funccal.
3635 */
3636 hashtab_T *
3637get_funccal_args_ht()
3638{
3639 if (current_funccal == NULL)
3640 return NULL;
3641 return &get_funccal()->l_avars.dv_hashtab;
3642}
3643
3644/*
3645 * Return the a: scope variable.
3646 * Return NULL if there is no current funccal.
3647 */
3648 dictitem_T *
3649get_funccal_args_var()
3650{
3651 if (current_funccal == NULL)
3652 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01003653 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003654}
3655
3656/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003657 * List function variables, if there is a function.
3658 */
3659 void
3660list_func_vars(int *first)
3661{
3662 if (current_funccal != NULL)
3663 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01003664 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003665}
3666
3667/*
3668 * If "ht" is the hashtable for local variables in the current funccal, return
3669 * the dict that contains it.
3670 * Otherwise return NULL.
3671 */
3672 dict_T *
3673get_current_funccal_dict(hashtab_T *ht)
3674{
3675 if (current_funccal != NULL
3676 && ht == &current_funccal->l_vars.dv_hashtab)
3677 return &current_funccal->l_vars;
3678 return NULL;
3679}
3680
3681/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003682 * Search hashitem in parent scope.
3683 */
3684 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003685find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003686{
3687 funccall_T *old_current_funccal = current_funccal;
3688 hashtab_T *ht;
3689 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003690 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003691
3692 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3693 return NULL;
3694
3695 /* Search in parent scope which is possible to reference from lambda */
3696 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003697 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003698 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003699 ht = find_var_ht(name, &varname);
3700 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003701 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003702 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003703 if (!HASHITEM_EMPTY(hi))
3704 {
3705 *pht = ht;
3706 break;
3707 }
3708 }
3709 if (current_funccal == current_funccal->func->uf_scoped)
3710 break;
3711 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003712 }
3713 current_funccal = old_current_funccal;
3714
3715 return hi;
3716}
3717
3718/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003719 * Search variable in parent scope.
3720 */
3721 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003722find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003723{
3724 dictitem_T *v = NULL;
3725 funccall_T *old_current_funccal = current_funccal;
3726 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003727 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003728
3729 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3730 return NULL;
3731
3732 /* Search in parent scope which is possible to reference from lambda */
3733 current_funccal = current_funccal->func->uf_scoped;
3734 while (current_funccal)
3735 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003738 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003739 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003740 if (v != NULL)
3741 break;
3742 }
3743 if (current_funccal == current_funccal->func->uf_scoped)
3744 break;
3745 current_funccal = current_funccal->func->uf_scoped;
3746 }
3747 current_funccal = old_current_funccal;
3748
3749 return v;
3750}
3751
3752/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003753 * Set "copyID + 1" in previous_funccal and callers.
3754 */
3755 int
3756set_ref_in_previous_funccal(int copyID)
3757{
3758 int abort = FALSE;
3759 funccall_T *fc;
3760
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003761 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003762 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003763 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003764 abort = abort
3765 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
3766 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003767 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003768 }
3769 return abort;
3770}
3771
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003772 static int
3773set_ref_in_funccal(funccall_T *fc, int copyID)
3774{
3775 int abort = FALSE;
3776
3777 if (fc->fc_copyID != copyID)
3778 {
3779 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003780 abort = abort
3781 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
3782 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003783 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003784 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003785 }
3786 return abort;
3787}
3788
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003789/*
3790 * Set "copyID" in all local vars and arguments in the call stack.
3791 */
3792 int
3793set_ref_in_call_stack(int copyID)
3794{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003795 int abort = FALSE;
3796 funccall_T *fc;
3797 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003798
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003799 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003800 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003801
3802 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003803 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
3804 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003805 abort = abort || set_ref_in_funccal(fc, copyID);
3806
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003807 return abort;
3808}
3809
3810/*
3811 * Set "copyID" in all functions available by name.
3812 */
3813 int
3814set_ref_in_functions(int copyID)
3815{
3816 int todo;
3817 hashitem_T *hi = NULL;
3818 int abort = FALSE;
3819 ufunc_T *fp;
3820
3821 todo = (int)func_hashtab.ht_used;
3822 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003823 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003824 if (!HASHITEM_EMPTY(hi))
3825 {
3826 --todo;
3827 fp = HI2UF(hi);
3828 if (!func_name_refcount(fp->uf_name))
3829 abort = abort || set_ref_in_func(NULL, fp, copyID);
3830 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003831 }
3832 return abort;
3833}
3834
3835/*
3836 * Set "copyID" in all function arguments.
3837 */
3838 int
3839set_ref_in_func_args(int copyID)
3840{
3841 int i;
3842 int abort = FALSE;
3843
3844 for (i = 0; i < funcargs.ga_len; ++i)
3845 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3846 copyID, NULL, NULL);
3847 return abort;
3848}
3849
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003850/*
3851 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003852 * Returns TRUE if setting references failed somehow.
3853 */
3854 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003855set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003856{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003857 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003858 funccall_T *fc;
3859 int error = ERROR_NONE;
3860 char_u fname_buf[FLEN_FIXED + 1];
3861 char_u *tofree = NULL;
3862 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003863 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003864
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003865 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003866 return FALSE;
3867
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003868 if (fp_in == NULL)
3869 {
3870 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3871 fp = find_func(fname);
3872 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003873 if (fp != NULL)
3874 {
3875 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003876 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003877 }
3878 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003879 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003880}
3881
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003882#endif /* FEAT_EVAL */