blob: 0ff1bdb82a34c9997870acb585cb4f2eb1fe1d37 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: User defined function support
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaara9b579f2016-07-17 18:29:19 +020025
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 Moolenaarc799fe22019-05-28 23:08:19 +0200291 fp = alloc_clear(sizeof(ufunc_T) + STRLEN(name));
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 '('
435 linenr_T firstline, // first line of range
436 linenr_T lastline, // last line of range
437 int *doesrange, // return: function handled range
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200438 int evaluate,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200439 partial_T *partial, // for extra arguments
440 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200441{
442 char_u *argp;
443 int ret = OK;
444 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
445 int argcount = 0; /* number of arguments found */
446
447 /*
448 * Get the arguments.
449 */
450 argp = *arg;
451 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
452 {
453 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
454 if (*argp == ')' || *argp == ',' || *argp == NUL)
455 break;
456 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
457 {
458 ret = FAIL;
459 break;
460 }
461 ++argcount;
462 if (*argp != ',')
463 break;
464 }
465 if (*argp == ')')
466 ++argp;
467 else
468 ret = FAIL;
469
470 if (ret == OK)
471 {
472 int i = 0;
473
474 if (get_vim_var_nr(VV_TESTING))
475 {
476 /* Prepare for calling test_garbagecollect_now(), need to know
477 * what variables are used on the call stack. */
478 if (funcargs.ga_itemsize == 0)
479 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
480 for (i = 0; i < argcount; ++i)
481 if (ga_grow(&funcargs, 1) == OK)
482 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
483 &argvars[i];
484 }
485
Bram Moolenaardf48fb42016-07-22 21:50:18 +0200486 ret = call_func(name, len, rettv, argcount, argvars, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200487 firstline, lastline, doesrange, evaluate, partial, selfdict);
488
489 funcargs.ga_len -= i;
490 }
491 else if (!aborting())
492 {
493 if (argcount == MAX_FUNC_ARGS)
494 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
495 else
496 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
497 }
498
499 while (--argcount >= 0)
500 clear_tv(&argvars[argcount]);
501
502 *arg = skipwhite(argp);
503 return ret;
504}
505
506#define FLEN_FIXED 40
507
508/*
509 * Return TRUE if "p" starts with "<SID>" or "s:".
510 * Only works if eval_fname_script() returned non-zero for "p"!
511 */
512 static int
513eval_fname_sid(char_u *p)
514{
515 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
516}
517
518/*
519 * In a script change <SID>name() and s:name() to K_SNR 123_name().
520 * Change <SNR>123_name() to K_SNR 123_name().
521 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
522 * (slow).
523 */
524 static char_u *
525fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
526{
527 int llen;
528 char_u *fname;
529 int i;
530
531 llen = eval_fname_script(name);
532 if (llen > 0)
533 {
534 fname_buf[0] = K_SPECIAL;
535 fname_buf[1] = KS_EXTRA;
536 fname_buf[2] = (int)KE_SNR;
537 i = 3;
538 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
539 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200540 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200541 *error = ERROR_SCRIPT;
542 else
543 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200544 sprintf((char *)fname_buf + 3, "%ld_",
545 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200546 i = (int)STRLEN(fname_buf);
547 }
548 }
549 if (i + STRLEN(name + llen) < FLEN_FIXED)
550 {
551 STRCPY(fname_buf + i, name + llen);
552 fname = fname_buf;
553 }
554 else
555 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200556 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200557 if (fname == NULL)
558 *error = ERROR_OTHER;
559 else
560 {
561 *tofree = fname;
562 mch_memmove(fname, fname_buf, (size_t)i);
563 STRCPY(fname + i, name + llen);
564 }
565 }
566 }
567 else
568 fname = name;
569 return fname;
570}
571
572/*
573 * Find a function by name, return pointer to it in ufuncs.
574 * Return NULL for unknown function.
575 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200576 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200577find_func(char_u *name)
578{
579 hashitem_T *hi;
580
581 hi = hash_find(&func_hashtab, name);
582 if (!HASHITEM_EMPTY(hi))
583 return HI2UF(hi);
584 return NULL;
585}
586
587/*
588 * Copy the function name of "fp" to buffer "buf".
589 * "buf" must be able to hold the function name plus three bytes.
590 * Takes care of script-local function names.
591 */
592 static void
593cat_func_name(char_u *buf, ufunc_T *fp)
594{
595 if (fp->uf_name[0] == K_SPECIAL)
596 {
597 STRCPY(buf, "<SNR>");
598 STRCAT(buf, fp->uf_name + 3);
599 }
600 else
601 STRCPY(buf, fp->uf_name);
602}
603
604/*
605 * Add a number variable "name" to dict "dp" with value "nr".
606 */
607 static void
608add_nr_var(
609 dict_T *dp,
610 dictitem_T *v,
611 char *name,
612 varnumber_T nr)
613{
614 STRCPY(v->di_key, name);
615 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
616 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
617 v->di_tv.v_type = VAR_NUMBER;
618 v->di_tv.v_lock = VAR_FIXED;
619 v->di_tv.vval.v_number = nr;
620}
621
622/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100623 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200624 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100625 static void
626free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200627{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100628 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200629
630 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
631 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100632 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200633
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100634 // When garbage collecting a funccall_T may be freed before the
635 // function that references it, clear its uf_scoped field.
636 // The function may have been redefined and point to another
637 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200638 if (fp != NULL && fp->uf_scoped == fc)
639 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200640 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200641 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200642
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200643 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200644 vim_free(fc);
645}
646
647/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100648 * Free "fc" and what it contains.
649 * Can be called only when "fc" is kept beyond the period of it called,
650 * i.e. after cleanup_function_call(fc).
651 */
652 static void
653free_funccal_contents(funccall_T *fc)
654{
655 listitem_T *li;
656
657 // Free all l: variables.
658 vars_clear(&fc->l_vars.dv_hashtab);
659
660 // Free all a: variables.
661 vars_clear(&fc->l_avars.dv_hashtab);
662
663 // Free the a:000 variables.
664 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
665 clear_tv(&li->li_tv);
666
667 free_funccal(fc);
668}
669
670/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200671 * Handle the last part of returning from a function: free the local hashtable.
672 * Unless it is still in use by a closure.
673 */
674 static void
675cleanup_function_call(funccall_T *fc)
676{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100677 int may_free_fc = fc->fc_refcount <= 0;
678 int free_fc = TRUE;
679
Bram Moolenaar6914c642017-04-01 21:21:30 +0200680 current_funccal = fc->caller;
681
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100682 // Free all l: variables if not referred.
683 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
684 vars_clear(&fc->l_vars.dv_hashtab);
685 else
686 free_fc = FALSE;
687
688 // If the a:000 list and the l: and a: dicts are not referenced and
689 // there is no closure using it, we can free the funccall_T and what's
690 // in it.
691 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
692 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200693 else
694 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100695 int todo;
696 hashitem_T *hi;
697 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200698
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100699 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200700
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100701 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200702 todo = (int)fc->l_avars.dv_hashtab.ht_used;
703 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
704 {
705 if (!HASHITEM_EMPTY(hi))
706 {
707 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100708 di = HI2DI(hi);
709 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200710 }
711 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100712 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200713
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100714 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
715 fc->l_varlist.lv_first = NULL;
716 else
717 {
718 listitem_T *li;
719
720 free_fc = FALSE;
721
722 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200723 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
724 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100725 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100726
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100727 if (free_fc)
728 free_funccal(fc);
729 else
730 {
731 static int made_copy = 0;
732
733 // "fc" is still in use. This can happen when returning "a:000",
734 // assigning "l:" to a global variable or defining a closure.
735 // Link "fc" in the list for garbage collection later.
736 fc->caller = previous_funccal;
737 previous_funccal = fc;
738
739 if (want_garbage_collect)
740 // If garbage collector is ready, clear count.
741 made_copy = 0;
742 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100743 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100744 // We have made a lot of copies, worth 4 Mbyte. This can happen
745 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100746 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100747 // too much memory.
748 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100749 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100750 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200751 }
752}
753
754/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200755 * Call a user function.
756 */
757 static void
758call_user_func(
759 ufunc_T *fp, /* pointer to function */
760 int argcount, /* nr of args */
761 typval_T *argvars, /* arguments */
762 typval_T *rettv, /* return value */
763 linenr_T firstline, /* first line of range */
764 linenr_T lastline, /* last line of range */
765 dict_T *selfdict) /* Dictionary for "self" */
766{
767 char_u *save_sourcing_name;
768 linenr_T save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200769 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +0200770 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200771 funccall_T *fc;
772 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200773 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200774 static int depth = 0;
775 dictitem_T *v;
776 int fixvar_idx = 0; /* index in fixvar[] */
777 int i;
778 int ai;
779 int islambda = FALSE;
780 char_u numbuf[NUMBUFLEN];
781 char_u *name;
782 size_t len;
783#ifdef FEAT_PROFILE
784 proftime_T wait_start;
785 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +0200786 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200787#endif
788
789 /* If depth of calling is getting too high, don't execute the function */
790 if (depth >= p_mfd)
791 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100792 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200793 rettv->v_type = VAR_NUMBER;
794 rettv->vval.v_number = -1;
795 return;
796 }
797 ++depth;
798
799 line_breakcheck(); /* check for CTRL-C hit */
800
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200801 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100802 if (fc == NULL)
803 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200804 fc->caller = current_funccal;
805 current_funccal = fc;
806 fc->func = fp;
807 fc->rettv = rettv;
808 rettv->vval.v_number = 0;
809 fc->linenr = 0;
810 fc->returned = FALSE;
811 fc->level = ex_nesting_level;
812 /* Check if this function has a breakpoint. */
813 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
814 fc->dbg_tick = debug_tick;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200815 /* Set up fields for closure. */
816 fc->fc_refcount = 0;
817 fc->fc_copyID = 0;
818 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200819 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200820
821 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
822 islambda = TRUE;
823
824 /*
825 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
826 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
827 * each argument variable and saves a lot of time.
828 */
829 /*
830 * Init l: variables.
831 */
832 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
833 if (selfdict != NULL)
834 {
835 /* Set l:self to "selfdict". Use "name" to avoid a warning from
836 * some compiler that checks the destination size. */
837 v = &fc->fixvar[fixvar_idx++].var;
838 name = v->di_key;
839 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +0100840 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200841 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
842 v->di_tv.v_type = VAR_DICT;
843 v->di_tv.v_lock = 0;
844 v->di_tv.vval.v_dict = selfdict;
845 ++selfdict->dv_refcount;
846 }
847
848 /*
849 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200850 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200851 * Set a:000 to a list with room for the "..." arguments.
852 */
853 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
854 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200855 (varnumber_T)(argcount >= fp->uf_args.ga_len
856 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +0100857 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200858 /* Use "name" to avoid a warning from some compiler that checks the
859 * destination size. */
860 v = &fc->fixvar[fixvar_idx++].var;
861 name = v->di_key;
862 STRCPY(name, "000");
863 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
864 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
865 v->di_tv.v_type = VAR_LIST;
866 v->di_tv.v_lock = VAR_FIXED;
867 v->di_tv.vval.v_list = &fc->l_varlist;
868 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
869 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
870 fc->l_varlist.lv_lock = VAR_FIXED;
871
872 /*
873 * Set a:firstline to "firstline" and a:lastline to "lastline".
874 * Set a:name to named arguments.
875 * Set a:N to the "..." arguments.
876 */
877 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
878 (varnumber_T)firstline);
879 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
880 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200881 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200882 {
883 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200884 typval_T def_rettv;
885 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200886
887 ai = i - fp->uf_args.ga_len;
888 if (ai < 0)
889 {
890 /* named argument a:name */
891 name = FUNCARG(fp, i);
892 if (islambda)
893 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200894
895 // evaluate named argument default expression
896 isdefault = ai + fp->uf_def_args.ga_len >= 0
897 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
898 && argvars[i].vval.v_number == VVAL_NONE));
899 if (isdefault)
900 {
901 char_u *default_expr = NULL;
902 def_rettv.v_type = VAR_NUMBER;
903 def_rettv.vval.v_number = -1;
904
905 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
906 [ai + fp->uf_def_args.ga_len];
907 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
908 {
909 default_arg_err = 1;
910 break;
911 }
912 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200913 }
914 else
915 {
916 /* "..." argument a:1, a:2, etc. */
917 sprintf((char *)numbuf, "%d", ai + 1);
918 name = numbuf;
919 }
920 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
921 {
922 v = &fc->fixvar[fixvar_idx++].var;
923 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100924 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200925 }
926 else
927 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100928 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200929 if (v == NULL)
930 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100931 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200932 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200933
Bram Moolenaar6e5000d2019-06-17 21:18:41 +0200934 // Note: the values are copied directly to avoid alloc/free.
935 // "argvars" must have VAR_FIXED for v_lock.
936 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200937 v->di_tv.v_lock = VAR_FIXED;
938
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200939 if (addlocal)
940 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200941 /* Named arguments should be accessed without the "a:" prefix in
942 * lambda expressions. Add to the l: dict. */
943 copy_tv(&v->di_tv, &v->di_tv);
944 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200945 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200946 else
947 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200948
949 if (ai >= 0 && ai < MAX_FUNC_ARGS)
950 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100951 listitem_T *li = &fc->l_listitems[ai];
952
953 li->li_tv = argvars[i];
954 li->li_tv.v_lock = VAR_FIXED;
955 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200956 }
957 }
958
959 /* Don't redraw while executing the function. */
960 ++RedrawingDisabled;
961 save_sourcing_name = sourcing_name;
962 save_sourcing_lnum = sourcing_lnum;
963 sourcing_lnum = 1;
Bram Moolenaar93343722018-07-10 19:39:18 +0200964
965 if (fp->uf_flags & FC_SANDBOX)
966 {
967 using_sandbox = TRUE;
968 ++sandbox;
969 }
970
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200971 /* need space for function name + ("function " + 3) or "[number]" */
972 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
973 + STRLEN(fp->uf_name) + 20;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200974 sourcing_name = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200975 if (sourcing_name != NULL)
976 {
977 if (save_sourcing_name != NULL
978 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
979 sprintf((char *)sourcing_name, "%s[%d]..",
980 save_sourcing_name, (int)save_sourcing_lnum);
981 else
982 STRCPY(sourcing_name, "function ");
983 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
984
985 if (p_verbose >= 12)
986 {
987 ++no_wait_return;
988 verbose_enter_scroll();
989
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100990 smsg(_("calling %s"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200991 if (p_verbose >= 14)
992 {
993 char_u buf[MSG_BUF_LEN];
994 char_u numbuf2[NUMBUFLEN];
995 char_u *tofree;
996 char_u *s;
997
Bram Moolenaar32526b32019-01-19 17:43:09 +0100998 msg_puts("(");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200999 for (i = 0; i < argcount; ++i)
1000 {
1001 if (i > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001002 msg_puts(", ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001003 if (argvars[i].v_type == VAR_NUMBER)
1004 msg_outnum((long)argvars[i].vval.v_number);
1005 else
1006 {
1007 /* Do not want errors such as E724 here. */
1008 ++emsg_off;
1009 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1010 --emsg_off;
1011 if (s != NULL)
1012 {
1013 if (vim_strsize(s) > MSG_BUF_CLEN)
1014 {
1015 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1016 s = buf;
1017 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001018 msg_puts((char *)s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001019 vim_free(tofree);
1020 }
1021 }
1022 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001023 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001024 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001025 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001026
1027 verbose_leave_scroll();
1028 --no_wait_return;
1029 }
1030 }
1031#ifdef FEAT_PROFILE
1032 if (do_profiling == PROF_YES)
1033 {
1034 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001035 {
1036 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001037 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001038 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001039 if (fp->uf_profiling
1040 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1041 {
1042 ++fp->uf_tm_count;
1043 profile_start(&call_start);
1044 profile_zero(&fp->uf_tm_children);
1045 }
1046 script_prof_save(&wait_start);
1047 }
1048#endif
1049
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001050 save_current_sctx = current_sctx;
1051 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001052 save_did_emsg = did_emsg;
1053 did_emsg = FALSE;
1054
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001055 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1056 did_emsg = TRUE;
1057 else
1058 // call do_cmdline() to execute the lines
1059 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001060 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1061
1062 --RedrawingDisabled;
1063
1064 /* when the function was aborted because of an error, return -1 */
1065 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1066 {
1067 clear_tv(rettv);
1068 rettv->v_type = VAR_NUMBER;
1069 rettv->vval.v_number = -1;
1070 }
1071
1072#ifdef FEAT_PROFILE
1073 if (do_profiling == PROF_YES && (fp->uf_profiling
1074 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1075 {
1076 profile_end(&call_start);
1077 profile_sub_wait(&wait_start, &call_start);
1078 profile_add(&fp->uf_tm_total, &call_start);
1079 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1080 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1081 {
1082 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1083 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1084 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001085 if (started_profiling)
1086 // make a ":profdel func" stop profiling the function
1087 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001088 }
1089#endif
1090
1091 /* when being verbose, mention the return value */
1092 if (p_verbose >= 12)
1093 {
1094 ++no_wait_return;
1095 verbose_enter_scroll();
1096
1097 if (aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001098 smsg(_("%s aborted"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001099 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001100 smsg(_("%s returning #%ld"), sourcing_name,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001101 (long)fc->rettv->vval.v_number);
1102 else
1103 {
1104 char_u buf[MSG_BUF_LEN];
1105 char_u numbuf2[NUMBUFLEN];
1106 char_u *tofree;
1107 char_u *s;
1108
1109 /* The value may be very long. Skip the middle part, so that we
1110 * have some idea how it starts and ends. smsg() would always
1111 * truncate it at the end. Don't want errors such as E724 here. */
1112 ++emsg_off;
1113 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1114 --emsg_off;
1115 if (s != NULL)
1116 {
1117 if (vim_strsize(s) > MSG_BUF_CLEN)
1118 {
1119 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1120 s = buf;
1121 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001122 smsg(_("%s returning %s"), sourcing_name, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001123 vim_free(tofree);
1124 }
1125 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001126 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001127
1128 verbose_leave_scroll();
1129 --no_wait_return;
1130 }
1131
1132 vim_free(sourcing_name);
1133 sourcing_name = save_sourcing_name;
1134 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001135 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001136#ifdef FEAT_PROFILE
1137 if (do_profiling == PROF_YES)
1138 script_prof_restore(&wait_start);
1139#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001140 if (using_sandbox)
1141 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001142
1143 if (p_verbose >= 12 && sourcing_name != NULL)
1144 {
1145 ++no_wait_return;
1146 verbose_enter_scroll();
1147
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001148 smsg(_("continuing in %s"), sourcing_name);
Bram Moolenaar32526b32019-01-19 17:43:09 +01001149 msg_puts("\n"); /* don't overwrite this either */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001150
1151 verbose_leave_scroll();
1152 --no_wait_return;
1153 }
1154
1155 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001156 --depth;
1157
Bram Moolenaar6914c642017-04-01 21:21:30 +02001158 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001159}
1160
1161/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001162 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001163 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001164 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001165 */
1166 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001167funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001168{
1169 funccall_T **pfc;
1170 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001171
1172 if (fc == NULL)
1173 return;
1174
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001175 if (--fc->fc_refcount <= 0 && (force || (
1176 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001177 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001178 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001179 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001180 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001181 if (fc == *pfc)
1182 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001183 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001184 free_funccal_contents(fc);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001185 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001186 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001187 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001188 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001189 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001190 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001191}
1192
1193/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001194 * Remove the function from the function hashtable. If the function was
1195 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001196 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001197 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001198 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001199func_remove(ufunc_T *fp)
1200{
1201 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1202
1203 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001204 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001205 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001206 return TRUE;
1207 }
1208 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001209}
1210
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001211 static void
1212func_clear_items(ufunc_T *fp)
1213{
1214 ga_clear_strings(&(fp->uf_args));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001215 ga_clear_strings(&(fp->uf_def_args));
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001216 ga_clear_strings(&(fp->uf_lines));
1217#ifdef FEAT_PROFILE
1218 vim_free(fp->uf_tml_count);
1219 fp->uf_tml_count = NULL;
1220 vim_free(fp->uf_tml_total);
1221 fp->uf_tml_total = NULL;
1222 vim_free(fp->uf_tml_self);
1223 fp->uf_tml_self = NULL;
1224#endif
1225}
1226
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001227/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001228 * Free all things that a function contains. Does not free the function
1229 * itself, use func_free() for that.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001230 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001231 */
1232 static void
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001233func_clear(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001234{
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001235 if (fp->uf_cleared)
1236 return;
1237 fp->uf_cleared = TRUE;
1238
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001239 /* clear this function */
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001240 func_clear_items(fp);
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001241 funccal_unref(fp->uf_scoped, fp, force);
1242}
1243
1244/*
1245 * Free a function and remove it from the list of functions. Does not free
1246 * what a function contains, call func_clear() first.
1247 */
1248 static void
1249func_free(ufunc_T *fp)
1250{
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001251 /* only remove it when not done already, otherwise we would remove a newer
1252 * version of the function */
1253 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1254 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001255
1256 vim_free(fp);
1257}
1258
Bram Moolenaarc2574872016-08-11 22:51:05 +02001259/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001260 * Free all things that a function contains and free the function itself.
1261 * When "force" is TRUE we are exiting.
1262 */
1263 static void
1264func_clear_free(ufunc_T *fp, int force)
1265{
1266 func_clear(fp, force);
1267 func_free(fp);
1268}
1269
1270/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001271 * There are two kinds of function names:
1272 * 1. ordinary names, function defined with :function
1273 * 2. numbered functions and lambdas
1274 * For the first we only count the name stored in func_hashtab as a reference,
1275 * using function() does not count as a reference, because the function is
1276 * looked up by name.
1277 */
1278 static int
1279func_name_refcount(char_u *name)
1280{
1281 return isdigit(*name) || *name == '<';
1282}
1283
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001284static funccal_entry_T *funccal_stack = NULL;
1285
1286/*
1287 * Save the current function call pointer, and set it to NULL.
1288 * Used when executing autocommands and for ":source".
1289 */
1290 void
1291save_funccal(funccal_entry_T *entry)
1292{
1293 entry->top_funccal = current_funccal;
1294 entry->next = funccal_stack;
1295 funccal_stack = entry;
1296 current_funccal = NULL;
1297}
1298
1299 void
1300restore_funccal(void)
1301{
1302 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001303 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001304 else
1305 {
1306 current_funccal = funccal_stack->top_funccal;
1307 funccal_stack = funccal_stack->next;
1308 }
1309}
1310
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001311 funccall_T *
1312get_current_funccal(void)
1313{
1314 return current_funccal;
1315}
1316
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001317#if defined(EXITFREE) || defined(PROTO)
1318 void
1319free_all_functions(void)
1320{
1321 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001322 ufunc_T *fp;
1323 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001324 long_u todo = 1;
1325 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001326
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001327 /* Clean up the current_funccal chain and the funccal stack. */
Bram Moolenaar6914c642017-04-01 21:21:30 +02001328 while (current_funccal != NULL)
1329 {
1330 clear_tv(current_funccal->rettv);
1331 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001332 if (current_funccal == NULL && funccal_stack != NULL)
1333 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001334 }
1335
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001336 /* First clear what the functions contain. Since this may lower the
1337 * reference count of a function, it may also free a function and change
1338 * the hash table. Restart if that happens. */
1339 while (todo > 0)
1340 {
1341 todo = func_hashtab.ht_used;
1342 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1343 if (!HASHITEM_EMPTY(hi))
1344 {
1345 /* Only free functions that are not refcounted, those are
1346 * supposed to be freed when no longer referenced. */
1347 fp = HI2UF(hi);
1348 if (func_name_refcount(fp->uf_name))
1349 ++skipped;
1350 else
1351 {
1352 used = func_hashtab.ht_used;
1353 func_clear(fp, TRUE);
1354 if (used != func_hashtab.ht_used)
1355 {
1356 skipped = 0;
1357 break;
1358 }
1359 }
1360 --todo;
1361 }
1362 }
1363
1364 /* Now actually free the functions. Need to start all over every time,
1365 * because func_free() may change the hash table. */
1366 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001367 while (func_hashtab.ht_used > skipped)
1368 {
1369 todo = func_hashtab.ht_used;
1370 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001371 if (!HASHITEM_EMPTY(hi))
1372 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001373 --todo;
1374 /* Only free functions that are not refcounted, those are
1375 * supposed to be freed when no longer referenced. */
1376 fp = HI2UF(hi);
1377 if (func_name_refcount(fp->uf_name))
1378 ++skipped;
1379 else
1380 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001381 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001382 skipped = 0;
1383 break;
1384 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001385 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001386 }
1387 if (skipped == 0)
1388 hash_clear(&func_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001389}
1390#endif
1391
1392/*
1393 * Return TRUE if "name" looks like a builtin function name: starts with a
1394 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1395 * "len" is the length of "name", or -1 for NUL terminated.
1396 */
1397 static int
1398builtin_function(char_u *name, int len)
1399{
1400 char_u *p;
1401
1402 if (!ASCII_ISLOWER(name[0]))
1403 return FALSE;
1404 p = vim_strchr(name, AUTOLOAD_CHAR);
1405 return p == NULL || (len > 0 && p > name + len);
1406}
1407
1408 int
1409func_call(
1410 char_u *name,
1411 typval_T *args,
1412 partial_T *partial,
1413 dict_T *selfdict,
1414 typval_T *rettv)
1415{
1416 listitem_T *item;
1417 typval_T argv[MAX_FUNC_ARGS + 1];
1418 int argc = 0;
1419 int dummy;
1420 int r = 0;
1421
1422 for (item = args->vval.v_list->lv_first; item != NULL;
1423 item = item->li_next)
1424 {
1425 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1426 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001427 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001428 break;
1429 }
1430 /* Make a copy of each argument. This is needed to be able to set
1431 * v_lock to VAR_FIXED in the copy without changing the original list.
1432 */
1433 copy_tv(&item->li_tv, &argv[argc++]);
1434 }
1435
1436 if (item == NULL)
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001437 r = call_func(name, -1, rettv, argc, argv, NULL,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
1439 &dummy, TRUE, partial, selfdict);
1440
1441 /* Free the arguments. */
1442 while (argc > 0)
1443 clear_tv(&argv[--argc]);
1444
1445 return r;
1446}
1447
1448/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001449 * Invoke call_func() with a callback.
1450 */
1451 int
1452call_callback(
1453 callback_T *callback,
1454 int len, // length of "name" or -1 to use strlen()
1455 typval_T *rettv, // return value goes here
1456 int argcount, // number of "argvars"
1457 typval_T *argvars, // vars for arguments, must have "argcount"
1458 // PLUS ONE elements!
1459 int (* argv_func)(int, typval_T *, int),
1460 // function to fill in argvars
1461 linenr_T firstline, // first line of range
1462 linenr_T lastline, // last line of range
1463 int *doesrange, // return: function handled range
1464 int evaluate,
1465 dict_T *selfdict) // Dictionary for "self"
1466{
1467 return call_func(callback->cb_name, len, rettv, argcount, argvars,
1468 argv_func, firstline, lastline, doesrange, evaluate,
1469 callback->cb_partial, selfdict);
1470}
1471
1472/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001473 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001474 *
1475 * "argv_func", when not NULL, can be used to fill in arguments only when the
1476 * invoked function uses them. It is called like this:
1477 * new_argcount = argv_func(current_argcount, argv, called_func_argcount)
1478 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001479 * Return FAIL when the function can't be called, OK otherwise.
1480 * Also returns OK when an error was encountered while executing the function.
1481 */
1482 int
1483call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001484 char_u *funcname, // name of the function
1485 int len, // length of "name" or -1 to use strlen()
1486 typval_T *rettv, // return value goes here
1487 int argcount_in, // number of "argvars"
1488 typval_T *argvars_in, // vars for arguments, must have "argcount"
1489 // PLUS ONE elements!
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001490 int (* argv_func)(int, typval_T *, int),
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001491 // function to fill in argvars
1492 linenr_T firstline, // first line of range
1493 linenr_T lastline, // last line of range
1494 int *doesrange, // return: function handled range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001495 int evaluate,
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001496 partial_T *partial, // optional, can be NULL
1497 dict_T *selfdict_in) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001498{
1499 int ret = FAIL;
1500 int error = ERROR_NONE;
1501 int i;
1502 ufunc_T *fp;
1503 char_u fname_buf[FLEN_FIXED + 1];
1504 char_u *tofree = NULL;
1505 char_u *fname;
1506 char_u *name;
1507 int argcount = argcount_in;
1508 typval_T *argvars = argvars_in;
1509 dict_T *selfdict = selfdict_in;
1510 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
1511 int argv_clear = 0;
1512
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001513 // Make a copy of the name, if it comes from a funcref variable it could
1514 // be changed or deleted in the called function.
1515 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001516 if (name == NULL)
1517 return ret;
1518
1519 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1520
1521 *doesrange = FALSE;
1522
1523 if (partial != NULL)
1524 {
1525 /* When the function has a partial with a dict and there is a dict
1526 * argument, use the dict argument. That is backwards compatible.
1527 * When the dict was bound explicitly use the one from the partial. */
1528 if (partial->pt_dict != NULL
1529 && (selfdict_in == NULL || !partial->pt_auto))
1530 selfdict = partial->pt_dict;
1531 if (error == ERROR_NONE && partial->pt_argc > 0)
1532 {
1533 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
1534 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
1535 for (i = 0; i < argcount_in; ++i)
1536 argv[i + argv_clear] = argvars_in[i];
1537 argvars = argv;
1538 argcount = partial->pt_argc + argcount_in;
1539 }
1540 }
1541
Bram Moolenaarb4518562018-05-22 18:31:35 +02001542 /*
1543 * Execute the function if executing and no errors were detected.
1544 */
1545 if (!evaluate)
1546 {
1547 // Not evaluating, which means the return value is unknown. This
1548 // matters for giving error messages.
1549 rettv->v_type = VAR_UNKNOWN;
1550 }
1551 else if (error == ERROR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001552 {
1553 char_u *rfname = fname;
1554
1555 /* Ignore "g:" before a function name. */
1556 if (fname[0] == 'g' && fname[1] == ':')
1557 rfname = fname + 2;
1558
1559 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
1560 rettv->vval.v_number = 0;
1561 error = ERROR_UNKNOWN;
1562
1563 if (!builtin_function(rfname, -1))
1564 {
1565 /*
1566 * User defined function.
1567 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001568 if (partial != NULL && partial->pt_func != NULL)
1569 fp = partial->pt_func;
1570 else
1571 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 /* Trigger FuncUndefined event, may load the function. */
1574 if (fp == NULL
1575 && apply_autocmds(EVENT_FUNCUNDEFINED,
1576 rfname, rfname, TRUE, NULL)
1577 && !aborting())
1578 {
1579 /* executed an autocommand, search for the function again */
1580 fp = find_func(rfname);
1581 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001582 /* Try loading a package. */
1583 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1584 {
1585 /* loaded a package, search for the function again */
1586 fp = find_func(rfname);
1587 }
1588
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001589 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1590 error = ERROR_DELETED;
1591 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001592 {
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001593 if (argv_func != NULL)
1594 argcount = argv_func(argcount, argvars, fp->uf_args.ga_len);
1595
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001596 if (fp->uf_flags & FC_RANGE)
1597 *doesrange = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001598 if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001599 error = ERROR_TOOFEW;
1600 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1601 error = ERROR_TOOMANY;
1602 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1603 error = ERROR_DICT;
1604 else
1605 {
1606 int did_save_redo = FALSE;
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001607 save_redo_T save_redo;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001608
1609 /*
1610 * Call the user function.
1611 * Save and restore search patterns, script variables and
1612 * redo buffer.
1613 */
1614 save_search_patterns();
1615#ifdef FEAT_INS_EXPAND
1616 if (!ins_compl_active())
1617#endif
1618 {
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001619 saveRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001620 did_save_redo = TRUE;
1621 }
1622 ++fp->uf_calls;
1623 call_user_func(fp, argcount, argvars, rettv,
1624 firstline, lastline,
1625 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001626 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001627 /* Function was unreferenced while being used, free it
1628 * now. */
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001629 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001630 if (did_save_redo)
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001631 restoreRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001632 restore_search_patterns();
1633 error = ERROR_NONE;
1634 }
1635 }
1636 }
1637 else
1638 {
1639 /*
1640 * Find the function name in the table, call its implementation.
1641 */
1642 error = call_internal_func(fname, argcount, argvars, rettv);
1643 }
1644 /*
1645 * The function call (or "FuncUndefined" autocommand sequence) might
1646 * have been aborted by an error, an interrupt, or an explicitly thrown
1647 * exception that has not been caught so far. This situation can be
1648 * tested for by calling aborting(). For an error in an internal
1649 * function or for the "E132" error in call_user_func(), however, the
1650 * throw point at which the "force_abort" flag (temporarily reset by
1651 * emsg()) is normally updated has not been reached yet. We need to
1652 * update that flag first to make aborting() reliable.
1653 */
1654 update_force_abort();
1655 }
1656 if (error == ERROR_NONE)
1657 ret = OK;
1658
1659 /*
1660 * Report an error unless the argument evaluation or function call has been
1661 * cancelled due to an aborting error, an interrupt, or an exception.
1662 */
1663 if (!aborting())
1664 {
1665 switch (error)
1666 {
1667 case ERROR_UNKNOWN:
1668 emsg_funcname(N_("E117: Unknown function: %s"), name);
1669 break;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001670 case ERROR_DELETED:
1671 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1672 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001673 case ERROR_TOOMANY:
1674 emsg_funcname((char *)e_toomanyarg, name);
1675 break;
1676 case ERROR_TOOFEW:
1677 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
1678 name);
1679 break;
1680 case ERROR_SCRIPT:
1681 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
1682 name);
1683 break;
1684 case ERROR_DICT:
1685 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
1686 name);
1687 break;
1688 }
1689 }
1690
1691 while (argv_clear > 0)
1692 clear_tv(&argv[--argv_clear]);
1693 vim_free(tofree);
1694 vim_free(name);
1695
1696 return ret;
1697}
1698
1699/*
1700 * List the head of the function: "name(arg1, arg2)".
1701 */
1702 static void
1703list_func_head(ufunc_T *fp, int indent)
1704{
1705 int j;
1706
1707 msg_start();
1708 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001709 msg_puts(" ");
1710 msg_puts("function ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001711 if (fp->uf_name[0] == K_SPECIAL)
1712 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001713 msg_puts_attr("<SNR>", HL_ATTR(HLF_8));
1714 msg_puts((char *)fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001715 }
1716 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001717 msg_puts((char *)fp->uf_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 msg_putchar('(');
1719 for (j = 0; j < fp->uf_args.ga_len; ++j)
1720 {
1721 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001722 msg_puts(", ");
1723 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001724 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1725 {
1726 msg_puts(" = ");
1727 msg_puts(((char **)(fp->uf_def_args.ga_data))
1728 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1729 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001730 }
1731 if (fp->uf_varargs)
1732 {
1733 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001734 msg_puts(", ");
1735 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 }
1737 msg_putchar(')');
1738 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001739 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001740 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001741 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001742 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001743 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001744 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001745 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 msg_clr_eos();
1747 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001748 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001749}
1750
1751/*
1752 * Get a function name, translating "<SID>" and "<SNR>".
1753 * Also handles a Funcref in a List or Dictionary.
1754 * Returns the function name in allocated memory, or NULL for failure.
1755 * flags:
1756 * TFN_INT: internal function name OK
1757 * TFN_QUIET: be quiet
1758 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001759 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 * Advances "pp" to just after the function name (if no error).
1761 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001762 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001763trans_function_name(
1764 char_u **pp,
1765 int skip, /* only find the end, don't evaluate */
1766 int flags,
1767 funcdict_T *fdp, /* return: info about dictionary used */
1768 partial_T **partial) /* return: partial of a FuncRef */
1769{
1770 char_u *name = NULL;
1771 char_u *start;
1772 char_u *end;
1773 int lead;
1774 char_u sid_buf[20];
1775 int len;
1776 lval_T lv;
1777
1778 if (fdp != NULL)
1779 vim_memset(fdp, 0, sizeof(funcdict_T));
1780 start = *pp;
1781
1782 /* Check for hard coded <SNR>: already translated function ID (from a user
1783 * command). */
1784 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1785 && (*pp)[2] == (int)KE_SNR)
1786 {
1787 *pp += 3;
1788 len = get_id_len(pp) + 3;
1789 return vim_strnsave(start, len);
1790 }
1791
1792 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
1793 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
1794 lead = eval_fname_script(start);
1795 if (lead > 2)
1796 start += lead;
1797
1798 /* Note that TFN_ flags use the same values as GLV_ flags. */
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001799 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001800 lead > 2 ? 0 : FNE_CHECK_START);
1801 if (end == start)
1802 {
1803 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001804 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001805 goto theend;
1806 }
1807 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1808 {
1809 /*
1810 * Report an invalid expression in braces, unless the expression
1811 * evaluation has been cancelled due to an aborting error, an
1812 * interrupt, or an exception.
1813 */
1814 if (!aborting())
1815 {
1816 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001817 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001818 }
1819 else
1820 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1821 goto theend;
1822 }
1823
1824 if (lv.ll_tv != NULL)
1825 {
1826 if (fdp != NULL)
1827 {
1828 fdp->fd_dict = lv.ll_dict;
1829 fdp->fd_newkey = lv.ll_newkey;
1830 lv.ll_newkey = NULL;
1831 fdp->fd_di = lv.ll_di;
1832 }
1833 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1834 {
1835 name = vim_strsave(lv.ll_tv->vval.v_string);
1836 *pp = end;
1837 }
1838 else if (lv.ll_tv->v_type == VAR_PARTIAL
1839 && lv.ll_tv->vval.v_partial != NULL)
1840 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001841 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001842 *pp = end;
1843 if (partial != NULL)
1844 *partial = lv.ll_tv->vval.v_partial;
1845 }
1846 else
1847 {
1848 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1849 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001850 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001851 else
1852 *pp = end;
1853 name = NULL;
1854 }
1855 goto theend;
1856 }
1857
1858 if (lv.ll_name == NULL)
1859 {
1860 /* Error found, but continue after the function name. */
1861 *pp = end;
1862 goto theend;
1863 }
1864
1865 /* Check if the name is a Funcref. If so, use the value. */
1866 if (lv.ll_exp_name != NULL)
1867 {
1868 len = (int)STRLEN(lv.ll_exp_name);
1869 name = deref_func_name(lv.ll_exp_name, &len, partial,
1870 flags & TFN_NO_AUTOLOAD);
1871 if (name == lv.ll_exp_name)
1872 name = NULL;
1873 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001874 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001875 {
1876 len = (int)(end - *pp);
1877 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1878 if (name == *pp)
1879 name = NULL;
1880 }
1881 if (name != NULL)
1882 {
1883 name = vim_strsave(name);
1884 *pp = end;
1885 if (STRNCMP(name, "<SNR>", 5) == 0)
1886 {
1887 /* Change "<SNR>" to the byte sequence. */
1888 name[0] = K_SPECIAL;
1889 name[1] = KS_EXTRA;
1890 name[2] = (int)KE_SNR;
1891 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1892 }
1893 goto theend;
1894 }
1895
1896 if (lv.ll_exp_name != NULL)
1897 {
1898 len = (int)STRLEN(lv.ll_exp_name);
1899 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1900 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1901 {
1902 /* When there was "s:" already or the name expanded to get a
1903 * leading "s:" then remove it. */
1904 lv.ll_name += 2;
1905 len -= 2;
1906 lead = 2;
1907 }
1908 }
1909 else
1910 {
1911 /* skip over "s:" and "g:" */
1912 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1913 lv.ll_name += 2;
1914 len = (int)(end - lv.ll_name);
1915 }
1916
1917 /*
1918 * Copy the function name to allocated memory.
1919 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1920 * Accept <SNR>123_name() outside a script.
1921 */
1922 if (skip)
1923 lead = 0; /* do nothing */
1924 else if (lead > 0)
1925 {
1926 lead = 3;
1927 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1928 || eval_fname_sid(*pp))
1929 {
1930 /* It's "s:" or "<SID>" */
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001931 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001933 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001934 goto theend;
1935 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001936 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001937 lead += (int)STRLEN(sid_buf);
1938 }
1939 }
1940 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1941 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001942 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001943 start);
1944 goto theend;
1945 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001946 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001947 {
1948 char_u *cp = vim_strchr(lv.ll_name, ':');
1949
1950 if (cp != NULL && cp < end)
1951 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001952 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001953 goto theend;
1954 }
1955 }
1956
Bram Moolenaar964b3742019-05-24 18:54:09 +02001957 name = alloc(len + lead + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001958 if (name != NULL)
1959 {
1960 if (lead > 0)
1961 {
1962 name[0] = K_SPECIAL;
1963 name[1] = KS_EXTRA;
1964 name[2] = (int)KE_SNR;
1965 if (lead > 3) /* If it's "<SID>" */
1966 STRCPY(name + 3, sid_buf);
1967 }
1968 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1969 name[lead + len] = NUL;
1970 }
1971 *pp = end;
1972
1973theend:
1974 clear_lval(&lv);
1975 return name;
1976}
1977
1978/*
1979 * ":function"
1980 */
1981 void
1982ex_function(exarg_T *eap)
1983{
1984 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02001985 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986 int j;
1987 int c;
1988 int saved_did_emsg;
1989 int saved_wait_return = need_wait_return;
1990 char_u *name = NULL;
1991 char_u *p;
1992 char_u *arg;
1993 char_u *line_arg = NULL;
1994 garray_T newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001995 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001996 garray_T newlines;
1997 int varargs = FALSE;
1998 int flags = 0;
1999 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002000 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002001 int indent;
2002 int nesting;
2003 char_u *skip_until = NULL;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002004 char_u *trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002005 dictitem_T *v;
2006 funcdict_T fudi;
2007 static int func_nr = 0; /* number for nameless function */
2008 int paren;
2009 hashtab_T *ht;
2010 int todo;
2011 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002012 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002013 linenr_T sourcing_lnum_off;
2014 linenr_T sourcing_lnum_top;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002015
2016 /*
2017 * ":function" without argument: list functions.
2018 */
2019 if (ends_excmd(*eap->arg))
2020 {
2021 if (!eap->skip)
2022 {
2023 todo = (int)func_hashtab.ht_used;
2024 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2025 {
2026 if (!HASHITEM_EMPTY(hi))
2027 {
2028 --todo;
2029 fp = HI2UF(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02002030 if (message_filtered(fp->uf_name))
2031 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002032 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002033 list_func_head(fp, FALSE);
2034 }
2035 }
2036 }
2037 eap->nextcmd = check_nextcmd(eap->arg);
2038 return;
2039 }
2040
2041 /*
2042 * ":function /pat": list functions matching pattern.
2043 */
2044 if (*eap->arg == '/')
2045 {
2046 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2047 if (!eap->skip)
2048 {
2049 regmatch_T regmatch;
2050
2051 c = *p;
2052 *p = NUL;
2053 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2054 *p = c;
2055 if (regmatch.regprog != NULL)
2056 {
2057 regmatch.rm_ic = p_ic;
2058
2059 todo = (int)func_hashtab.ht_used;
2060 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2061 {
2062 if (!HASHITEM_EMPTY(hi))
2063 {
2064 --todo;
2065 fp = HI2UF(hi);
2066 if (!isdigit(*fp->uf_name)
2067 && vim_regexec(&regmatch, fp->uf_name, 0))
2068 list_func_head(fp, FALSE);
2069 }
2070 }
2071 vim_regfree(regmatch.regprog);
2072 }
2073 }
2074 if (*p == '/')
2075 ++p;
2076 eap->nextcmd = check_nextcmd(p);
2077 return;
2078 }
2079
2080 /*
2081 * Get the function name. There are these situations:
2082 * func normal function name
2083 * "name" == func, "fudi.fd_dict" == NULL
2084 * dict.func new dictionary entry
2085 * "name" == NULL, "fudi.fd_dict" set,
2086 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2087 * dict.func existing dict entry with a Funcref
2088 * "name" == func, "fudi.fd_dict" set,
2089 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2090 * dict.func existing dict entry that's not a Funcref
2091 * "name" == NULL, "fudi.fd_dict" set,
2092 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2093 * s:func script-local function name
2094 * g:func global function name, same as "func"
2095 */
2096 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002097 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002098 paren = (vim_strchr(p, '(') != NULL);
2099 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2100 {
2101 /*
2102 * Return on an invalid expression in braces, unless the expression
2103 * evaluation has been cancelled due to an aborting error, an
2104 * interrupt, or an exception.
2105 */
2106 if (!aborting())
2107 {
2108 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002109 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002110 vim_free(fudi.fd_newkey);
2111 return;
2112 }
2113 else
2114 eap->skip = TRUE;
2115 }
2116
2117 /* An error in a function call during evaluation of an expression in magic
2118 * braces should not cause the function not to be defined. */
2119 saved_did_emsg = did_emsg;
2120 did_emsg = FALSE;
2121
2122 /*
2123 * ":function func" with only function name: list function.
2124 */
2125 if (!paren)
2126 {
2127 if (!ends_excmd(*skipwhite(p)))
2128 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002129 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002130 goto ret_free;
2131 }
2132 eap->nextcmd = check_nextcmd(p);
2133 if (eap->nextcmd != NULL)
2134 *p = NUL;
2135 if (!eap->skip && !got_int)
2136 {
2137 fp = find_func(name);
2138 if (fp != NULL)
2139 {
2140 list_func_head(fp, TRUE);
2141 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2142 {
2143 if (FUNCLINE(fp, j) == NULL)
2144 continue;
2145 msg_putchar('\n');
2146 msg_outnum((long)(j + 1));
2147 if (j < 9)
2148 msg_putchar(' ');
2149 if (j < 99)
2150 msg_putchar(' ');
2151 msg_prt_line(FUNCLINE(fp, j), FALSE);
2152 out_flush(); /* show a line at a time */
2153 ui_breakcheck();
2154 }
2155 if (!got_int)
2156 {
2157 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01002158 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159 }
2160 }
2161 else
2162 emsg_funcname(N_("E123: Undefined function: %s"), name);
2163 }
2164 goto ret_free;
2165 }
2166
2167 /*
2168 * ":function name(arg1, arg2)" Define function.
2169 */
2170 p = skipwhite(p);
2171 if (*p != '(')
2172 {
2173 if (!eap->skip)
2174 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002175 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002176 goto ret_free;
2177 }
2178 /* attempt to continue by skipping some text */
2179 if (vim_strchr(p, '(') != NULL)
2180 p = vim_strchr(p, '(');
2181 }
2182 p = skipwhite(p + 1);
2183
2184 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2185
2186 if (!eap->skip)
2187 {
2188 /* Check the name of the function. Unless it's a dictionary function
2189 * (that we are overwriting). */
2190 if (name != NULL)
2191 arg = name;
2192 else
2193 arg = fudi.fd_newkey;
2194 if (arg != NULL && (fudi.fd_di == NULL
2195 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2196 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2197 {
2198 if (*arg == K_SPECIAL)
2199 j = 3;
2200 else
2201 j = 0;
2202 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2203 : eval_isnamec(arg[j])))
2204 ++j;
2205 if (arg[j] != NUL)
2206 emsg_funcname((char *)e_invarg2, arg);
2207 }
2208 /* Disallow using the g: dict. */
2209 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002210 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 }
2212
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002213 if (get_function_args(&p, ')', &newargs, &varargs,
2214 &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002215 goto errret_2;
2216
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002217 /* find extra arguments "range", "dict", "abort" and "closure" */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002218 for (;;)
2219 {
2220 p = skipwhite(p);
2221 if (STRNCMP(p, "range", 5) == 0)
2222 {
2223 flags |= FC_RANGE;
2224 p += 5;
2225 }
2226 else if (STRNCMP(p, "dict", 4) == 0)
2227 {
2228 flags |= FC_DICT;
2229 p += 4;
2230 }
2231 else if (STRNCMP(p, "abort", 5) == 0)
2232 {
2233 flags |= FC_ABORT;
2234 p += 5;
2235 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002236 else if (STRNCMP(p, "closure", 7) == 0)
2237 {
2238 flags |= FC_CLOSURE;
2239 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02002240 if (current_funccal == NULL)
2241 {
Bram Moolenaarba209902016-08-24 22:06:38 +02002242 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
Bram Moolenaar58016442016-07-31 18:30:22 +02002243 name == NULL ? (char_u *)"" : name);
2244 goto erret;
2245 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002246 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002247 else
2248 break;
2249 }
2250
2251 /* When there is a line break use what follows for the function body.
2252 * Makes 'exe "func Test()\n...\nendfunc"' work. */
2253 if (*p == '\n')
2254 line_arg = p + 1;
2255 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002256 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002257
2258 /*
2259 * Read the body of the function, until ":endfunction" is found.
2260 */
2261 if (KeyTyped)
2262 {
2263 /* Check if the function already exists, don't let the user type the
2264 * whole function before telling him it doesn't work! For a script we
2265 * need to skip the body to be able to find what follows. */
2266 if (!eap->skip && !eap->forceit)
2267 {
2268 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002269 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002270 else if (name != NULL && find_func(name) != NULL)
2271 emsg_funcname(e_funcexts, name);
2272 }
2273
2274 if (!eap->skip && did_emsg)
2275 goto erret;
2276
2277 msg_putchar('\n'); /* don't overwrite the function name */
2278 cmdline_row = msg_row;
2279 }
2280
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002281 // Save the starting line number.
2282 sourcing_lnum_top = sourcing_lnum;
2283
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002284 indent = 2;
2285 nesting = 0;
2286 for (;;)
2287 {
2288 if (KeyTyped)
2289 {
2290 msg_scroll = TRUE;
2291 saved_wait_return = FALSE;
2292 }
2293 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002294
2295 if (line_arg != NULL)
2296 {
2297 /* Use eap->arg, split up in parts by line breaks. */
2298 theline = line_arg;
2299 p = vim_strchr(theline, '\n');
2300 if (p == NULL)
2301 line_arg += STRLEN(line_arg);
2302 else
2303 {
2304 *p = NUL;
2305 line_arg = p + 1;
2306 }
2307 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002308 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002309 {
2310 vim_free(line_to_free);
2311 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002312 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002313 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002314 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002315 line_to_free = theline;
2316 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002317 if (KeyTyped)
2318 lines_left = Rows - 1;
2319 if (theline == NULL)
2320 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002321 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002322 goto erret;
2323 }
2324
2325 /* Detect line continuation: sourcing_lnum increased more than one. */
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002326 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
2327 if (sourcing_lnum < sourcing_lnum_off)
2328 sourcing_lnum_off -= sourcing_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 else
2330 sourcing_lnum_off = 0;
2331
2332 if (skip_until != NULL)
2333 {
Bram Moolenaar8471e572019-05-19 21:37:18 +02002334 // Between ":append" and "." and between ":python <<EOF" and "EOF"
2335 // don't check for ":endfunc".
2336 if (trimmed == NULL
2337 || STRNCMP(theline, trimmed, STRLEN(trimmed)) == 0)
2338 {
2339 p = trimmed == NULL ? theline : theline + STRLEN(trimmed);
2340 if (STRCMP(p, skip_until) == 0)
2341 {
2342 VIM_CLEAR(skip_until);
2343 VIM_CLEAR(trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002344 do_concat = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002345 }
2346 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002347 }
2348 else
2349 {
2350 /* skip ':' and blanks*/
Bram Moolenaar1c465442017-03-12 20:10:05 +01002351 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002352 ;
2353
2354 /* Check for "endfunction". */
2355 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2356 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002357 char_u *nextcmd = NULL;
2358
Bram Moolenaar663bb232017-06-22 19:12:10 +02002359 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002360 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002361 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002362 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002363 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002364 give_warning2(
2365 (char_u *)_("W22: Text found after :endfunction: %s"),
2366 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002367 if (nextcmd != NULL)
2368 {
2369 /* Another command follows. If the line came from "eap" we
2370 * can simply point into it, otherwise we need to change
2371 * "eap->cmdlinep". */
2372 eap->nextcmd = nextcmd;
2373 if (line_to_free != NULL)
2374 {
2375 vim_free(*eap->cmdlinep);
2376 *eap->cmdlinep = line_to_free;
2377 line_to_free = NULL;
2378 }
2379 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002380 break;
2381 }
2382
2383 /* Increase indent inside "if", "while", "for" and "try", decrease
2384 * at "end". */
2385 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2386 indent -= 2;
2387 else if (STRNCMP(p, "if", 2) == 0
2388 || STRNCMP(p, "wh", 2) == 0
2389 || STRNCMP(p, "for", 3) == 0
2390 || STRNCMP(p, "try", 3) == 0)
2391 indent += 2;
2392
2393 /* Check for defining a function inside this function. */
2394 if (checkforcmd(&p, "function", 2))
2395 {
2396 if (*p == '!')
2397 p = skipwhite(p + 1);
2398 p += eval_fname_script(p);
2399 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2400 if (*skipwhite(p) == '(')
2401 {
2402 ++nesting;
2403 indent += 2;
2404 }
2405 }
2406
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002407 /* Check for ":append", ":change", ":insert". */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002408 p = skip_range(p, NULL);
2409 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002410 || (p[0] == 'c'
2411 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2412 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2413 && (STRNCMP(&p[3], "nge", 3) != 0
2414 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002415 || (p[0] == 'i'
2416 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2417 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2418 skip_until = vim_strsave((char_u *)".");
2419
2420 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
2421 arg = skipwhite(skiptowhite(p));
2422 if (arg[0] == '<' && arg[1] =='<'
2423 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002424 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2425 || ((p[2] == '3' || p[2] == 'x')
2426 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002427 || (p[0] == 'p' && p[1] == 'e'
2428 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2429 || (p[0] == 't' && p[1] == 'c'
2430 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2431 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2432 && !ASCII_ISALPHA(p[3]))
2433 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2434 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2435 || (p[0] == 'm' && p[1] == 'z'
2436 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2437 ))
2438 {
2439 /* ":python <<" continues until a dot, like ":append" */
2440 p = skipwhite(arg + 2);
2441 if (*p == NUL)
2442 skip_until = vim_strsave((char_u *)".");
2443 else
2444 skip_until = vim_strsave(p);
2445 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002446
2447 // Check for ":let v =<< [trim] EOF"
2448 arg = skipwhite(skiptowhite(p));
2449 arg = skipwhite(skiptowhite(arg));
2450 if (arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2451 && ((p[0] == 'l'
2452 && p[1] == 'e'
2453 && (!ASCII_ISALNUM(p[2])
2454 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
2455 {
2456 // ":let v =<<" continues until a dot
2457 p = skipwhite(arg + 3);
2458 if (STRNCMP(p, "trim", 4) == 0)
2459 {
2460 // Ignore leading white space.
2461 p = skipwhite(p + 4);
2462 trimmed = vim_strnsave(theline,
2463 (int)(skipwhite(theline) - theline));
2464 }
2465 if (*p == NUL)
2466 skip_until = vim_strsave((char_u *)".");
2467 else
2468 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
Bram Moolenaare96a2492019-06-25 04:12:16 +02002469 do_concat = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002470 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002471 }
2472
2473 /* Add the line to the function. */
2474 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002475 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002476
2477 /* Copy the line to newly allocated memory. get_one_sourceline()
2478 * allocates 250 bytes per line, this saves 80% on average. The cost
2479 * is an extra alloc/free. */
2480 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002481 if (p == NULL)
2482 goto erret;
2483 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002484
2485 /* Add NULL lines for continuation lines, so that the line count is
2486 * equal to the index in the growarray. */
2487 while (sourcing_lnum_off-- > 0)
2488 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2489
2490 /* Check for end of eap->arg. */
2491 if (line_arg != NULL && *line_arg == NUL)
2492 line_arg = NULL;
2493 }
2494
2495 /* Don't define the function when skipping commands or when an error was
2496 * detected. */
2497 if (eap->skip || did_emsg)
2498 goto erret;
2499
2500 /*
2501 * If there are no errors, add the function
2502 */
2503 if (fudi.fd_dict == NULL)
2504 {
2505 v = find_var(name, &ht, FALSE);
2506 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2507 {
2508 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2509 name);
2510 goto erret;
2511 }
2512
2513 fp = find_func(name);
2514 if (fp != NULL)
2515 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002516 // Function can be replaced with "function!" and when sourcing the
2517 // same script again, but only once.
2518 if (!eap->forceit
2519 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2520 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002521 {
2522 emsg_funcname(e_funcexts, name);
2523 goto erret;
2524 }
2525 if (fp->uf_calls > 0)
2526 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002527 emsg_funcname(
2528 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002529 name);
2530 goto erret;
2531 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002532 if (fp->uf_refcount > 1)
2533 {
2534 /* This function is referenced somewhere, don't redefine it but
2535 * create a new one. */
2536 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002537 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002538 fp = NULL;
2539 overwrite = TRUE;
2540 }
2541 else
2542 {
2543 /* redefine existing function */
Bram Moolenaard23a8232018-02-10 18:45:26 +01002544 VIM_CLEAR(name);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002545 func_clear_items(fp);
2546#ifdef FEAT_PROFILE
2547 fp->uf_profiling = FALSE;
2548 fp->uf_prof_initialized = FALSE;
2549#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002550 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002551 }
2552 }
2553 else
2554 {
2555 char numbuf[20];
2556
2557 fp = NULL;
2558 if (fudi.fd_newkey == NULL && !eap->forceit)
2559 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002560 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002561 goto erret;
2562 }
2563 if (fudi.fd_di == NULL)
2564 {
2565 /* Can't add a function to a locked dictionary */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002566 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002567 goto erret;
2568 }
2569 /* Can't change an existing function if it is locked */
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002570 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002571 goto erret;
2572
2573 /* Give the function a sequential number. Can only be used with a
2574 * Funcref! */
2575 vim_free(name);
2576 sprintf(numbuf, "%d", ++func_nr);
2577 name = vim_strsave((char_u *)numbuf);
2578 if (name == NULL)
2579 goto erret;
2580 }
2581
2582 if (fp == NULL)
2583 {
2584 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2585 {
2586 int slen, plen;
2587 char_u *scriptname;
2588
2589 /* Check that the autoload name matches the script name. */
2590 j = FAIL;
2591 if (sourcing_name != NULL)
2592 {
2593 scriptname = autoload_name(name);
2594 if (scriptname != NULL)
2595 {
2596 p = vim_strchr(scriptname, '/');
2597 plen = (int)STRLEN(p);
2598 slen = (int)STRLEN(sourcing_name);
2599 if (slen > plen && fnamecmp(p,
2600 sourcing_name + slen - plen) == 0)
2601 j = OK;
2602 vim_free(scriptname);
2603 }
2604 }
2605 if (j == FAIL)
2606 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002607 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002608 goto erret;
2609 }
2610 }
2611
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002612 fp = alloc_clear(sizeof(ufunc_T) + STRLEN(name));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002613 if (fp == NULL)
2614 goto erret;
2615
2616 if (fudi.fd_dict != NULL)
2617 {
2618 if (fudi.fd_di == NULL)
2619 {
2620 /* add new dict entry */
2621 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2622 if (fudi.fd_di == NULL)
2623 {
2624 vim_free(fp);
2625 goto erret;
2626 }
2627 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2628 {
2629 vim_free(fudi.fd_di);
2630 vim_free(fp);
2631 goto erret;
2632 }
2633 }
2634 else
2635 /* overwrite existing dict entry */
2636 clear_tv(&fudi.fd_di->di_tv);
2637 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002639
2640 /* behave like "dict" was used */
2641 flags |= FC_DICT;
2642 }
2643
2644 /* insert the new function in the function list */
2645 STRCPY(fp->uf_name, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002646 if (overwrite)
2647 {
2648 hi = hash_find(&func_hashtab, name);
2649 hi->hi_key = UF2HIKEY(fp);
2650 }
2651 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002652 {
2653 vim_free(fp);
2654 goto erret;
2655 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002656 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002657 }
2658 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002659 fp->uf_def_args = default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002660 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002661 if ((flags & FC_CLOSURE) != 0)
2662 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002663 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002664 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002665 }
2666 else
2667 fp->uf_scoped = NULL;
2668
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002669#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002670 if (prof_def_func())
2671 func_do_profile(fp);
2672#endif
2673 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02002674 if (sandbox)
2675 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002676 fp->uf_flags = flags;
2677 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002678 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002679 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 goto ret_free;
2681
2682erret:
2683 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002684 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002685errret_2:
2686 ga_clear_strings(&newlines);
2687ret_free:
2688 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002689 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002690 vim_free(fudi.fd_newkey);
2691 vim_free(name);
2692 did_emsg |= saved_did_emsg;
2693 need_wait_return |= saved_wait_return;
2694}
2695
2696/*
2697 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2698 * Return 2 if "p" starts with "s:".
2699 * Return 0 otherwise.
2700 */
2701 int
2702eval_fname_script(char_u *p)
2703{
2704 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2705 * the standard library function. */
2706 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2707 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2708 return 5;
2709 if (p[0] == 's' && p[1] == ':')
2710 return 2;
2711 return 0;
2712}
2713
2714 int
2715translated_function_exists(char_u *name)
2716{
2717 if (builtin_function(name, -1))
2718 return find_internal_func(name) >= 0;
2719 return find_func(name) != NULL;
2720}
2721
2722/*
2723 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002724 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002725 */
2726 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002727function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728{
2729 char_u *nm = name;
2730 char_u *p;
2731 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002732 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002734 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2735 if (no_deref)
2736 flag |= TFN_NO_DEREF;
2737 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002738 nm = skipwhite(nm);
2739
2740 /* Only accept "funcname", "funcname ", "funcname (..." and
2741 * "funcname(...", not "funcname!...". */
2742 if (p != NULL && (*nm == NUL || *nm == '('))
2743 n = translated_function_exists(p);
2744 vim_free(p);
2745 return n;
2746}
2747
Bram Moolenaar113e1072019-01-20 15:30:40 +01002748#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 char_u *
2750get_expanded_name(char_u *name, int check)
2751{
2752 char_u *nm = name;
2753 char_u *p;
2754
2755 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2756
2757 if (p != NULL && *nm == NUL)
2758 if (!check || translated_function_exists(p))
2759 return p;
2760
2761 vim_free(p);
2762 return NULL;
2763}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002764#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002765
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002766#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
2767
2768/*
2769 * Function given to ExpandGeneric() to obtain the list of user defined
2770 * function names.
2771 */
2772 char_u *
2773get_user_func_name(expand_T *xp, int idx)
2774{
2775 static long_u done;
2776 static hashitem_T *hi;
2777 ufunc_T *fp;
2778
2779 if (idx == 0)
2780 {
2781 done = 0;
2782 hi = func_hashtab.ht_array;
2783 }
2784 if (done < func_hashtab.ht_used)
2785 {
2786 if (done++ > 0)
2787 ++hi;
2788 while (HASHITEM_EMPTY(hi))
2789 ++hi;
2790 fp = HI2UF(hi);
2791
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002792 if ((fp->uf_flags & FC_DICT)
2793 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
2794 return (char_u *)""; /* don't show dict and lambda functions */
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002795
2796 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
2797 return fp->uf_name; /* prevents overflow */
2798
2799 cat_func_name(IObuff, fp);
2800 if (xp->xp_context != EXPAND_USER_FUNC)
2801 {
2802 STRCAT(IObuff, "(");
2803 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2804 STRCAT(IObuff, ")");
2805 }
2806 return IObuff;
2807 }
2808 return NULL;
2809}
2810
2811#endif /* FEAT_CMDL_COMPL */
2812
2813/*
2814 * ":delfunction {name}"
2815 */
2816 void
2817ex_delfunction(exarg_T *eap)
2818{
2819 ufunc_T *fp = NULL;
2820 char_u *p;
2821 char_u *name;
2822 funcdict_T fudi;
2823
2824 p = eap->arg;
2825 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2826 vim_free(fudi.fd_newkey);
2827 if (name == NULL)
2828 {
2829 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002830 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002831 return;
2832 }
2833 if (!ends_excmd(*skipwhite(p)))
2834 {
2835 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002836 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002837 return;
2838 }
2839 eap->nextcmd = check_nextcmd(p);
2840 if (eap->nextcmd != NULL)
2841 *p = NUL;
2842
2843 if (!eap->skip)
2844 fp = find_func(name);
2845 vim_free(name);
2846
2847 if (!eap->skip)
2848 {
2849 if (fp == NULL)
2850 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02002851 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002852 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002853 return;
2854 }
2855 if (fp->uf_calls > 0)
2856 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002857 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002858 return;
2859 }
2860
2861 if (fudi.fd_dict != NULL)
2862 {
2863 /* Delete the dict item that refers to the function, it will
2864 * invoke func_unref() and possibly delete the function. */
2865 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2866 }
2867 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002868 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002869 /* A normal function (not a numbered function or lambda) has a
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002870 * refcount of 1 for the entry in the hashtable. When deleting
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002871 * it and the refcount is more than one, it should be kept.
Bram Moolenaarba209902016-08-24 22:06:38 +02002872 * A numbered function and lambda should be kept if the refcount is
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002873 * one or more. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002874 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002875 {
2876 /* Function is still referenced somewhere. Don't free it but
2877 * do remove it from the hashtable. */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002878 if (func_remove(fp))
2879 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002880 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002881 }
2882 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002883 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002884 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002885 }
2886}
2887
2888/*
2889 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002890 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002891 */
2892 void
2893func_unref(char_u *name)
2894{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002895 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002896
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002897 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002898 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002899 fp = find_func(name);
2900 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002901 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002902#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002903 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002905 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002906 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002907 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002908 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002909 /* Only delete it when it's not being used. Otherwise it's done
2910 * when "uf_calls" becomes zero. */
2911 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002912 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02002913 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002914}
2915
2916/*
2917 * Unreference a Function: decrement the reference count and free it when it
2918 * becomes zero.
2919 */
2920 void
2921func_ptr_unref(ufunc_T *fp)
2922{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002923 if (fp != NULL && --fp->uf_refcount <= 0)
2924 {
2925 /* Only delete it when it's not being used. Otherwise it's done
2926 * when "uf_calls" becomes zero. */
2927 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002928 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002929 }
2930}
2931
2932/*
2933 * Count a reference to a Function.
2934 */
2935 void
2936func_ref(char_u *name)
2937{
2938 ufunc_T *fp;
2939
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002940 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002942 fp = find_func(name);
2943 if (fp != NULL)
2944 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 else if (isdigit(*name))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002946 /* Only give an error for a numbered function.
2947 * Fail silently, when named or lambda function isn't found. */
Bram Moolenaar95f09602016-11-10 20:01:45 +01002948 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002949}
2950
2951/*
2952 * Count a reference to a Function.
2953 */
2954 void
2955func_ptr_ref(ufunc_T *fp)
2956{
2957 if (fp != NULL)
2958 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002959}
2960
2961/*
2962 * Return TRUE if items in "fc" do not have "copyID". That means they are not
2963 * referenced from anywhere that is in use.
2964 */
2965 static int
2966can_free_funccal(funccall_T *fc, int copyID)
2967{
2968 return (fc->l_varlist.lv_copyID != copyID
2969 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02002970 && fc->l_avars.dv_copyID != copyID
2971 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002972}
2973
2974/*
2975 * ":return [expr]"
2976 */
2977 void
2978ex_return(exarg_T *eap)
2979{
2980 char_u *arg = eap->arg;
2981 typval_T rettv;
2982 int returning = FALSE;
2983
2984 if (current_funccal == NULL)
2985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002986 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002987 return;
2988 }
2989
2990 if (eap->skip)
2991 ++emsg_skip;
2992
2993 eap->nextcmd = NULL;
2994 if ((*arg != NUL && *arg != '|' && *arg != '\n')
2995 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
2996 {
2997 if (!eap->skip)
2998 returning = do_return(eap, FALSE, TRUE, &rettv);
2999 else
3000 clear_tv(&rettv);
3001 }
3002 /* It's safer to return also on error. */
3003 else if (!eap->skip)
3004 {
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003005 /* In return statement, cause_abort should be force_abort. */
3006 update_force_abort();
3007
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003008 /*
3009 * Return unless the expression evaluation has been cancelled due to an
3010 * aborting error, an interrupt, or an exception.
3011 */
3012 if (!aborting())
3013 returning = do_return(eap, FALSE, TRUE, NULL);
3014 }
3015
3016 /* When skipping or the return gets pending, advance to the next command
3017 * in this line (!returning). Otherwise, ignore the rest of the line.
3018 * Following lines will be ignored by get_func_line(). */
3019 if (returning)
3020 eap->nextcmd = NULL;
3021 else if (eap->nextcmd == NULL) /* no argument */
3022 eap->nextcmd = check_nextcmd(arg);
3023
3024 if (eap->skip)
3025 --emsg_skip;
3026}
3027
3028/*
3029 * ":1,25call func(arg1, arg2)" function call.
3030 */
3031 void
3032ex_call(exarg_T *eap)
3033{
3034 char_u *arg = eap->arg;
3035 char_u *startarg;
3036 char_u *name;
3037 char_u *tofree;
3038 int len;
3039 typval_T rettv;
3040 linenr_T lnum;
3041 int doesrange;
3042 int failed = FALSE;
3043 funcdict_T fudi;
3044 partial_T *partial = NULL;
3045
3046 if (eap->skip)
3047 {
3048 /* trans_function_name() doesn't work well when skipping, use eval0()
3049 * instead to skip to any following command, e.g. for:
3050 * :if 0 | call dict.foo().bar() | endif */
3051 ++emsg_skip;
3052 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3053 clear_tv(&rettv);
3054 --emsg_skip;
3055 return;
3056 }
3057
3058 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3059 if (fudi.fd_newkey != NULL)
3060 {
3061 /* Still need to give an error message for missing key. */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003062 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003063 vim_free(fudi.fd_newkey);
3064 }
3065 if (tofree == NULL)
3066 return;
3067
3068 /* Increase refcount on dictionary, it could get deleted when evaluating
3069 * the arguments. */
3070 if (fudi.fd_dict != NULL)
3071 ++fudi.fd_dict->dv_refcount;
3072
3073 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3074 * contents. For VAR_PARTIAL get its partial, unless we already have one
3075 * from trans_function_name(). */
3076 len = (int)STRLEN(tofree);
3077 name = deref_func_name(tofree, &len,
3078 partial != NULL ? NULL : &partial, FALSE);
3079
3080 /* Skip white space to allow ":call func ()". Not good, but required for
3081 * backward compatibility. */
3082 startarg = skipwhite(arg);
3083 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
3084
3085 if (*startarg != '(')
3086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003087 semsg(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003088 goto end;
3089 }
3090
3091 /*
3092 * When skipping, evaluate the function once, to find the end of the
3093 * arguments.
3094 * When the function takes a range, this is discovered after the first
3095 * call, and the loop is broken.
3096 */
3097 if (eap->skip)
3098 {
3099 ++emsg_skip;
3100 lnum = eap->line2; /* do it once, also with an invalid range */
3101 }
3102 else
3103 lnum = eap->line1;
3104 for ( ; lnum <= eap->line2; ++lnum)
3105 {
3106 if (!eap->skip && eap->addr_count > 0)
3107 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003108 if (lnum > curbuf->b_ml.ml_line_count)
3109 {
3110 // If the function deleted lines or switched to another buffer
3111 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003112 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003113 break;
3114 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 curwin->w_cursor.lnum = lnum;
3116 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003117 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003118 }
3119 arg = startarg;
Bram Moolenaar6ed88192019-05-11 18:37:44 +02003120 if (get_func_tv(name, -1, &rettv, &arg,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121 eap->line1, eap->line2, &doesrange,
3122 !eap->skip, partial, fudi.fd_dict) == FAIL)
3123 {
3124 failed = TRUE;
3125 break;
3126 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003127 if (has_watchexpr())
3128 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003129
3130 /* Handle a function returning a Funcref, Dictionary or List. */
3131 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3132 {
3133 failed = TRUE;
3134 break;
3135 }
3136
3137 clear_tv(&rettv);
3138 if (doesrange || eap->skip)
3139 break;
3140
3141 /* Stop when immediately aborting on error, or when an interrupt
3142 * occurred or an exception was thrown but not caught.
3143 * get_func_tv() returned OK, so that the check for trailing
3144 * characters below is executed. */
3145 if (aborting())
3146 break;
3147 }
3148 if (eap->skip)
3149 --emsg_skip;
3150
3151 if (!failed)
3152 {
3153 /* Check for trailing illegal characters and a following command. */
3154 if (!ends_excmd(*arg))
3155 {
3156 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003157 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 }
3159 else
3160 eap->nextcmd = check_nextcmd(arg);
3161 }
3162
3163end:
3164 dict_unref(fudi.fd_dict);
3165 vim_free(tofree);
3166}
3167
3168/*
3169 * Return from a function. Possibly makes the return pending. Also called
3170 * for a pending return at the ":endtry" or after returning from an extra
3171 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3172 * when called due to a ":return" command. "rettv" may point to a typval_T
3173 * with the return rettv. Returns TRUE when the return can be carried out,
3174 * FALSE when the return gets pending.
3175 */
3176 int
3177do_return(
3178 exarg_T *eap,
3179 int reanimate,
3180 int is_cmd,
3181 void *rettv)
3182{
3183 int idx;
3184 struct condstack *cstack = eap->cstack;
3185
3186 if (reanimate)
3187 /* Undo the return. */
3188 current_funccal->returned = FALSE;
3189
3190 /*
3191 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3192 * not in its finally clause (which then is to be executed next) is found.
3193 * In this case, make the ":return" pending for execution at the ":endtry".
3194 * Otherwise, return normally.
3195 */
3196 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3197 if (idx >= 0)
3198 {
3199 cstack->cs_pending[idx] = CSTP_RETURN;
3200
3201 if (!is_cmd && !reanimate)
3202 /* A pending return again gets pending. "rettv" points to an
3203 * allocated variable with the rettv of the original ":return"'s
3204 * argument if present or is NULL else. */
3205 cstack->cs_rettv[idx] = rettv;
3206 else
3207 {
3208 /* When undoing a return in order to make it pending, get the stored
3209 * return rettv. */
3210 if (reanimate)
3211 rettv = current_funccal->rettv;
3212
3213 if (rettv != NULL)
3214 {
3215 /* Store the value of the pending return. */
3216 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3217 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3218 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003219 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003220 }
3221 else
3222 cstack->cs_rettv[idx] = NULL;
3223
3224 if (reanimate)
3225 {
3226 /* The pending return value could be overwritten by a ":return"
3227 * without argument in a finally clause; reset the default
3228 * return value. */
3229 current_funccal->rettv->v_type = VAR_NUMBER;
3230 current_funccal->rettv->vval.v_number = 0;
3231 }
3232 }
3233 report_make_pending(CSTP_RETURN, rettv);
3234 }
3235 else
3236 {
3237 current_funccal->returned = TRUE;
3238
3239 /* If the return is carried out now, store the return value. For
3240 * a return immediately after reanimation, the value is already
3241 * there. */
3242 if (!reanimate && rettv != NULL)
3243 {
3244 clear_tv(current_funccal->rettv);
3245 *current_funccal->rettv = *(typval_T *)rettv;
3246 if (!is_cmd)
3247 vim_free(rettv);
3248 }
3249 }
3250
3251 return idx < 0;
3252}
3253
3254/*
3255 * Free the variable with a pending return value.
3256 */
3257 void
3258discard_pending_return(void *rettv)
3259{
3260 free_tv((typval_T *)rettv);
3261}
3262
3263/*
3264 * Generate a return command for producing the value of "rettv". The result
3265 * is an allocated string. Used by report_pending() for verbose messages.
3266 */
3267 char_u *
3268get_return_cmd(void *rettv)
3269{
3270 char_u *s = NULL;
3271 char_u *tofree = NULL;
3272 char_u numbuf[NUMBUFLEN];
3273
3274 if (rettv != NULL)
3275 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3276 if (s == NULL)
3277 s = (char_u *)"";
3278
3279 STRCPY(IObuff, ":return ");
3280 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3281 if (STRLEN(s) + 8 >= IOSIZE)
3282 STRCPY(IObuff + IOSIZE - 4, "...");
3283 vim_free(tofree);
3284 return vim_strsave(IObuff);
3285}
3286
3287/*
3288 * Get next function line.
3289 * Called by do_cmdline() to get the next line.
3290 * Returns allocated string, or NULL for end of function.
3291 */
3292 char_u *
3293get_func_line(
3294 int c UNUSED,
3295 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003296 int indent UNUSED,
3297 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003298{
3299 funccall_T *fcp = (funccall_T *)cookie;
3300 ufunc_T *fp = fcp->func;
3301 char_u *retval;
3302 garray_T *gap; /* growarray with function lines */
3303
3304 /* If breakpoints have been added/deleted need to check for it. */
3305 if (fcp->dbg_tick != debug_tick)
3306 {
3307 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3308 sourcing_lnum);
3309 fcp->dbg_tick = debug_tick;
3310 }
3311#ifdef FEAT_PROFILE
3312 if (do_profiling == PROF_YES)
3313 func_line_end(cookie);
3314#endif
3315
3316 gap = &fp->uf_lines;
3317 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3318 || fcp->returned)
3319 retval = NULL;
3320 else
3321 {
3322 /* Skip NULL lines (continuation lines). */
3323 while (fcp->linenr < gap->ga_len
3324 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3325 ++fcp->linenr;
3326 if (fcp->linenr >= gap->ga_len)
3327 retval = NULL;
3328 else
3329 {
3330 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
3331 sourcing_lnum = fcp->linenr;
3332#ifdef FEAT_PROFILE
3333 if (do_profiling == PROF_YES)
3334 func_line_start(cookie);
3335#endif
3336 }
3337 }
3338
3339 /* Did we encounter a breakpoint? */
3340 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
3341 {
3342 dbg_breakpoint(fp->uf_name, sourcing_lnum);
3343 /* Find next breakpoint. */
3344 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3345 sourcing_lnum);
3346 fcp->dbg_tick = debug_tick;
3347 }
3348
3349 return retval;
3350}
3351
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003352/*
3353 * Return TRUE if the currently active function should be ended, because a
3354 * return was encountered or an error occurred. Used inside a ":while".
3355 */
3356 int
3357func_has_ended(void *cookie)
3358{
3359 funccall_T *fcp = (funccall_T *)cookie;
3360
3361 /* Ignore the "abort" flag if the abortion behavior has been changed due to
3362 * an error inside a try conditional. */
3363 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3364 || fcp->returned);
3365}
3366
3367/*
3368 * return TRUE if cookie indicates a function which "abort"s on errors.
3369 */
3370 int
3371func_has_abort(
3372 void *cookie)
3373{
3374 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3375}
3376
3377
3378/*
3379 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3380 * Don't do this when "Func" is already a partial that was bound
3381 * explicitly (pt_auto is FALSE).
3382 * Changes "rettv" in-place.
3383 * Returns the updated "selfdict_in".
3384 */
3385 dict_T *
3386make_partial(dict_T *selfdict_in, typval_T *rettv)
3387{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003388 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003389 char_u *tofree = NULL;
3390 ufunc_T *fp;
3391 char_u fname_buf[FLEN_FIXED + 1];
3392 int error;
3393 dict_T *selfdict = selfdict_in;
3394
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003395 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3396 fp = rettv->vval.v_partial->pt_func;
3397 else
3398 {
3399 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3400 : rettv->vval.v_partial->pt_name;
3401 /* Translate "s:func" to the stored function name. */
3402 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3403 fp = find_func(fname);
3404 vim_free(tofree);
3405 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003406
3407 if (fp != NULL && (fp->uf_flags & FC_DICT))
3408 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003409 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003410
3411 if (pt != NULL)
3412 {
3413 pt->pt_refcount = 1;
3414 pt->pt_dict = selfdict;
3415 pt->pt_auto = TRUE;
3416 selfdict = NULL;
3417 if (rettv->v_type == VAR_FUNC)
3418 {
3419 /* Just a function: Take over the function name and use
3420 * selfdict. */
3421 pt->pt_name = rettv->vval.v_string;
3422 }
3423 else
3424 {
3425 partial_T *ret_pt = rettv->vval.v_partial;
3426 int i;
3427
3428 /* Partial: copy the function name, use selfdict and copy
3429 * args. Can't take over name or args, the partial might
3430 * be referenced elsewhere. */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003431 if (ret_pt->pt_name != NULL)
3432 {
3433 pt->pt_name = vim_strsave(ret_pt->pt_name);
3434 func_ref(pt->pt_name);
3435 }
3436 else
3437 {
3438 pt->pt_func = ret_pt->pt_func;
3439 func_ptr_ref(pt->pt_func);
3440 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003441 if (ret_pt->pt_argc > 0)
3442 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003443 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003444 if (pt->pt_argv == NULL)
3445 /* out of memory: drop the arguments */
3446 pt->pt_argc = 0;
3447 else
3448 {
3449 pt->pt_argc = ret_pt->pt_argc;
3450 for (i = 0; i < pt->pt_argc; i++)
3451 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3452 }
3453 }
3454 partial_unref(ret_pt);
3455 }
3456 rettv->v_type = VAR_PARTIAL;
3457 rettv->vval.v_partial = pt;
3458 }
3459 }
3460 return selfdict;
3461}
3462
3463/*
3464 * Return the name of the executed function.
3465 */
3466 char_u *
3467func_name(void *cookie)
3468{
3469 return ((funccall_T *)cookie)->func->uf_name;
3470}
3471
3472/*
3473 * Return the address holding the next breakpoint line for a funccall cookie.
3474 */
3475 linenr_T *
3476func_breakpoint(void *cookie)
3477{
3478 return &((funccall_T *)cookie)->breakpoint;
3479}
3480
3481/*
3482 * Return the address holding the debug tick for a funccall cookie.
3483 */
3484 int *
3485func_dbg_tick(void *cookie)
3486{
3487 return &((funccall_T *)cookie)->dbg_tick;
3488}
3489
3490/*
3491 * Return the nesting level for a funccall cookie.
3492 */
3493 int
3494func_level(void *cookie)
3495{
3496 return ((funccall_T *)cookie)->level;
3497}
3498
3499/*
3500 * Return TRUE when a function was ended by a ":return" command.
3501 */
3502 int
3503current_func_returned(void)
3504{
3505 return current_funccal->returned;
3506}
3507
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003508 int
3509free_unref_funccal(int copyID, int testing)
3510{
3511 int did_free = FALSE;
3512 int did_free_funccal = FALSE;
3513 funccall_T *fc, **pfc;
3514
3515 for (pfc = &previous_funccal; *pfc != NULL; )
3516 {
3517 if (can_free_funccal(*pfc, copyID))
3518 {
3519 fc = *pfc;
3520 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003521 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003522 did_free = TRUE;
3523 did_free_funccal = TRUE;
3524 }
3525 else
3526 pfc = &(*pfc)->caller;
3527 }
3528 if (did_free_funccal)
3529 /* When a funccal was freed some more items might be garbage
3530 * collected, so run again. */
3531 (void)garbage_collect(testing);
3532
3533 return did_free;
3534}
3535
3536/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003537 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003538 */
3539 static funccall_T *
3540get_funccal(void)
3541{
3542 int i;
3543 funccall_T *funccal;
3544 funccall_T *temp_funccal;
3545
3546 funccal = current_funccal;
3547 if (debug_backtrace_level > 0)
3548 {
3549 for (i = 0; i < debug_backtrace_level; i++)
3550 {
3551 temp_funccal = funccal->caller;
3552 if (temp_funccal)
3553 funccal = temp_funccal;
3554 else
3555 /* backtrace level overflow. reset to max */
3556 debug_backtrace_level = i;
3557 }
3558 }
3559 return funccal;
3560}
3561
3562/*
3563 * Return the hashtable used for local variables in the current funccal.
3564 * Return NULL if there is no current funccal.
3565 */
3566 hashtab_T *
3567get_funccal_local_ht()
3568{
3569 if (current_funccal == NULL)
3570 return NULL;
3571 return &get_funccal()->l_vars.dv_hashtab;
3572}
3573
3574/*
3575 * Return the l: scope variable.
3576 * Return NULL if there is no current funccal.
3577 */
3578 dictitem_T *
3579get_funccal_local_var()
3580{
3581 if (current_funccal == NULL)
3582 return NULL;
3583 return &get_funccal()->l_vars_var;
3584}
3585
3586/*
3587 * Return the hashtable used for argument in the current funccal.
3588 * Return NULL if there is no current funccal.
3589 */
3590 hashtab_T *
3591get_funccal_args_ht()
3592{
3593 if (current_funccal == NULL)
3594 return NULL;
3595 return &get_funccal()->l_avars.dv_hashtab;
3596}
3597
3598/*
3599 * Return the a: scope variable.
3600 * Return NULL if there is no current funccal.
3601 */
3602 dictitem_T *
3603get_funccal_args_var()
3604{
3605 if (current_funccal == NULL)
3606 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01003607 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003608}
3609
3610/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003611 * List function variables, if there is a function.
3612 */
3613 void
3614list_func_vars(int *first)
3615{
3616 if (current_funccal != NULL)
3617 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01003618 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003619}
3620
3621/*
3622 * If "ht" is the hashtable for local variables in the current funccal, return
3623 * the dict that contains it.
3624 * Otherwise return NULL.
3625 */
3626 dict_T *
3627get_current_funccal_dict(hashtab_T *ht)
3628{
3629 if (current_funccal != NULL
3630 && ht == &current_funccal->l_vars.dv_hashtab)
3631 return &current_funccal->l_vars;
3632 return NULL;
3633}
3634
3635/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003636 * Search hashitem in parent scope.
3637 */
3638 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003639find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003640{
3641 funccall_T *old_current_funccal = current_funccal;
3642 hashtab_T *ht;
3643 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003644 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003645
3646 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3647 return NULL;
3648
3649 /* Search in parent scope which is possible to reference from lambda */
3650 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003651 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003652 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003653 ht = find_var_ht(name, &varname);
3654 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003655 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003656 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003657 if (!HASHITEM_EMPTY(hi))
3658 {
3659 *pht = ht;
3660 break;
3661 }
3662 }
3663 if (current_funccal == current_funccal->func->uf_scoped)
3664 break;
3665 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003666 }
3667 current_funccal = old_current_funccal;
3668
3669 return hi;
3670}
3671
3672/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003673 * Search variable in parent scope.
3674 */
3675 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003676find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003677{
3678 dictitem_T *v = NULL;
3679 funccall_T *old_current_funccal = current_funccal;
3680 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003681 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003682
3683 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3684 return NULL;
3685
3686 /* Search in parent scope which is possible to reference from lambda */
3687 current_funccal = current_funccal->func->uf_scoped;
3688 while (current_funccal)
3689 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003690 ht = find_var_ht(name, &varname);
3691 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003692 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003693 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003694 if (v != NULL)
3695 break;
3696 }
3697 if (current_funccal == current_funccal->func->uf_scoped)
3698 break;
3699 current_funccal = current_funccal->func->uf_scoped;
3700 }
3701 current_funccal = old_current_funccal;
3702
3703 return v;
3704}
3705
3706/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003707 * Set "copyID + 1" in previous_funccal and callers.
3708 */
3709 int
3710set_ref_in_previous_funccal(int copyID)
3711{
3712 int abort = FALSE;
3713 funccall_T *fc;
3714
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003715 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003716 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003717 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003718 abort = abort
3719 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
3720 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003721 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003722 }
3723 return abort;
3724}
3725
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003726 static int
3727set_ref_in_funccal(funccall_T *fc, int copyID)
3728{
3729 int abort = FALSE;
3730
3731 if (fc->fc_copyID != copyID)
3732 {
3733 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003734 abort = abort
3735 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
3736 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003737 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003738 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003739 }
3740 return abort;
3741}
3742
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003743/*
3744 * Set "copyID" in all local vars and arguments in the call stack.
3745 */
3746 int
3747set_ref_in_call_stack(int copyID)
3748{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003749 int abort = FALSE;
3750 funccall_T *fc;
3751 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003752
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003753 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003754 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003755
3756 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003757 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
3758 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003759 abort = abort || set_ref_in_funccal(fc, copyID);
3760
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003761 return abort;
3762}
3763
3764/*
3765 * Set "copyID" in all functions available by name.
3766 */
3767 int
3768set_ref_in_functions(int copyID)
3769{
3770 int todo;
3771 hashitem_T *hi = NULL;
3772 int abort = FALSE;
3773 ufunc_T *fp;
3774
3775 todo = (int)func_hashtab.ht_used;
3776 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003777 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003778 if (!HASHITEM_EMPTY(hi))
3779 {
3780 --todo;
3781 fp = HI2UF(hi);
3782 if (!func_name_refcount(fp->uf_name))
3783 abort = abort || set_ref_in_func(NULL, fp, copyID);
3784 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003785 }
3786 return abort;
3787}
3788
3789/*
3790 * Set "copyID" in all function arguments.
3791 */
3792 int
3793set_ref_in_func_args(int copyID)
3794{
3795 int i;
3796 int abort = FALSE;
3797
3798 for (i = 0; i < funcargs.ga_len; ++i)
3799 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3800 copyID, NULL, NULL);
3801 return abort;
3802}
3803
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003804/*
3805 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003806 * Returns TRUE if setting references failed somehow.
3807 */
3808 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003809set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003810{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003811 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003812 funccall_T *fc;
3813 int error = ERROR_NONE;
3814 char_u fname_buf[FLEN_FIXED + 1];
3815 char_u *tofree = NULL;
3816 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003817 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003818
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003819 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003820 return FALSE;
3821
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003822 if (fp_in == NULL)
3823 {
3824 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3825 fp = find_func(fname);
3826 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003827 if (fp != NULL)
3828 {
3829 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003830 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003831 }
3832 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003833 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003834}
3835
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003836#endif /* FEAT_EVAL */