blob: b3a7558a913d277ac3df07f539ac578a14d66f65 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar14c01f82019-10-09 22:53:08 +020011 * userfunc.c: User defined function support
Bram Moolenaara9b579f2016-07-17 18:29:19 +020012 */
13
14#include "vim.h"
15
16#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar93343722018-07-10 19:39:18 +020017// flags used in uf_flags
18#define FC_ABORT 0x01 // abort function on error
19#define FC_RANGE 0x02 // function accepts range
20#define FC_DICT 0x04 // Dict function, uses "self"
21#define FC_CLOSURE 0x08 // closure, uses outer scope variables
22#define FC_DELETED 0x10 // :delfunction used while uf_refcount > 0
23#define FC_REMOVED 0x20 // function redefined while uf_refcount > 0
24#define FC_SANDBOX 0x40 // function defined in the sandbox
Bram Moolenaara9b579f2016-07-17 18:29:19 +020025
Bram Moolenaara9b579f2016-07-17 18:29:19 +020026#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
Bram Moolenaara9b579f2016-07-17 18:29:19 +020027
Bram Moolenaara9b579f2016-07-17 18:29:19 +020028/*
29 * All user-defined functions are found in this hashtable.
30 */
31static hashtab_T func_hashtab;
32
Bram Moolenaare38eab22019-12-05 21:50:01 +010033// Used by get_func_tv()
Bram Moolenaara9b579f2016-07-17 18:29:19 +020034static 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
Bram Moolenaare38eab22019-12-05 21:50:01 +0100131 // Check for duplicate argument name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200132 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;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100196 ++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)
Bram Moolenaare38eab22019-12-05 21:50:01 +0100216 // no change
Bram Moolenaar8dd3a432016-08-01 20:46:25 +0200217 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
Bram Moolenaare38eab22019-12-05 21:50:01 +0100252 // 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
Bram Moolenaare38eab22019-12-05 21:50:01 +0100257 // 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 Moolenaare38eab22019-12-05 21:50:01 +0100267 // 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 Moolenaare38eab22019-12-05 21:50:01 +0100271 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200272 *arg = skipwhite(*arg + 1);
273 s = *arg;
274 ret = skip_expr(arg);
275 if (ret == FAIL)
276 goto errret;
277 e = *arg;
278 *arg = skipwhite(*arg);
279 if (**arg != '}')
280 goto errret;
281 ++*arg;
282
283 if (evaluate)
284 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200285 int len, flags = 0;
286 char_u *p;
287 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200288
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200289 sprintf((char*)name, "<lambda>%d", ++lambda_no);
290
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200291 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200292 if (fp == NULL)
293 goto errret;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200294 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200295 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200296 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200297
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200298 ga_init2(&newlines, (int)sizeof(char_u *), 1);
299 if (ga_grow(&newlines, 1) == FAIL)
300 goto errret;
301
Bram Moolenaare38eab22019-12-05 21:50:01 +0100302 // 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;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100381 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200382 }
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;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100395 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200396 }
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 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100411 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412emsg_funcname(char *ermsg, char_u *name)
413{
414 char_u *p;
415
416 if (*name == K_SPECIAL)
417 p = concat_str((char_u *)"<SNR>", name + 3);
418 else
419 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100420 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200421 if (p != name)
422 vim_free(p);
423}
424
425/*
426 * Allocate a variable for the result of a function.
427 * Return OK or FAIL.
428 */
429 int
430get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200431 char_u *name, // name of the function
432 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200433 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200434 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200435 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200436{
437 char_u *argp;
438 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100439 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
440 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200441
442 /*
443 * Get the arguments.
444 */
445 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200446 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
447 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200448 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100449 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200450 if (*argp == ')' || *argp == ',' || *argp == NUL)
451 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200452 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200453 {
454 ret = FAIL;
455 break;
456 }
457 ++argcount;
458 if (*argp != ',')
459 break;
460 }
461 if (*argp == ')')
462 ++argp;
463 else
464 ret = FAIL;
465
466 if (ret == OK)
467 {
468 int i = 0;
469
470 if (get_vim_var_nr(VV_TESTING))
471 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100472 // Prepare for calling test_garbagecollect_now(), need to know
473 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200474 if (funcargs.ga_itemsize == 0)
475 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
476 for (i = 0; i < argcount; ++i)
477 if (ga_grow(&funcargs, 1) == OK)
478 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
479 &argvars[i];
480 }
481
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200482 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200483
484 funcargs.ga_len -= i;
485 }
486 else if (!aborting())
487 {
488 if (argcount == MAX_FUNC_ARGS)
489 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
490 else
491 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
492 }
493
494 while (--argcount >= 0)
495 clear_tv(&argvars[argcount]);
496
497 *arg = skipwhite(argp);
498 return ret;
499}
500
501#define FLEN_FIXED 40
502
503/*
504 * Return TRUE if "p" starts with "<SID>" or "s:".
505 * Only works if eval_fname_script() returned non-zero for "p"!
506 */
507 static int
508eval_fname_sid(char_u *p)
509{
510 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
511}
512
513/*
514 * In a script change <SID>name() and s:name() to K_SNR 123_name().
515 * Change <SNR>123_name() to K_SNR 123_name().
516 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
517 * (slow).
518 */
519 static char_u *
520fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
521{
522 int llen;
523 char_u *fname;
524 int i;
525
526 llen = eval_fname_script(name);
527 if (llen > 0)
528 {
529 fname_buf[0] = K_SPECIAL;
530 fname_buf[1] = KS_EXTRA;
531 fname_buf[2] = (int)KE_SNR;
532 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100533 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200534 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200535 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200536 *error = ERROR_SCRIPT;
537 else
538 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200539 sprintf((char *)fname_buf + 3, "%ld_",
540 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200541 i = (int)STRLEN(fname_buf);
542 }
543 }
544 if (i + STRLEN(name + llen) < FLEN_FIXED)
545 {
546 STRCPY(fname_buf + i, name + llen);
547 fname = fname_buf;
548 }
549 else
550 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200551 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200552 if (fname == NULL)
553 *error = ERROR_OTHER;
554 else
555 {
556 *tofree = fname;
557 mch_memmove(fname, fname_buf, (size_t)i);
558 STRCPY(fname + i, name + llen);
559 }
560 }
561 }
562 else
563 fname = name;
564 return fname;
565}
566
567/*
568 * Find a function by name, return pointer to it in ufuncs.
569 * Return NULL for unknown function.
570 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200571 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200572find_func(char_u *name)
573{
574 hashitem_T *hi;
575
576 hi = hash_find(&func_hashtab, name);
577 if (!HASHITEM_EMPTY(hi))
578 return HI2UF(hi);
579 return NULL;
580}
581
582/*
583 * Copy the function name of "fp" to buffer "buf".
584 * "buf" must be able to hold the function name plus three bytes.
585 * Takes care of script-local function names.
586 */
587 static void
588cat_func_name(char_u *buf, ufunc_T *fp)
589{
590 if (fp->uf_name[0] == K_SPECIAL)
591 {
592 STRCPY(buf, "<SNR>");
593 STRCAT(buf, fp->uf_name + 3);
594 }
595 else
596 STRCPY(buf, fp->uf_name);
597}
598
599/*
600 * Add a number variable "name" to dict "dp" with value "nr".
601 */
602 static void
603add_nr_var(
604 dict_T *dp,
605 dictitem_T *v,
606 char *name,
607 varnumber_T nr)
608{
609 STRCPY(v->di_key, name);
610 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
611 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
612 v->di_tv.v_type = VAR_NUMBER;
613 v->di_tv.v_lock = VAR_FIXED;
614 v->di_tv.vval.v_number = nr;
615}
616
617/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100618 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200619 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100620 static void
621free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200622{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100623 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200624
625 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
626 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100627 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200628
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100629 // When garbage collecting a funccall_T may be freed before the
630 // function that references it, clear its uf_scoped field.
631 // The function may have been redefined and point to another
632 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200633 if (fp != NULL && fp->uf_scoped == fc)
634 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200635 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200636 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200637
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200638 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200639 vim_free(fc);
640}
641
642/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100643 * Free "fc" and what it contains.
644 * Can be called only when "fc" is kept beyond the period of it called,
645 * i.e. after cleanup_function_call(fc).
646 */
647 static void
648free_funccal_contents(funccall_T *fc)
649{
650 listitem_T *li;
651
652 // Free all l: variables.
653 vars_clear(&fc->l_vars.dv_hashtab);
654
655 // Free all a: variables.
656 vars_clear(&fc->l_avars.dv_hashtab);
657
658 // Free the a:000 variables.
659 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
660 clear_tv(&li->li_tv);
661
662 free_funccal(fc);
663}
664
665/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200666 * Handle the last part of returning from a function: free the local hashtable.
667 * Unless it is still in use by a closure.
668 */
669 static void
670cleanup_function_call(funccall_T *fc)
671{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100672 int may_free_fc = fc->fc_refcount <= 0;
673 int free_fc = TRUE;
674
Bram Moolenaar6914c642017-04-01 21:21:30 +0200675 current_funccal = fc->caller;
676
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100677 // Free all l: variables if not referred.
678 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
679 vars_clear(&fc->l_vars.dv_hashtab);
680 else
681 free_fc = FALSE;
682
683 // If the a:000 list and the l: and a: dicts are not referenced and
684 // there is no closure using it, we can free the funccall_T and what's
685 // in it.
686 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
687 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200688 else
689 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100690 int todo;
691 hashitem_T *hi;
692 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200693
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100694 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200695
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100696 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200697 todo = (int)fc->l_avars.dv_hashtab.ht_used;
698 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
699 {
700 if (!HASHITEM_EMPTY(hi))
701 {
702 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100703 di = HI2DI(hi);
704 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200705 }
706 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100707 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200708
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100709 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
710 fc->l_varlist.lv_first = NULL;
711 else
712 {
713 listitem_T *li;
714
715 free_fc = FALSE;
716
717 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200718 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
719 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100720 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100721
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100722 if (free_fc)
723 free_funccal(fc);
724 else
725 {
726 static int made_copy = 0;
727
728 // "fc" is still in use. This can happen when returning "a:000",
729 // assigning "l:" to a global variable or defining a closure.
730 // Link "fc" in the list for garbage collection later.
731 fc->caller = previous_funccal;
732 previous_funccal = fc;
733
734 if (want_garbage_collect)
735 // If garbage collector is ready, clear count.
736 made_copy = 0;
737 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100738 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100739 // We have made a lot of copies, worth 4 Mbyte. This can happen
740 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100741 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100742 // too much memory.
743 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100744 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100745 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200746 }
747}
748
749/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200750 * Call a user function.
751 */
752 static void
753call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +0100754 ufunc_T *fp, // pointer to function
755 int argcount, // nr of args
756 typval_T *argvars, // arguments
757 typval_T *rettv, // return value
758 linenr_T firstline, // first line of range
759 linenr_T lastline, // last line of range
760 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200761{
762 char_u *save_sourcing_name;
763 linenr_T save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200764 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +0200765 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766 funccall_T *fc;
767 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200768 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200769 static int depth = 0;
770 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100771 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200772 int i;
773 int ai;
774 int islambda = FALSE;
775 char_u numbuf[NUMBUFLEN];
776 char_u *name;
777 size_t len;
778#ifdef FEAT_PROFILE
779 proftime_T wait_start;
780 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +0200781 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200782#endif
783
Bram Moolenaare38eab22019-12-05 21:50:01 +0100784 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200785 if (depth >= p_mfd)
786 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100787 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200788 rettv->v_type = VAR_NUMBER;
789 rettv->vval.v_number = -1;
790 return;
791 }
792 ++depth;
793
Bram Moolenaare38eab22019-12-05 21:50:01 +0100794 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200795
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200796 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100797 if (fc == NULL)
798 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 fc->caller = current_funccal;
800 current_funccal = fc;
801 fc->func = fp;
802 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200803 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100804 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200805 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
806 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100807 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200808 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200809 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200810
811 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
812 islambda = TRUE;
813
814 /*
815 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
816 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
817 * each argument variable and saves a lot of time.
818 */
819 /*
820 * Init l: variables.
821 */
822 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
823 if (selfdict != NULL)
824 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100825 // Set l:self to "selfdict". Use "name" to avoid a warning from
826 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200827 v = &fc->fixvar[fixvar_idx++].var;
828 name = v->di_key;
829 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +0100830 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200831 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
832 v->di_tv.v_type = VAR_DICT;
833 v->di_tv.v_lock = 0;
834 v->di_tv.vval.v_dict = selfdict;
835 ++selfdict->dv_refcount;
836 }
837
838 /*
839 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200840 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200841 * Set a:000 to a list with room for the "..." arguments.
842 */
843 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
844 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200845 (varnumber_T)(argcount >= fp->uf_args.ga_len
846 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +0100847 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100848 // Use "name" to avoid a warning from some compiler that checks the
849 // destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200850 v = &fc->fixvar[fixvar_idx++].var;
851 name = v->di_key;
852 STRCPY(name, "000");
853 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
854 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
855 v->di_tv.v_type = VAR_LIST;
856 v->di_tv.v_lock = VAR_FIXED;
857 v->di_tv.vval.v_list = &fc->l_varlist;
858 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
859 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
860 fc->l_varlist.lv_lock = VAR_FIXED;
861
862 /*
863 * Set a:firstline to "firstline" and a:lastline to "lastline".
864 * Set a:name to named arguments.
865 * Set a:N to the "..." arguments.
866 */
867 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
868 (varnumber_T)firstline);
869 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
870 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200871 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200872 {
873 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200874 typval_T def_rettv;
875 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200876
877 ai = i - fp->uf_args.ga_len;
878 if (ai < 0)
879 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100880 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200881 name = FUNCARG(fp, i);
882 if (islambda)
883 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200884
885 // evaluate named argument default expression
886 isdefault = ai + fp->uf_def_args.ga_len >= 0
887 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
888 && argvars[i].vval.v_number == VVAL_NONE));
889 if (isdefault)
890 {
891 char_u *default_expr = NULL;
892 def_rettv.v_type = VAR_NUMBER;
893 def_rettv.vval.v_number = -1;
894
895 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
896 [ai + fp->uf_def_args.ga_len];
897 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
898 {
899 default_arg_err = 1;
900 break;
901 }
902 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200903 }
904 else
905 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100906 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200907 sprintf((char *)numbuf, "%d", ai + 1);
908 name = numbuf;
909 }
910 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
911 {
912 v = &fc->fixvar[fixvar_idx++].var;
913 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100914 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200915 }
916 else
917 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100918 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200919 if (v == NULL)
920 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100921 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200922 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200923
Bram Moolenaar6e5000d2019-06-17 21:18:41 +0200924 // Note: the values are copied directly to avoid alloc/free.
925 // "argvars" must have VAR_FIXED for v_lock.
926 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200927 v->di_tv.v_lock = VAR_FIXED;
928
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200929 if (addlocal)
930 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100931 // Named arguments should be accessed without the "a:" prefix in
932 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200933 copy_tv(&v->di_tv, &v->di_tv);
934 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200935 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200936 else
937 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200938
939 if (ai >= 0 && ai < MAX_FUNC_ARGS)
940 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100941 listitem_T *li = &fc->l_listitems[ai];
942
943 li->li_tv = argvars[i];
944 li->li_tv.v_lock = VAR_FIXED;
945 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200946 }
947 }
948
Bram Moolenaare38eab22019-12-05 21:50:01 +0100949 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200950 ++RedrawingDisabled;
951 save_sourcing_name = sourcing_name;
952 save_sourcing_lnum = sourcing_lnum;
953 sourcing_lnum = 1;
Bram Moolenaar93343722018-07-10 19:39:18 +0200954
955 if (fp->uf_flags & FC_SANDBOX)
956 {
957 using_sandbox = TRUE;
958 ++sandbox;
959 }
960
Bram Moolenaare38eab22019-12-05 21:50:01 +0100961 // need space for function name + ("function " + 3) or "[number]"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200962 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
963 + STRLEN(fp->uf_name) + 20;
Bram Moolenaar964b3742019-05-24 18:54:09 +0200964 sourcing_name = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200965 if (sourcing_name != NULL)
966 {
967 if (save_sourcing_name != NULL
968 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
969 sprintf((char *)sourcing_name, "%s[%d]..",
970 save_sourcing_name, (int)save_sourcing_lnum);
971 else
972 STRCPY(sourcing_name, "function ");
973 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
974
975 if (p_verbose >= 12)
976 {
977 ++no_wait_return;
978 verbose_enter_scroll();
979
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100980 smsg(_("calling %s"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200981 if (p_verbose >= 14)
982 {
983 char_u buf[MSG_BUF_LEN];
984 char_u numbuf2[NUMBUFLEN];
985 char_u *tofree;
986 char_u *s;
987
Bram Moolenaar32526b32019-01-19 17:43:09 +0100988 msg_puts("(");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200989 for (i = 0; i < argcount; ++i)
990 {
991 if (i > 0)
Bram Moolenaar32526b32019-01-19 17:43:09 +0100992 msg_puts(", ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200993 if (argvars[i].v_type == VAR_NUMBER)
994 msg_outnum((long)argvars[i].vval.v_number);
995 else
996 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100997 // Do not want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200998 ++emsg_off;
999 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
1000 --emsg_off;
1001 if (s != NULL)
1002 {
1003 if (vim_strsize(s) > MSG_BUF_CLEN)
1004 {
1005 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1006 s = buf;
1007 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001008 msg_puts((char *)s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001009 vim_free(tofree);
1010 }
1011 }
1012 }
Bram Moolenaar32526b32019-01-19 17:43:09 +01001013 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001014 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001015 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001016
1017 verbose_leave_scroll();
1018 --no_wait_return;
1019 }
1020 }
1021#ifdef FEAT_PROFILE
1022 if (do_profiling == PROF_YES)
1023 {
1024 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001025 {
1026 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001027 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001028 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001029 if (fp->uf_profiling
1030 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1031 {
1032 ++fp->uf_tm_count;
1033 profile_start(&call_start);
1034 profile_zero(&fp->uf_tm_children);
1035 }
1036 script_prof_save(&wait_start);
1037 }
1038#endif
1039
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001040 save_current_sctx = current_sctx;
1041 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001042 save_did_emsg = did_emsg;
1043 did_emsg = FALSE;
1044
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001045 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1046 did_emsg = TRUE;
1047 else
1048 // call do_cmdline() to execute the lines
1049 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001050 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1051
1052 --RedrawingDisabled;
1053
Bram Moolenaare38eab22019-12-05 21:50:01 +01001054 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001055 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1056 {
1057 clear_tv(rettv);
1058 rettv->v_type = VAR_NUMBER;
1059 rettv->vval.v_number = -1;
1060 }
1061
1062#ifdef FEAT_PROFILE
1063 if (do_profiling == PROF_YES && (fp->uf_profiling
1064 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1065 {
1066 profile_end(&call_start);
1067 profile_sub_wait(&wait_start, &call_start);
1068 profile_add(&fp->uf_tm_total, &call_start);
1069 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1070 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1071 {
1072 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1073 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1074 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001075 if (started_profiling)
1076 // make a ":profdel func" stop profiling the function
1077 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001078 }
1079#endif
1080
Bram Moolenaare38eab22019-12-05 21:50:01 +01001081 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001082 if (p_verbose >= 12)
1083 {
1084 ++no_wait_return;
1085 verbose_enter_scroll();
1086
1087 if (aborting())
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001088 smsg(_("%s aborted"), sourcing_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001089 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001090 smsg(_("%s returning #%ld"), sourcing_name,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001091 (long)fc->rettv->vval.v_number);
1092 else
1093 {
1094 char_u buf[MSG_BUF_LEN];
1095 char_u numbuf2[NUMBUFLEN];
1096 char_u *tofree;
1097 char_u *s;
1098
Bram Moolenaare38eab22019-12-05 21:50:01 +01001099 // The value may be very long. Skip the middle part, so that we
1100 // have some idea how it starts and ends. smsg() would always
1101 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001102 ++emsg_off;
1103 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1104 --emsg_off;
1105 if (s != NULL)
1106 {
1107 if (vim_strsize(s) > MSG_BUF_CLEN)
1108 {
1109 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1110 s = buf;
1111 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001112 smsg(_("%s returning %s"), sourcing_name, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001113 vim_free(tofree);
1114 }
1115 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001116 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001117
1118 verbose_leave_scroll();
1119 --no_wait_return;
1120 }
1121
1122 vim_free(sourcing_name);
1123 sourcing_name = save_sourcing_name;
1124 sourcing_lnum = save_sourcing_lnum;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001125 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001126#ifdef FEAT_PROFILE
1127 if (do_profiling == PROF_YES)
1128 script_prof_restore(&wait_start);
1129#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001130 if (using_sandbox)
1131 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001132
1133 if (p_verbose >= 12 && sourcing_name != NULL)
1134 {
1135 ++no_wait_return;
1136 verbose_enter_scroll();
1137
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001138 smsg(_("continuing in %s"), sourcing_name);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001139 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001140
1141 verbose_leave_scroll();
1142 --no_wait_return;
1143 }
1144
1145 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001146 --depth;
1147
Bram Moolenaar6914c642017-04-01 21:21:30 +02001148 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001149}
1150
1151/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001152 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001153 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001154 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001155 */
1156 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001157funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001158{
1159 funccall_T **pfc;
1160 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001161
1162 if (fc == NULL)
1163 return;
1164
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001165 if (--fc->fc_refcount <= 0 && (force || (
1166 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001167 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001168 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001169 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001170 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001171 if (fc == *pfc)
1172 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001173 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001174 free_funccal_contents(fc);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001175 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001176 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001177 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001178 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001179 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001180 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001181}
1182
1183/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001184 * Remove the function from the function hashtable. If the function was
1185 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001186 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001187 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001188 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001189func_remove(ufunc_T *fp)
1190{
1191 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1192
1193 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001194 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001195 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001196 return TRUE;
1197 }
1198 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001199}
1200
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001201 static void
1202func_clear_items(ufunc_T *fp)
1203{
1204 ga_clear_strings(&(fp->uf_args));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001205 ga_clear_strings(&(fp->uf_def_args));
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001206 ga_clear_strings(&(fp->uf_lines));
1207#ifdef FEAT_PROFILE
1208 vim_free(fp->uf_tml_count);
1209 fp->uf_tml_count = NULL;
1210 vim_free(fp->uf_tml_total);
1211 fp->uf_tml_total = NULL;
1212 vim_free(fp->uf_tml_self);
1213 fp->uf_tml_self = NULL;
1214#endif
1215}
1216
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001217/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001218 * Free all things that a function contains. Does not free the function
1219 * itself, use func_free() for that.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001220 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001221 */
1222 static void
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001223func_clear(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001224{
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001225 if (fp->uf_cleared)
1226 return;
1227 fp->uf_cleared = TRUE;
1228
Bram Moolenaare38eab22019-12-05 21:50:01 +01001229 // clear this function
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001230 func_clear_items(fp);
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001231 funccal_unref(fp->uf_scoped, fp, force);
1232}
1233
1234/*
1235 * Free a function and remove it from the list of functions. Does not free
1236 * what a function contains, call func_clear() first.
1237 */
1238 static void
1239func_free(ufunc_T *fp)
1240{
Bram Moolenaare38eab22019-12-05 21:50:01 +01001241 // only remove it when not done already, otherwise we would remove a newer
1242 // version of the function
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001243 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1244 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001245
1246 vim_free(fp);
1247}
1248
Bram Moolenaarc2574872016-08-11 22:51:05 +02001249/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001250 * Free all things that a function contains and free the function itself.
1251 * When "force" is TRUE we are exiting.
1252 */
1253 static void
1254func_clear_free(ufunc_T *fp, int force)
1255{
1256 func_clear(fp, force);
1257 func_free(fp);
1258}
1259
1260/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001261 * There are two kinds of function names:
1262 * 1. ordinary names, function defined with :function
1263 * 2. numbered functions and lambdas
1264 * For the first we only count the name stored in func_hashtab as a reference,
1265 * using function() does not count as a reference, because the function is
1266 * looked up by name.
1267 */
1268 static int
1269func_name_refcount(char_u *name)
1270{
1271 return isdigit(*name) || *name == '<';
1272}
1273
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001274static funccal_entry_T *funccal_stack = NULL;
1275
1276/*
1277 * Save the current function call pointer, and set it to NULL.
1278 * Used when executing autocommands and for ":source".
1279 */
1280 void
1281save_funccal(funccal_entry_T *entry)
1282{
1283 entry->top_funccal = current_funccal;
1284 entry->next = funccal_stack;
1285 funccal_stack = entry;
1286 current_funccal = NULL;
1287}
1288
1289 void
1290restore_funccal(void)
1291{
1292 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001293 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001294 else
1295 {
1296 current_funccal = funccal_stack->top_funccal;
1297 funccal_stack = funccal_stack->next;
1298 }
1299}
1300
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001301 funccall_T *
1302get_current_funccal(void)
1303{
1304 return current_funccal;
1305}
1306
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001307#if defined(EXITFREE) || defined(PROTO)
1308 void
1309free_all_functions(void)
1310{
1311 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001312 ufunc_T *fp;
1313 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001314 long_u todo = 1;
1315 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001316
Bram Moolenaare38eab22019-12-05 21:50:01 +01001317 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001318 while (current_funccal != NULL)
1319 {
1320 clear_tv(current_funccal->rettv);
1321 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001322 if (current_funccal == NULL && funccal_stack != NULL)
1323 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001324 }
1325
Bram Moolenaare38eab22019-12-05 21:50:01 +01001326 // First clear what the functions contain. Since this may lower the
1327 // reference count of a function, it may also free a function and change
1328 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001329 while (todo > 0)
1330 {
1331 todo = func_hashtab.ht_used;
1332 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1333 if (!HASHITEM_EMPTY(hi))
1334 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001335 // Only free functions that are not refcounted, those are
1336 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001337 fp = HI2UF(hi);
1338 if (func_name_refcount(fp->uf_name))
1339 ++skipped;
1340 else
1341 {
1342 used = func_hashtab.ht_used;
1343 func_clear(fp, TRUE);
1344 if (used != func_hashtab.ht_used)
1345 {
1346 skipped = 0;
1347 break;
1348 }
1349 }
1350 --todo;
1351 }
1352 }
1353
Bram Moolenaare38eab22019-12-05 21:50:01 +01001354 // Now actually free the functions. Need to start all over every time,
1355 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001356 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001357 while (func_hashtab.ht_used > skipped)
1358 {
1359 todo = func_hashtab.ht_used;
1360 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001361 if (!HASHITEM_EMPTY(hi))
1362 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001363 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001364 // Only free functions that are not refcounted, those are
1365 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001366 fp = HI2UF(hi);
1367 if (func_name_refcount(fp->uf_name))
1368 ++skipped;
1369 else
1370 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001371 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001372 skipped = 0;
1373 break;
1374 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001375 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001376 }
1377 if (skipped == 0)
1378 hash_clear(&func_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001379}
1380#endif
1381
1382/*
1383 * Return TRUE if "name" looks like a builtin function name: starts with a
1384 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1385 * "len" is the length of "name", or -1 for NUL terminated.
1386 */
1387 static int
1388builtin_function(char_u *name, int len)
1389{
1390 char_u *p;
1391
1392 if (!ASCII_ISLOWER(name[0]))
1393 return FALSE;
1394 p = vim_strchr(name, AUTOLOAD_CHAR);
1395 return p == NULL || (len > 0 && p > name + len);
1396}
1397
1398 int
1399func_call(
1400 char_u *name,
1401 typval_T *args,
1402 partial_T *partial,
1403 dict_T *selfdict,
1404 typval_T *rettv)
1405{
1406 listitem_T *item;
1407 typval_T argv[MAX_FUNC_ARGS + 1];
1408 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001409 int r = 0;
1410
1411 for (item = args->vval.v_list->lv_first; item != NULL;
1412 item = item->li_next)
1413 {
1414 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1415 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001416 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001417 break;
1418 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001419 // Make a copy of each argument. This is needed to be able to set
1420 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001421 copy_tv(&item->li_tv, &argv[argc++]);
1422 }
1423
1424 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001425 {
1426 funcexe_T funcexe;
1427
Bram Moolenaarac92e252019-08-03 21:58:38 +02001428 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001429 funcexe.firstline = curwin->w_cursor.lnum;
1430 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001431 funcexe.evaluate = TRUE;
1432 funcexe.partial = partial;
1433 funcexe.selfdict = selfdict;
1434 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1435 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001436
Bram Moolenaare38eab22019-12-05 21:50:01 +01001437 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001438 while (argc > 0)
1439 clear_tv(&argv[--argc]);
1440
1441 return r;
1442}
1443
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001444static int callback_depth = 0;
1445
1446 int
1447get_callback_depth(void)
1448{
1449 return callback_depth;
1450}
1451
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001452/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001453 * Invoke call_func() with a callback.
1454 */
1455 int
1456call_callback(
1457 callback_T *callback,
1458 int len, // length of "name" or -1 to use strlen()
1459 typval_T *rettv, // return value goes here
1460 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001461 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001462 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001463{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001464 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001465 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001466
1467 vim_memset(&funcexe, 0, sizeof(funcexe));
1468 funcexe.evaluate = TRUE;
1469 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001470 ++callback_depth;
1471 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1472 --callback_depth;
1473 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001474}
1475
1476/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001477 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001478 *
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 Moolenaarc6538bc2019-08-03 18:17:11 +02001490 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001491{
1492 int ret = FAIL;
1493 int error = ERROR_NONE;
1494 int i;
1495 ufunc_T *fp;
1496 char_u fname_buf[FLEN_FIXED + 1];
1497 char_u *tofree = NULL;
1498 char_u *fname;
1499 char_u *name;
1500 int argcount = argcount_in;
1501 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001502 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001503 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1504 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001505 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001506 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001507 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001508
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001509 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1510 // even when call_func() returns FAIL.
1511 rettv->v_type = VAR_UNKNOWN;
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
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001521 if (funcexe->doesrange != NULL)
1522 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001523
1524 if (partial != NULL)
1525 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001526 // When the function has a partial with a dict and there is a dict
1527 // argument, use the dict argument. That is backwards compatible.
1528 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001529 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001530 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)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001534 {
1535 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1536 {
1537 error = ERROR_TOOMANY;
1538 goto theend;
1539 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001540 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001541 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001542 for (i = 0; i < argcount_in; ++i)
1543 argv[i + argv_clear] = argvars_in[i];
1544 argvars = argv;
1545 argcount = partial->pt_argc + argcount_in;
1546 }
1547 }
1548
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001549 if (error == ERROR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001550 {
1551 char_u *rfname = fname;
1552
Bram Moolenaare38eab22019-12-05 21:50:01 +01001553 // Ignore "g:" before a function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001554 if (fname[0] == 'g' && fname[1] == ':')
1555 rfname = fname + 2;
1556
Bram Moolenaare38eab22019-12-05 21:50:01 +01001557 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001558 rettv->vval.v_number = 0;
1559 error = ERROR_UNKNOWN;
1560
1561 if (!builtin_function(rfname, -1))
1562 {
1563 /*
1564 * User defined function.
1565 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001566 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001567 fp = partial->pt_func;
1568 else
1569 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001570
Bram Moolenaare38eab22019-12-05 21:50:01 +01001571 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001572 if (fp == NULL
1573 && apply_autocmds(EVENT_FUNCUNDEFINED,
1574 rfname, rfname, TRUE, NULL)
1575 && !aborting())
1576 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001577 // executed an autocommand, search for the function again
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001578 fp = find_func(rfname);
1579 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001580 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001581 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1582 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001583 // loaded a package, search for the function again
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001584 fp = find_func(rfname);
1585 }
1586
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001587 if (fp != NULL && (fp->uf_flags & FC_DELETED))
1588 error = ERROR_DELETED;
1589 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001590 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001591 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001592 // postponed filling in the arguments, do it now
1593 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001594 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001595
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001596 if (funcexe->basetv != NULL)
1597 {
1598 // Method call: base->Method()
1599 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1600 argv[0] = *funcexe->basetv;
1601 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001602 argvars = argv;
1603 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001604 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001605
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001606 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1607 *funcexe->doesrange = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001608 if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 error = ERROR_TOOFEW;
1610 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
1611 error = ERROR_TOOMANY;
1612 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
1613 error = ERROR_DICT;
1614 else
1615 {
1616 int did_save_redo = FALSE;
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001617 save_redo_T save_redo;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001618
1619 /*
1620 * Call the user function.
1621 * Save and restore search patterns, script variables and
1622 * redo buffer.
1623 */
1624 save_search_patterns();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001625 if (!ins_compl_active())
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001626 {
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001627 saveRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628 did_save_redo = TRUE;
1629 }
1630 ++fp->uf_calls;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001631 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001632 funcexe->firstline, funcexe->lastline,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001633 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001634 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001635 // Function was unreferenced while being used, free it
1636 // now.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001637 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001638 if (did_save_redo)
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001639 restoreRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001640 restore_search_patterns();
1641 error = ERROR_NONE;
1642 }
1643 }
1644 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001645 else if (funcexe->basetv != NULL)
1646 {
1647 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001648 * expr->method(): Find the method name in the table, call its
1649 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001650 */
1651 error = call_internal_method(fname, argcount, argvars, rettv,
1652 funcexe->basetv);
1653 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001654 else
1655 {
1656 /*
1657 * Find the function name in the table, call its implementation.
1658 */
1659 error = call_internal_func(fname, argcount, argvars, rettv);
1660 }
1661 /*
1662 * The function call (or "FuncUndefined" autocommand sequence) might
1663 * have been aborted by an error, an interrupt, or an explicitly thrown
1664 * exception that has not been caught so far. This situation can be
1665 * tested for by calling aborting(). For an error in an internal
1666 * function or for the "E132" error in call_user_func(), however, the
1667 * throw point at which the "force_abort" flag (temporarily reset by
1668 * emsg()) is normally updated has not been reached yet. We need to
1669 * update that flag first to make aborting() reliable.
1670 */
1671 update_force_abort();
1672 }
1673 if (error == ERROR_NONE)
1674 ret = OK;
1675
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001676theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001677 /*
1678 * Report an error unless the argument evaluation or function call has been
1679 * cancelled due to an aborting error, an interrupt, or an exception.
1680 */
1681 if (!aborting())
1682 {
1683 switch (error)
1684 {
1685 case ERROR_UNKNOWN:
1686 emsg_funcname(N_("E117: Unknown function: %s"), name);
1687 break;
Bram Moolenaar91746392019-08-16 22:22:31 +02001688 case ERROR_NOTMETHOD:
1689 emsg_funcname(
1690 N_("E276: Cannot use function as a method: %s"),
1691 name);
1692 break;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001693 case ERROR_DELETED:
1694 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1695 break;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001696 case ERROR_TOOMANY:
1697 emsg_funcname((char *)e_toomanyarg, name);
1698 break;
1699 case ERROR_TOOFEW:
Bram Moolenaar91746392019-08-16 22:22:31 +02001700 emsg_funcname(
1701 N_("E119: Not enough arguments for function: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001702 name);
1703 break;
1704 case ERROR_SCRIPT:
Bram Moolenaar91746392019-08-16 22:22:31 +02001705 emsg_funcname(
1706 N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 name);
1708 break;
1709 case ERROR_DICT:
Bram Moolenaar91746392019-08-16 22:22:31 +02001710 emsg_funcname(
1711 N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001712 name);
1713 break;
1714 }
1715 }
1716
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001717 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001718 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001719 clear_tv(&argv[--argv_clear + argv_base]);
1720
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001721 vim_free(tofree);
1722 vim_free(name);
1723
1724 return ret;
1725}
1726
1727/*
1728 * List the head of the function: "name(arg1, arg2)".
1729 */
1730 static void
1731list_func_head(ufunc_T *fp, int indent)
1732{
1733 int j;
1734
1735 msg_start();
1736 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001737 msg_puts(" ");
1738 msg_puts("function ");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001739 if (fp->uf_name[0] == K_SPECIAL)
1740 {
Bram Moolenaar32526b32019-01-19 17:43:09 +01001741 msg_puts_attr("<SNR>", HL_ATTR(HLF_8));
1742 msg_puts((char *)fp->uf_name + 3);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001743 }
1744 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001745 msg_puts((char *)fp->uf_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001746 msg_putchar('(');
1747 for (j = 0; j < fp->uf_args.ga_len; ++j)
1748 {
1749 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001750 msg_puts(", ");
1751 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001752 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1753 {
1754 msg_puts(" = ");
1755 msg_puts(((char **)(fp->uf_def_args.ga_data))
1756 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1757 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001758 }
1759 if (fp->uf_varargs)
1760 {
1761 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001762 msg_puts(", ");
1763 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001764 }
1765 msg_putchar(')');
1766 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001767 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001768 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001769 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001770 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001771 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001772 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001773 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001774 msg_clr_eos();
1775 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001776 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001777}
1778
1779/*
1780 * Get a function name, translating "<SID>" and "<SNR>".
1781 * Also handles a Funcref in a List or Dictionary.
1782 * Returns the function name in allocated memory, or NULL for failure.
1783 * flags:
1784 * TFN_INT: internal function name OK
1785 * TFN_QUIET: be quiet
1786 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001787 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001788 * Advances "pp" to just after the function name (if no error).
1789 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001790 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001791trans_function_name(
1792 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001793 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001794 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001795 funcdict_T *fdp, // return: info about dictionary used
1796 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001797{
1798 char_u *name = NULL;
1799 char_u *start;
1800 char_u *end;
1801 int lead;
1802 char_u sid_buf[20];
1803 int len;
1804 lval_T lv;
1805
1806 if (fdp != NULL)
1807 vim_memset(fdp, 0, sizeof(funcdict_T));
1808 start = *pp;
1809
Bram Moolenaare38eab22019-12-05 21:50:01 +01001810 // Check for hard coded <SNR>: already translated function ID (from a user
1811 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001812 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1813 && (*pp)[2] == (int)KE_SNR)
1814 {
1815 *pp += 3;
1816 len = get_id_len(pp) + 3;
1817 return vim_strnsave(start, len);
1818 }
1819
Bram Moolenaare38eab22019-12-05 21:50:01 +01001820 // A name starting with "<SID>" or "<SNR>" is local to a script. But
1821 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001822 lead = eval_fname_script(start);
1823 if (lead > 2)
1824 start += lead;
1825
Bram Moolenaare38eab22019-12-05 21:50:01 +01001826 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001827 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001828 lead > 2 ? 0 : FNE_CHECK_START);
1829 if (end == start)
1830 {
1831 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001832 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001833 goto theend;
1834 }
1835 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1836 {
1837 /*
1838 * Report an invalid expression in braces, unless the expression
1839 * evaluation has been cancelled due to an aborting error, an
1840 * interrupt, or an exception.
1841 */
1842 if (!aborting())
1843 {
1844 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001845 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001846 }
1847 else
1848 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1849 goto theend;
1850 }
1851
1852 if (lv.ll_tv != NULL)
1853 {
1854 if (fdp != NULL)
1855 {
1856 fdp->fd_dict = lv.ll_dict;
1857 fdp->fd_newkey = lv.ll_newkey;
1858 lv.ll_newkey = NULL;
1859 fdp->fd_di = lv.ll_di;
1860 }
1861 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1862 {
1863 name = vim_strsave(lv.ll_tv->vval.v_string);
1864 *pp = end;
1865 }
1866 else if (lv.ll_tv->v_type == VAR_PARTIAL
1867 && lv.ll_tv->vval.v_partial != NULL)
1868 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001869 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001870 *pp = end;
1871 if (partial != NULL)
1872 *partial = lv.ll_tv->vval.v_partial;
1873 }
1874 else
1875 {
1876 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1877 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001878 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001879 else
1880 *pp = end;
1881 name = NULL;
1882 }
1883 goto theend;
1884 }
1885
1886 if (lv.ll_name == NULL)
1887 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001888 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001889 *pp = end;
1890 goto theend;
1891 }
1892
Bram Moolenaare38eab22019-12-05 21:50:01 +01001893 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001894 if (lv.ll_exp_name != NULL)
1895 {
1896 len = (int)STRLEN(lv.ll_exp_name);
1897 name = deref_func_name(lv.ll_exp_name, &len, partial,
1898 flags & TFN_NO_AUTOLOAD);
1899 if (name == lv.ll_exp_name)
1900 name = NULL;
1901 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001902 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001903 {
1904 len = (int)(end - *pp);
1905 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1906 if (name == *pp)
1907 name = NULL;
1908 }
1909 if (name != NULL)
1910 {
1911 name = vim_strsave(name);
1912 *pp = end;
1913 if (STRNCMP(name, "<SNR>", 5) == 0)
1914 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001915 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001916 name[0] = K_SPECIAL;
1917 name[1] = KS_EXTRA;
1918 name[2] = (int)KE_SNR;
1919 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1920 }
1921 goto theend;
1922 }
1923
1924 if (lv.ll_exp_name != NULL)
1925 {
1926 len = (int)STRLEN(lv.ll_exp_name);
1927 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1928 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1929 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001930 // When there was "s:" already or the name expanded to get a
1931 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 lv.ll_name += 2;
1933 len -= 2;
1934 lead = 2;
1935 }
1936 }
1937 else
1938 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001939 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001940 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1941 lv.ll_name += 2;
1942 len = (int)(end - lv.ll_name);
1943 }
1944
1945 /*
1946 * Copy the function name to allocated memory.
1947 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1948 * Accept <SNR>123_name() outside a script.
1949 */
1950 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001951 lead = 0; // do nothing
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 else if (lead > 0)
1953 {
1954 lead = 3;
1955 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1956 || eval_fname_sid(*pp))
1957 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001958 // It's "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001959 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001961 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001962 goto theend;
1963 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001964 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001965 lead += (int)STRLEN(sid_buf);
1966 }
1967 }
1968 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1969 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001970 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001971 start);
1972 goto theend;
1973 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001974 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001975 {
1976 char_u *cp = vim_strchr(lv.ll_name, ':');
1977
1978 if (cp != NULL && cp < end)
1979 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001980 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001981 goto theend;
1982 }
1983 }
1984
Bram Moolenaar964b3742019-05-24 18:54:09 +02001985 name = alloc(len + lead + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986 if (name != NULL)
1987 {
1988 if (lead > 0)
1989 {
1990 name[0] = K_SPECIAL;
1991 name[1] = KS_EXTRA;
1992 name[2] = (int)KE_SNR;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001993 if (lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001994 STRCPY(name + 3, sid_buf);
1995 }
1996 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1997 name[lead + len] = NUL;
1998 }
1999 *pp = end;
2000
2001theend:
2002 clear_lval(&lv);
2003 return name;
2004}
2005
2006/*
2007 * ":function"
2008 */
2009 void
2010ex_function(exarg_T *eap)
2011{
2012 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002013 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002014 int j;
2015 int c;
2016 int saved_did_emsg;
2017 int saved_wait_return = need_wait_return;
2018 char_u *name = NULL;
2019 char_u *p;
2020 char_u *arg;
2021 char_u *line_arg = NULL;
2022 garray_T newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002023 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002024 garray_T newlines;
2025 int varargs = FALSE;
2026 int flags = 0;
2027 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002028 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002029 int indent;
2030 int nesting;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002031 dictitem_T *v;
2032 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002033 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002034 int paren;
2035 hashtab_T *ht;
2036 int todo;
2037 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002038 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002039 linenr_T sourcing_lnum_off;
2040 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002041 int is_heredoc = FALSE;
2042 char_u *skip_until = NULL;
2043 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002044
2045 /*
2046 * ":function" without argument: list functions.
2047 */
2048 if (ends_excmd(*eap->arg))
2049 {
2050 if (!eap->skip)
2051 {
2052 todo = (int)func_hashtab.ht_used;
2053 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2054 {
2055 if (!HASHITEM_EMPTY(hi))
2056 {
2057 --todo;
2058 fp = HI2UF(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02002059 if (message_filtered(fp->uf_name))
2060 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002061 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002062 list_func_head(fp, FALSE);
2063 }
2064 }
2065 }
2066 eap->nextcmd = check_nextcmd(eap->arg);
2067 return;
2068 }
2069
2070 /*
2071 * ":function /pat": list functions matching pattern.
2072 */
2073 if (*eap->arg == '/')
2074 {
2075 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2076 if (!eap->skip)
2077 {
2078 regmatch_T regmatch;
2079
2080 c = *p;
2081 *p = NUL;
2082 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2083 *p = c;
2084 if (regmatch.regprog != NULL)
2085 {
2086 regmatch.rm_ic = p_ic;
2087
2088 todo = (int)func_hashtab.ht_used;
2089 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2090 {
2091 if (!HASHITEM_EMPTY(hi))
2092 {
2093 --todo;
2094 fp = HI2UF(hi);
2095 if (!isdigit(*fp->uf_name)
2096 && vim_regexec(&regmatch, fp->uf_name, 0))
2097 list_func_head(fp, FALSE);
2098 }
2099 }
2100 vim_regfree(regmatch.regprog);
2101 }
2102 }
2103 if (*p == '/')
2104 ++p;
2105 eap->nextcmd = check_nextcmd(p);
2106 return;
2107 }
2108
2109 /*
2110 * Get the function name. There are these situations:
2111 * func normal function name
2112 * "name" == func, "fudi.fd_dict" == NULL
2113 * dict.func new dictionary entry
2114 * "name" == NULL, "fudi.fd_dict" set,
2115 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2116 * dict.func existing dict entry with a Funcref
2117 * "name" == func, "fudi.fd_dict" set,
2118 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2119 * dict.func existing dict entry that's not a Funcref
2120 * "name" == NULL, "fudi.fd_dict" set,
2121 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2122 * s:func script-local function name
2123 * g:func global function name, same as "func"
2124 */
2125 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002126 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002127 paren = (vim_strchr(p, '(') != NULL);
2128 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2129 {
2130 /*
2131 * Return on an invalid expression in braces, unless the expression
2132 * evaluation has been cancelled due to an aborting error, an
2133 * interrupt, or an exception.
2134 */
2135 if (!aborting())
2136 {
2137 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002138 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002139 vim_free(fudi.fd_newkey);
2140 return;
2141 }
2142 else
2143 eap->skip = TRUE;
2144 }
2145
Bram Moolenaare38eab22019-12-05 21:50:01 +01002146 // An error in a function call during evaluation of an expression in magic
2147 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002148 saved_did_emsg = did_emsg;
2149 did_emsg = FALSE;
2150
2151 /*
2152 * ":function func" with only function name: list function.
2153 */
2154 if (!paren)
2155 {
2156 if (!ends_excmd(*skipwhite(p)))
2157 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002158 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002159 goto ret_free;
2160 }
2161 eap->nextcmd = check_nextcmd(p);
2162 if (eap->nextcmd != NULL)
2163 *p = NUL;
2164 if (!eap->skip && !got_int)
2165 {
2166 fp = find_func(name);
2167 if (fp != NULL)
2168 {
2169 list_func_head(fp, TRUE);
2170 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2171 {
2172 if (FUNCLINE(fp, j) == NULL)
2173 continue;
2174 msg_putchar('\n');
2175 msg_outnum((long)(j + 1));
2176 if (j < 9)
2177 msg_putchar(' ');
2178 if (j < 99)
2179 msg_putchar(' ');
2180 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002181 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002182 ui_breakcheck();
2183 }
2184 if (!got_int)
2185 {
2186 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01002187 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002188 }
2189 }
2190 else
2191 emsg_funcname(N_("E123: Undefined function: %s"), name);
2192 }
2193 goto ret_free;
2194 }
2195
2196 /*
2197 * ":function name(arg1, arg2)" Define function.
2198 */
2199 p = skipwhite(p);
2200 if (*p != '(')
2201 {
2202 if (!eap->skip)
2203 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002204 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002205 goto ret_free;
2206 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002207 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002208 if (vim_strchr(p, '(') != NULL)
2209 p = vim_strchr(p, '(');
2210 }
2211 p = skipwhite(p + 1);
2212
2213 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2214
2215 if (!eap->skip)
2216 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002217 // Check the name of the function. Unless it's a dictionary function
2218 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002219 if (name != NULL)
2220 arg = name;
2221 else
2222 arg = fudi.fd_newkey;
2223 if (arg != NULL && (fudi.fd_di == NULL
2224 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2225 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2226 {
2227 if (*arg == K_SPECIAL)
2228 j = 3;
2229 else
2230 j = 0;
2231 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2232 : eval_isnamec(arg[j])))
2233 ++j;
2234 if (arg[j] != NUL)
2235 emsg_funcname((char *)e_invarg2, arg);
2236 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002237 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002238 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002239 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002240 }
2241
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002242 if (get_function_args(&p, ')', &newargs, &varargs,
2243 &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002244 goto errret_2;
2245
Bram Moolenaare38eab22019-12-05 21:50:01 +01002246 // find extra arguments "range", "dict", "abort" and "closure"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002247 for (;;)
2248 {
2249 p = skipwhite(p);
2250 if (STRNCMP(p, "range", 5) == 0)
2251 {
2252 flags |= FC_RANGE;
2253 p += 5;
2254 }
2255 else if (STRNCMP(p, "dict", 4) == 0)
2256 {
2257 flags |= FC_DICT;
2258 p += 4;
2259 }
2260 else if (STRNCMP(p, "abort", 5) == 0)
2261 {
2262 flags |= FC_ABORT;
2263 p += 5;
2264 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002265 else if (STRNCMP(p, "closure", 7) == 0)
2266 {
2267 flags |= FC_CLOSURE;
2268 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02002269 if (current_funccal == NULL)
2270 {
Bram Moolenaarba209902016-08-24 22:06:38 +02002271 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
Bram Moolenaar58016442016-07-31 18:30:22 +02002272 name == NULL ? (char_u *)"" : name);
2273 goto erret;
2274 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002275 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002276 else
2277 break;
2278 }
2279
Bram Moolenaare38eab22019-12-05 21:50:01 +01002280 // When there is a line break use what follows for the function body.
2281 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002282 if (*p == '\n')
2283 line_arg = p + 1;
2284 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002285 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002286
2287 /*
2288 * Read the body of the function, until ":endfunction" is found.
2289 */
2290 if (KeyTyped)
2291 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002292 // Check if the function already exists, don't let the user type the
2293 // whole function before telling him it doesn't work! For a script we
2294 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002295 if (!eap->skip && !eap->forceit)
2296 {
2297 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002298 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 else if (name != NULL && find_func(name) != NULL)
2300 emsg_funcname(e_funcexts, name);
2301 }
2302
2303 if (!eap->skip && did_emsg)
2304 goto erret;
2305
Bram Moolenaare38eab22019-12-05 21:50:01 +01002306 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002307 cmdline_row = msg_row;
2308 }
2309
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002310 // Save the starting line number.
2311 sourcing_lnum_top = sourcing_lnum;
2312
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002313 indent = 2;
2314 nesting = 0;
2315 for (;;)
2316 {
2317 if (KeyTyped)
2318 {
2319 msg_scroll = TRUE;
2320 saved_wait_return = FALSE;
2321 }
2322 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002323
2324 if (line_arg != NULL)
2325 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002326 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002327 theline = line_arg;
2328 p = vim_strchr(theline, '\n');
2329 if (p == NULL)
2330 line_arg += STRLEN(line_arg);
2331 else
2332 {
2333 *p = NUL;
2334 line_arg = p + 1;
2335 }
2336 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002337 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002338 {
2339 vim_free(line_to_free);
2340 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002341 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002342 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002343 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002344 line_to_free = theline;
2345 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002346 if (KeyTyped)
2347 lines_left = Rows - 1;
2348 if (theline == NULL)
2349 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002350 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002351 goto erret;
2352 }
2353
Bram Moolenaare38eab22019-12-05 21:50:01 +01002354 // Detect line continuation: sourcing_lnum increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002355 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
2356 if (sourcing_lnum < sourcing_lnum_off)
2357 sourcing_lnum_off -= sourcing_lnum;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002358 else
2359 sourcing_lnum_off = 0;
2360
2361 if (skip_until != NULL)
2362 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002363 // Don't check for ":endfunc" between
2364 // * ":append" and "."
2365 // * ":python <<EOF" and "EOF"
2366 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2367 if (heredoc_trimmed == NULL
2368 || (is_heredoc && skipwhite(theline) == theline)
2369 || STRNCMP(theline, heredoc_trimmed,
2370 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002371 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002372 if (heredoc_trimmed == NULL)
2373 p = theline;
2374 else if (is_heredoc)
2375 p = skipwhite(theline) == theline
2376 ? theline : theline + STRLEN(heredoc_trimmed);
2377 else
2378 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002379 if (STRCMP(p, skip_until) == 0)
2380 {
2381 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002382 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002383 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002384 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002385 }
2386 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002387 }
2388 else
2389 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002390 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002391 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002392 ;
2393
Bram Moolenaare38eab22019-12-05 21:50:01 +01002394 // Check for "endfunction".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002395 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2396 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002397 char_u *nextcmd = NULL;
2398
Bram Moolenaar663bb232017-06-22 19:12:10 +02002399 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002400 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002401 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002402 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002403 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002404 give_warning2(
2405 (char_u *)_("W22: Text found after :endfunction: %s"),
2406 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002407 if (nextcmd != NULL)
2408 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002409 // Another command follows. If the line came from "eap" we
2410 // can simply point into it, otherwise we need to change
2411 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002412 eap->nextcmd = nextcmd;
2413 if (line_to_free != NULL)
2414 {
2415 vim_free(*eap->cmdlinep);
2416 *eap->cmdlinep = line_to_free;
2417 line_to_free = NULL;
2418 }
2419 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002420 break;
2421 }
2422
Bram Moolenaare38eab22019-12-05 21:50:01 +01002423 // Increase indent inside "if", "while", "for" and "try", decrease
2424 // at "end".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002425 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2426 indent -= 2;
2427 else if (STRNCMP(p, "if", 2) == 0
2428 || STRNCMP(p, "wh", 2) == 0
2429 || STRNCMP(p, "for", 3) == 0
2430 || STRNCMP(p, "try", 3) == 0)
2431 indent += 2;
2432
Bram Moolenaare38eab22019-12-05 21:50:01 +01002433 // Check for defining a function inside this function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002434 if (checkforcmd(&p, "function", 2))
2435 {
2436 if (*p == '!')
2437 p = skipwhite(p + 1);
2438 p += eval_fname_script(p);
2439 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2440 if (*skipwhite(p) == '(')
2441 {
2442 ++nesting;
2443 indent += 2;
2444 }
2445 }
2446
Bram Moolenaare38eab22019-12-05 21:50:01 +01002447 // Check for ":append", ":change", ":insert".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002448 p = skip_range(p, NULL);
2449 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002450 || (p[0] == 'c'
2451 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2452 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2453 && (STRNCMP(&p[3], "nge", 3) != 0
2454 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002455 || (p[0] == 'i'
2456 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2457 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2458 skip_until = vim_strsave((char_u *)".");
2459
Bram Moolenaare38eab22019-12-05 21:50:01 +01002460 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002461 arg = skipwhite(skiptowhite(p));
2462 if (arg[0] == '<' && arg[1] =='<'
2463 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002464 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2465 || ((p[2] == '3' || p[2] == 'x')
2466 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002467 || (p[0] == 'p' && p[1] == 'e'
2468 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2469 || (p[0] == 't' && p[1] == 'c'
2470 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2471 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2472 && !ASCII_ISALPHA(p[3]))
2473 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2474 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2475 || (p[0] == 'm' && p[1] == 'z'
2476 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2477 ))
2478 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002479 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002480 p = skipwhite(arg + 2);
2481 if (*p == NUL)
2482 skip_until = vim_strsave((char_u *)".");
2483 else
2484 skip_until = vim_strsave(p);
2485 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002486
2487 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002488 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002489 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002490 if (*arg == '[')
2491 arg = vim_strchr(arg, ']');
2492 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002493 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002494 arg = skipwhite(skiptowhite(arg));
2495 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2496 && ((p[0] == 'l'
2497 && p[1] == 'e'
2498 && (!ASCII_ISALNUM(p[2])
2499 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002500 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002501 p = skipwhite(arg + 3);
2502 if (STRNCMP(p, "trim", 4) == 0)
2503 {
2504 // Ignore leading white space.
2505 p = skipwhite(p + 4);
2506 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002507 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002508 }
2509 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2510 do_concat = FALSE;
2511 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002512 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002513 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002514 }
2515
Bram Moolenaare38eab22019-12-05 21:50:01 +01002516 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002517 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002518 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519
Bram Moolenaare38eab22019-12-05 21:50:01 +01002520 // Copy the line to newly allocated memory. get_one_sourceline()
2521 // allocates 250 bytes per line, this saves 80% on average. The cost
2522 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002523 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002524 if (p == NULL)
2525 goto erret;
2526 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002527
Bram Moolenaare38eab22019-12-05 21:50:01 +01002528 // Add NULL lines for continuation lines, so that the line count is
2529 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002530 while (sourcing_lnum_off-- > 0)
2531 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2532
Bram Moolenaare38eab22019-12-05 21:50:01 +01002533 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002534 if (line_arg != NULL && *line_arg == NUL)
2535 line_arg = NULL;
2536 }
2537
Bram Moolenaare38eab22019-12-05 21:50:01 +01002538 // Don't define the function when skipping commands or when an error was
2539 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002540 if (eap->skip || did_emsg)
2541 goto erret;
2542
2543 /*
2544 * If there are no errors, add the function
2545 */
2546 if (fudi.fd_dict == NULL)
2547 {
2548 v = find_var(name, &ht, FALSE);
2549 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2550 {
2551 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2552 name);
2553 goto erret;
2554 }
2555
2556 fp = find_func(name);
2557 if (fp != NULL)
2558 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002559 // Function can be replaced with "function!" and when sourcing the
2560 // same script again, but only once.
2561 if (!eap->forceit
2562 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2563 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 {
2565 emsg_funcname(e_funcexts, name);
2566 goto erret;
2567 }
2568 if (fp->uf_calls > 0)
2569 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002570 emsg_funcname(
2571 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002572 name);
2573 goto erret;
2574 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002575 if (fp->uf_refcount > 1)
2576 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002577 // This function is referenced somewhere, don't redefine it but
2578 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002579 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002580 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002581 fp = NULL;
2582 overwrite = TRUE;
2583 }
2584 else
2585 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002586 // redefine existing function
Bram Moolenaard23a8232018-02-10 18:45:26 +01002587 VIM_CLEAR(name);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002588 func_clear_items(fp);
2589#ifdef FEAT_PROFILE
2590 fp->uf_profiling = FALSE;
2591 fp->uf_prof_initialized = FALSE;
2592#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002593 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002594 }
2595 }
2596 else
2597 {
2598 char numbuf[20];
2599
2600 fp = NULL;
2601 if (fudi.fd_newkey == NULL && !eap->forceit)
2602 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002603 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002604 goto erret;
2605 }
2606 if (fudi.fd_di == NULL)
2607 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002608 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002609 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 goto erret;
2611 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002612 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002613 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614 goto erret;
2615
Bram Moolenaare38eab22019-12-05 21:50:01 +01002616 // Give the function a sequential number. Can only be used with a
2617 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002618 vim_free(name);
2619 sprintf(numbuf, "%d", ++func_nr);
2620 name = vim_strsave((char_u *)numbuf);
2621 if (name == NULL)
2622 goto erret;
2623 }
2624
2625 if (fp == NULL)
2626 {
2627 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2628 {
2629 int slen, plen;
2630 char_u *scriptname;
2631
Bram Moolenaare38eab22019-12-05 21:50:01 +01002632 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002633 j = FAIL;
2634 if (sourcing_name != NULL)
2635 {
2636 scriptname = autoload_name(name);
2637 if (scriptname != NULL)
2638 {
2639 p = vim_strchr(scriptname, '/');
2640 plen = (int)STRLEN(p);
2641 slen = (int)STRLEN(sourcing_name);
2642 if (slen > plen && fnamecmp(p,
2643 sourcing_name + slen - plen) == 0)
2644 j = OK;
2645 vim_free(scriptname);
2646 }
2647 }
2648 if (j == FAIL)
2649 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002650 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002651 goto erret;
2652 }
2653 }
2654
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002655 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002656 if (fp == NULL)
2657 goto erret;
2658
2659 if (fudi.fd_dict != NULL)
2660 {
2661 if (fudi.fd_di == NULL)
2662 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002663 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002664 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2665 if (fudi.fd_di == NULL)
2666 {
2667 vim_free(fp);
2668 goto erret;
2669 }
2670 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2671 {
2672 vim_free(fudi.fd_di);
2673 vim_free(fp);
2674 goto erret;
2675 }
2676 }
2677 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002678 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002679 clear_tv(&fudi.fd_di->di_tv);
2680 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002681 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002682
Bram Moolenaare38eab22019-12-05 21:50:01 +01002683 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002684 flags |= FC_DICT;
2685 }
2686
Bram Moolenaare38eab22019-12-05 21:50:01 +01002687 // insert the new function in the function list
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002688 STRCPY(fp->uf_name, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002689 if (overwrite)
2690 {
2691 hi = hash_find(&func_hashtab, name);
2692 hi->hi_key = UF2HIKEY(fp);
2693 }
2694 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002695 {
2696 vim_free(fp);
2697 goto erret;
2698 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002699 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002700 }
2701 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002702 fp->uf_def_args = default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002703 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002704 if ((flags & FC_CLOSURE) != 0)
2705 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002706 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002707 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002708 }
2709 else
2710 fp->uf_scoped = NULL;
2711
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002712#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002713 if (prof_def_func())
2714 func_do_profile(fp);
2715#endif
2716 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02002717 if (sandbox)
2718 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002719 fp->uf_flags = flags;
2720 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002721 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002722 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002723 goto ret_free;
2724
2725erret:
2726 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002727 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002728errret_2:
2729 ga_clear_strings(&newlines);
2730ret_free:
2731 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002732 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002733 vim_free(fudi.fd_newkey);
2734 vim_free(name);
2735 did_emsg |= saved_did_emsg;
2736 need_wait_return |= saved_wait_return;
2737}
2738
2739/*
2740 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2741 * Return 2 if "p" starts with "s:".
2742 * Return 0 otherwise.
2743 */
2744 int
2745eval_fname_script(char_u *p)
2746{
Bram Moolenaare38eab22019-12-05 21:50:01 +01002747 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2748 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002749 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2750 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2751 return 5;
2752 if (p[0] == 's' && p[1] == ':')
2753 return 2;
2754 return 0;
2755}
2756
2757 int
2758translated_function_exists(char_u *name)
2759{
2760 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02002761 return has_internal_func(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002762 return find_func(name) != NULL;
2763}
2764
2765/*
2766 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002767 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002768 */
2769 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002770function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002771{
2772 char_u *nm = name;
2773 char_u *p;
2774 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002775 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002776
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002777 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2778 if (no_deref)
2779 flag |= TFN_NO_DEREF;
2780 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002781 nm = skipwhite(nm);
2782
Bram Moolenaare38eab22019-12-05 21:50:01 +01002783 // Only accept "funcname", "funcname ", "funcname (..." and
2784 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002785 if (p != NULL && (*nm == NUL || *nm == '('))
2786 n = translated_function_exists(p);
2787 vim_free(p);
2788 return n;
2789}
2790
Bram Moolenaar113e1072019-01-20 15:30:40 +01002791#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002792 char_u *
2793get_expanded_name(char_u *name, int check)
2794{
2795 char_u *nm = name;
2796 char_u *p;
2797
2798 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2799
2800 if (p != NULL && *nm == NUL)
2801 if (!check || translated_function_exists(p))
2802 return p;
2803
2804 vim_free(p);
2805 return NULL;
2806}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002807#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002808
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002809/*
2810 * Function given to ExpandGeneric() to obtain the list of user defined
2811 * function names.
2812 */
2813 char_u *
2814get_user_func_name(expand_T *xp, int idx)
2815{
2816 static long_u done;
2817 static hashitem_T *hi;
2818 ufunc_T *fp;
2819
2820 if (idx == 0)
2821 {
2822 done = 0;
2823 hi = func_hashtab.ht_array;
2824 }
2825 if (done < func_hashtab.ht_used)
2826 {
2827 if (done++ > 0)
2828 ++hi;
2829 while (HASHITEM_EMPTY(hi))
2830 ++hi;
2831 fp = HI2UF(hi);
2832
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002833 if ((fp->uf_flags & FC_DICT)
2834 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002835 return (char_u *)""; // don't show dict and lambda functions
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002836
2837 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002838 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002839
2840 cat_func_name(IObuff, fp);
2841 if (xp->xp_context != EXPAND_USER_FUNC)
2842 {
2843 STRCAT(IObuff, "(");
2844 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2845 STRCAT(IObuff, ")");
2846 }
2847 return IObuff;
2848 }
2849 return NULL;
2850}
2851
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002852/*
2853 * ":delfunction {name}"
2854 */
2855 void
2856ex_delfunction(exarg_T *eap)
2857{
2858 ufunc_T *fp = NULL;
2859 char_u *p;
2860 char_u *name;
2861 funcdict_T fudi;
2862
2863 p = eap->arg;
2864 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2865 vim_free(fudi.fd_newkey);
2866 if (name == NULL)
2867 {
2868 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002869 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002870 return;
2871 }
2872 if (!ends_excmd(*skipwhite(p)))
2873 {
2874 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002875 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002876 return;
2877 }
2878 eap->nextcmd = check_nextcmd(p);
2879 if (eap->nextcmd != NULL)
2880 *p = NUL;
2881
2882 if (!eap->skip)
2883 fp = find_func(name);
2884 vim_free(name);
2885
2886 if (!eap->skip)
2887 {
2888 if (fp == NULL)
2889 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02002890 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002891 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002892 return;
2893 }
2894 if (fp->uf_calls > 0)
2895 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002896 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002897 return;
2898 }
2899
2900 if (fudi.fd_dict != NULL)
2901 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002902 // Delete the dict item that refers to the function, it will
2903 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002904 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2905 }
2906 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002907 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002908 // A normal function (not a numbered function or lambda) has a
2909 // refcount of 1 for the entry in the hashtable. When deleting
2910 // it and the refcount is more than one, it should be kept.
2911 // A numbered function and lambda should be kept if the refcount is
2912 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002913 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002914 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002915 // Function is still referenced somewhere. Don't free it but
2916 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002917 if (func_remove(fp))
2918 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002919 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002920 }
2921 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002922 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002923 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002924 }
2925}
2926
2927/*
2928 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002929 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002930 */
2931 void
2932func_unref(char_u *name)
2933{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002934 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002935
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002936 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002937 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002938 fp = find_func(name);
2939 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002940 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002942 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002944 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002945 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002946 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002947 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002948 // Only delete it when it's not being used. Otherwise it's done
2949 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002950 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002951 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02002952 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002953}
2954
2955/*
2956 * Unreference a Function: decrement the reference count and free it when it
2957 * becomes zero.
2958 */
2959 void
2960func_ptr_unref(ufunc_T *fp)
2961{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002962 if (fp != NULL && --fp->uf_refcount <= 0)
2963 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002964 // Only delete it when it's not being used. Otherwise it's done
2965 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02002966 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002967 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002968 }
2969}
2970
2971/*
2972 * Count a reference to a Function.
2973 */
2974 void
2975func_ref(char_u *name)
2976{
2977 ufunc_T *fp;
2978
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002979 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002981 fp = find_func(name);
2982 if (fp != NULL)
2983 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002984 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01002985 // Only give an error for a numbered function.
2986 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01002987 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002988}
2989
2990/*
2991 * Count a reference to a Function.
2992 */
2993 void
2994func_ptr_ref(ufunc_T *fp)
2995{
2996 if (fp != NULL)
2997 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002998}
2999
3000/*
3001 * Return TRUE if items in "fc" do not have "copyID". That means they are not
3002 * referenced from anywhere that is in use.
3003 */
3004 static int
3005can_free_funccal(funccall_T *fc, int copyID)
3006{
3007 return (fc->l_varlist.lv_copyID != copyID
3008 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003009 && fc->l_avars.dv_copyID != copyID
3010 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003011}
3012
3013/*
3014 * ":return [expr]"
3015 */
3016 void
3017ex_return(exarg_T *eap)
3018{
3019 char_u *arg = eap->arg;
3020 typval_T rettv;
3021 int returning = FALSE;
3022
3023 if (current_funccal == NULL)
3024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003025 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003026 return;
3027 }
3028
3029 if (eap->skip)
3030 ++emsg_skip;
3031
3032 eap->nextcmd = NULL;
3033 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3034 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3035 {
3036 if (!eap->skip)
3037 returning = do_return(eap, FALSE, TRUE, &rettv);
3038 else
3039 clear_tv(&rettv);
3040 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003041 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003042 else if (!eap->skip)
3043 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003044 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003045 update_force_abort();
3046
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003047 /*
3048 * Return unless the expression evaluation has been cancelled due to an
3049 * aborting error, an interrupt, or an exception.
3050 */
3051 if (!aborting())
3052 returning = do_return(eap, FALSE, TRUE, NULL);
3053 }
3054
Bram Moolenaare38eab22019-12-05 21:50:01 +01003055 // When skipping or the return gets pending, advance to the next command
3056 // in this line (!returning). Otherwise, ignore the rest of the line.
3057 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003058 if (returning)
3059 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003060 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003061 eap->nextcmd = check_nextcmd(arg);
3062
3063 if (eap->skip)
3064 --emsg_skip;
3065}
3066
3067/*
3068 * ":1,25call func(arg1, arg2)" function call.
3069 */
3070 void
3071ex_call(exarg_T *eap)
3072{
3073 char_u *arg = eap->arg;
3074 char_u *startarg;
3075 char_u *name;
3076 char_u *tofree;
3077 int len;
3078 typval_T rettv;
3079 linenr_T lnum;
3080 int doesrange;
3081 int failed = FALSE;
3082 funcdict_T fudi;
3083 partial_T *partial = NULL;
3084
3085 if (eap->skip)
3086 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003087 // trans_function_name() doesn't work well when skipping, use eval0()
3088 // instead to skip to any following command, e.g. for:
3089 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003090 ++emsg_skip;
3091 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3092 clear_tv(&rettv);
3093 --emsg_skip;
3094 return;
3095 }
3096
3097 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3098 if (fudi.fd_newkey != NULL)
3099 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003100 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003101 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003102 vim_free(fudi.fd_newkey);
3103 }
3104 if (tofree == NULL)
3105 return;
3106
Bram Moolenaare38eab22019-12-05 21:50:01 +01003107 // Increase refcount on dictionary, it could get deleted when evaluating
3108 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003109 if (fudi.fd_dict != NULL)
3110 ++fudi.fd_dict->dv_refcount;
3111
Bram Moolenaare38eab22019-12-05 21:50:01 +01003112 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3113 // contents. For VAR_PARTIAL get its partial, unless we already have one
3114 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003115 len = (int)STRLEN(tofree);
3116 name = deref_func_name(tofree, &len,
3117 partial != NULL ? NULL : &partial, FALSE);
3118
Bram Moolenaare38eab22019-12-05 21:50:01 +01003119 // Skip white space to allow ":call func ()". Not good, but required for
3120 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003121 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003122 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003123
3124 if (*startarg != '(')
3125 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02003126 semsg(_(e_missingparen), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003127 goto end;
3128 }
3129
3130 /*
3131 * When skipping, evaluate the function once, to find the end of the
3132 * arguments.
3133 * When the function takes a range, this is discovered after the first
3134 * call, and the loop is broken.
3135 */
3136 if (eap->skip)
3137 {
3138 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003139 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003140 }
3141 else
3142 lnum = eap->line1;
3143 for ( ; lnum <= eap->line2; ++lnum)
3144 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003145 funcexe_T funcexe;
3146
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003147 if (!eap->skip && eap->addr_count > 0)
3148 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003149 if (lnum > curbuf->b_ml.ml_line_count)
3150 {
3151 // If the function deleted lines or switched to another buffer
3152 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003153 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003154 break;
3155 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003156 curwin->w_cursor.lnum = lnum;
3157 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003158 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003159 }
3160 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003161
Bram Moolenaarac92e252019-08-03 21:58:38 +02003162 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003163 funcexe.firstline = eap->line1;
3164 funcexe.lastline = eap->line2;
3165 funcexe.doesrange = &doesrange;
3166 funcexe.evaluate = !eap->skip;
3167 funcexe.partial = partial;
3168 funcexe.selfdict = fudi.fd_dict;
3169 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003170 {
3171 failed = TRUE;
3172 break;
3173 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003174 if (has_watchexpr())
3175 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003177 // Handle a function returning a Funcref, Dictionary or List.
3178 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3179 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003180 {
3181 failed = TRUE;
3182 break;
3183 }
3184
3185 clear_tv(&rettv);
3186 if (doesrange || eap->skip)
3187 break;
3188
Bram Moolenaare38eab22019-12-05 21:50:01 +01003189 // Stop when immediately aborting on error, or when an interrupt
3190 // occurred or an exception was thrown but not caught.
3191 // get_func_tv() returned OK, so that the check for trailing
3192 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003193 if (aborting())
3194 break;
3195 }
3196 if (eap->skip)
3197 --emsg_skip;
3198
3199 if (!failed)
3200 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003201 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202 if (!ends_excmd(*arg))
3203 {
3204 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003205 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003206 }
3207 else
3208 eap->nextcmd = check_nextcmd(arg);
3209 }
3210
3211end:
3212 dict_unref(fudi.fd_dict);
3213 vim_free(tofree);
3214}
3215
3216/*
3217 * Return from a function. Possibly makes the return pending. Also called
3218 * for a pending return at the ":endtry" or after returning from an extra
3219 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3220 * when called due to a ":return" command. "rettv" may point to a typval_T
3221 * with the return rettv. Returns TRUE when the return can be carried out,
3222 * FALSE when the return gets pending.
3223 */
3224 int
3225do_return(
3226 exarg_T *eap,
3227 int reanimate,
3228 int is_cmd,
3229 void *rettv)
3230{
3231 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003232 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003233
3234 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003235 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003236 current_funccal->returned = FALSE;
3237
3238 /*
3239 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3240 * not in its finally clause (which then is to be executed next) is found.
3241 * In this case, make the ":return" pending for execution at the ":endtry".
3242 * Otherwise, return normally.
3243 */
3244 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3245 if (idx >= 0)
3246 {
3247 cstack->cs_pending[idx] = CSTP_RETURN;
3248
3249 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003250 // A pending return again gets pending. "rettv" points to an
3251 // allocated variable with the rettv of the original ":return"'s
3252 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003253 cstack->cs_rettv[idx] = rettv;
3254 else
3255 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003256 // When undoing a return in order to make it pending, get the stored
3257 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003258 if (reanimate)
3259 rettv = current_funccal->rettv;
3260
3261 if (rettv != NULL)
3262 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003263 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003264 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3265 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3266 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003267 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003268 }
3269 else
3270 cstack->cs_rettv[idx] = NULL;
3271
3272 if (reanimate)
3273 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003274 // The pending return value could be overwritten by a ":return"
3275 // without argument in a finally clause; reset the default
3276 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003277 current_funccal->rettv->v_type = VAR_NUMBER;
3278 current_funccal->rettv->vval.v_number = 0;
3279 }
3280 }
3281 report_make_pending(CSTP_RETURN, rettv);
3282 }
3283 else
3284 {
3285 current_funccal->returned = TRUE;
3286
Bram Moolenaare38eab22019-12-05 21:50:01 +01003287 // If the return is carried out now, store the return value. For
3288 // a return immediately after reanimation, the value is already
3289 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003290 if (!reanimate && rettv != NULL)
3291 {
3292 clear_tv(current_funccal->rettv);
3293 *current_funccal->rettv = *(typval_T *)rettv;
3294 if (!is_cmd)
3295 vim_free(rettv);
3296 }
3297 }
3298
3299 return idx < 0;
3300}
3301
3302/*
3303 * Free the variable with a pending return value.
3304 */
3305 void
3306discard_pending_return(void *rettv)
3307{
3308 free_tv((typval_T *)rettv);
3309}
3310
3311/*
3312 * Generate a return command for producing the value of "rettv". The result
3313 * is an allocated string. Used by report_pending() for verbose messages.
3314 */
3315 char_u *
3316get_return_cmd(void *rettv)
3317{
3318 char_u *s = NULL;
3319 char_u *tofree = NULL;
3320 char_u numbuf[NUMBUFLEN];
3321
3322 if (rettv != NULL)
3323 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3324 if (s == NULL)
3325 s = (char_u *)"";
3326
3327 STRCPY(IObuff, ":return ");
3328 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3329 if (STRLEN(s) + 8 >= IOSIZE)
3330 STRCPY(IObuff + IOSIZE - 4, "...");
3331 vim_free(tofree);
3332 return vim_strsave(IObuff);
3333}
3334
3335/*
3336 * Get next function line.
3337 * Called by do_cmdline() to get the next line.
3338 * Returns allocated string, or NULL for end of function.
3339 */
3340 char_u *
3341get_func_line(
3342 int c UNUSED,
3343 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003344 int indent UNUSED,
3345 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003346{
3347 funccall_T *fcp = (funccall_T *)cookie;
3348 ufunc_T *fp = fcp->func;
3349 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003350 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003351
Bram Moolenaare38eab22019-12-05 21:50:01 +01003352 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 if (fcp->dbg_tick != debug_tick)
3354 {
3355 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3356 sourcing_lnum);
3357 fcp->dbg_tick = debug_tick;
3358 }
3359#ifdef FEAT_PROFILE
3360 if (do_profiling == PROF_YES)
3361 func_line_end(cookie);
3362#endif
3363
3364 gap = &fp->uf_lines;
3365 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3366 || fcp->returned)
3367 retval = NULL;
3368 else
3369 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003370 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003371 while (fcp->linenr < gap->ga_len
3372 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3373 ++fcp->linenr;
3374 if (fcp->linenr >= gap->ga_len)
3375 retval = NULL;
3376 else
3377 {
3378 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
3379 sourcing_lnum = fcp->linenr;
3380#ifdef FEAT_PROFILE
3381 if (do_profiling == PROF_YES)
3382 func_line_start(cookie);
3383#endif
3384 }
3385 }
3386
Bram Moolenaare38eab22019-12-05 21:50:01 +01003387 // Did we encounter a breakpoint?
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
3389 {
3390 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003391 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003392 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
3393 sourcing_lnum);
3394 fcp->dbg_tick = debug_tick;
3395 }
3396
3397 return retval;
3398}
3399
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003400/*
3401 * Return TRUE if the currently active function should be ended, because a
3402 * return was encountered or an error occurred. Used inside a ":while".
3403 */
3404 int
3405func_has_ended(void *cookie)
3406{
3407 funccall_T *fcp = (funccall_T *)cookie;
3408
Bram Moolenaare38eab22019-12-05 21:50:01 +01003409 // Ignore the "abort" flag if the abortion behavior has been changed due to
3410 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003411 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3412 || fcp->returned);
3413}
3414
3415/*
3416 * return TRUE if cookie indicates a function which "abort"s on errors.
3417 */
3418 int
3419func_has_abort(
3420 void *cookie)
3421{
3422 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3423}
3424
3425
3426/*
3427 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3428 * Don't do this when "Func" is already a partial that was bound
3429 * explicitly (pt_auto is FALSE).
3430 * Changes "rettv" in-place.
3431 * Returns the updated "selfdict_in".
3432 */
3433 dict_T *
3434make_partial(dict_T *selfdict_in, typval_T *rettv)
3435{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003436 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003437 char_u *tofree = NULL;
3438 ufunc_T *fp;
3439 char_u fname_buf[FLEN_FIXED + 1];
3440 int error;
3441 dict_T *selfdict = selfdict_in;
3442
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003443 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3444 fp = rettv->vval.v_partial->pt_func;
3445 else
3446 {
3447 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3448 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003449 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003450 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3451 fp = find_func(fname);
3452 vim_free(tofree);
3453 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454
3455 if (fp != NULL && (fp->uf_flags & FC_DICT))
3456 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003457 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003458
3459 if (pt != NULL)
3460 {
3461 pt->pt_refcount = 1;
3462 pt->pt_dict = selfdict;
3463 pt->pt_auto = TRUE;
3464 selfdict = NULL;
3465 if (rettv->v_type == VAR_FUNC)
3466 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003467 // Just a function: Take over the function name and use
3468 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003469 pt->pt_name = rettv->vval.v_string;
3470 }
3471 else
3472 {
3473 partial_T *ret_pt = rettv->vval.v_partial;
3474 int i;
3475
Bram Moolenaare38eab22019-12-05 21:50:01 +01003476 // Partial: copy the function name, use selfdict and copy
3477 // args. Can't take over name or args, the partial might
3478 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003479 if (ret_pt->pt_name != NULL)
3480 {
3481 pt->pt_name = vim_strsave(ret_pt->pt_name);
3482 func_ref(pt->pt_name);
3483 }
3484 else
3485 {
3486 pt->pt_func = ret_pt->pt_func;
3487 func_ptr_ref(pt->pt_func);
3488 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003489 if (ret_pt->pt_argc > 0)
3490 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003491 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003492 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003493 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003494 pt->pt_argc = 0;
3495 else
3496 {
3497 pt->pt_argc = ret_pt->pt_argc;
3498 for (i = 0; i < pt->pt_argc; i++)
3499 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3500 }
3501 }
3502 partial_unref(ret_pt);
3503 }
3504 rettv->v_type = VAR_PARTIAL;
3505 rettv->vval.v_partial = pt;
3506 }
3507 }
3508 return selfdict;
3509}
3510
3511/*
3512 * Return the name of the executed function.
3513 */
3514 char_u *
3515func_name(void *cookie)
3516{
3517 return ((funccall_T *)cookie)->func->uf_name;
3518}
3519
3520/*
3521 * Return the address holding the next breakpoint line for a funccall cookie.
3522 */
3523 linenr_T *
3524func_breakpoint(void *cookie)
3525{
3526 return &((funccall_T *)cookie)->breakpoint;
3527}
3528
3529/*
3530 * Return the address holding the debug tick for a funccall cookie.
3531 */
3532 int *
3533func_dbg_tick(void *cookie)
3534{
3535 return &((funccall_T *)cookie)->dbg_tick;
3536}
3537
3538/*
3539 * Return the nesting level for a funccall cookie.
3540 */
3541 int
3542func_level(void *cookie)
3543{
3544 return ((funccall_T *)cookie)->level;
3545}
3546
3547/*
3548 * Return TRUE when a function was ended by a ":return" command.
3549 */
3550 int
3551current_func_returned(void)
3552{
3553 return current_funccal->returned;
3554}
3555
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003556 int
3557free_unref_funccal(int copyID, int testing)
3558{
3559 int did_free = FALSE;
3560 int did_free_funccal = FALSE;
3561 funccall_T *fc, **pfc;
3562
3563 for (pfc = &previous_funccal; *pfc != NULL; )
3564 {
3565 if (can_free_funccal(*pfc, copyID))
3566 {
3567 fc = *pfc;
3568 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003569 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003570 did_free = TRUE;
3571 did_free_funccal = TRUE;
3572 }
3573 else
3574 pfc = &(*pfc)->caller;
3575 }
3576 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003577 // When a funccal was freed some more items might be garbage
3578 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003579 (void)garbage_collect(testing);
3580
3581 return did_free;
3582}
3583
3584/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003585 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003586 */
3587 static funccall_T *
3588get_funccal(void)
3589{
3590 int i;
3591 funccall_T *funccal;
3592 funccall_T *temp_funccal;
3593
3594 funccal = current_funccal;
3595 if (debug_backtrace_level > 0)
3596 {
3597 for (i = 0; i < debug_backtrace_level; i++)
3598 {
3599 temp_funccal = funccal->caller;
3600 if (temp_funccal)
3601 funccal = temp_funccal;
3602 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003603 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003604 debug_backtrace_level = i;
3605 }
3606 }
3607 return funccal;
3608}
3609
3610/*
3611 * Return the hashtable used for local variables in the current funccal.
3612 * Return NULL if there is no current funccal.
3613 */
3614 hashtab_T *
3615get_funccal_local_ht()
3616{
3617 if (current_funccal == NULL)
3618 return NULL;
3619 return &get_funccal()->l_vars.dv_hashtab;
3620}
3621
3622/*
3623 * Return the l: scope variable.
3624 * Return NULL if there is no current funccal.
3625 */
3626 dictitem_T *
3627get_funccal_local_var()
3628{
3629 if (current_funccal == NULL)
3630 return NULL;
3631 return &get_funccal()->l_vars_var;
3632}
3633
3634/*
3635 * Return the hashtable used for argument in the current funccal.
3636 * Return NULL if there is no current funccal.
3637 */
3638 hashtab_T *
3639get_funccal_args_ht()
3640{
3641 if (current_funccal == NULL)
3642 return NULL;
3643 return &get_funccal()->l_avars.dv_hashtab;
3644}
3645
3646/*
3647 * Return the a: scope variable.
3648 * Return NULL if there is no current funccal.
3649 */
3650 dictitem_T *
3651get_funccal_args_var()
3652{
3653 if (current_funccal == NULL)
3654 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01003655 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003656}
3657
3658/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003659 * List function variables, if there is a function.
3660 */
3661 void
3662list_func_vars(int *first)
3663{
3664 if (current_funccal != NULL)
3665 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01003666 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003667}
3668
3669/*
3670 * If "ht" is the hashtable for local variables in the current funccal, return
3671 * the dict that contains it.
3672 * Otherwise return NULL.
3673 */
3674 dict_T *
3675get_current_funccal_dict(hashtab_T *ht)
3676{
3677 if (current_funccal != NULL
3678 && ht == &current_funccal->l_vars.dv_hashtab)
3679 return &current_funccal->l_vars;
3680 return NULL;
3681}
3682
3683/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003684 * Search hashitem in parent scope.
3685 */
3686 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003687find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003688{
3689 funccall_T *old_current_funccal = current_funccal;
3690 hashtab_T *ht;
3691 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003692 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003693
3694 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3695 return NULL;
3696
Bram Moolenaare38eab22019-12-05 21:50:01 +01003697 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003698 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003699 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003700 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003701 ht = find_var_ht(name, &varname);
3702 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003703 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003704 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003705 if (!HASHITEM_EMPTY(hi))
3706 {
3707 *pht = ht;
3708 break;
3709 }
3710 }
3711 if (current_funccal == current_funccal->func->uf_scoped)
3712 break;
3713 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003714 }
3715 current_funccal = old_current_funccal;
3716
3717 return hi;
3718}
3719
3720/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003721 * Search variable in parent scope.
3722 */
3723 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003724find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003725{
3726 dictitem_T *v = NULL;
3727 funccall_T *old_current_funccal = current_funccal;
3728 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003729 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003730
3731 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3732 return NULL;
3733
Bram Moolenaare38eab22019-12-05 21:50:01 +01003734 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003735 current_funccal = current_funccal->func->uf_scoped;
3736 while (current_funccal)
3737 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003738 ht = find_var_ht(name, &varname);
3739 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003740 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003741 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003742 if (v != NULL)
3743 break;
3744 }
3745 if (current_funccal == current_funccal->func->uf_scoped)
3746 break;
3747 current_funccal = current_funccal->func->uf_scoped;
3748 }
3749 current_funccal = old_current_funccal;
3750
3751 return v;
3752}
3753
3754/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003755 * Set "copyID + 1" in previous_funccal and callers.
3756 */
3757 int
3758set_ref_in_previous_funccal(int copyID)
3759{
3760 int abort = FALSE;
3761 funccall_T *fc;
3762
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003763 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003764 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003765 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003766 abort = abort
3767 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
3768 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003769 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003770 }
3771 return abort;
3772}
3773
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003774 static int
3775set_ref_in_funccal(funccall_T *fc, int copyID)
3776{
3777 int abort = FALSE;
3778
3779 if (fc->fc_copyID != copyID)
3780 {
3781 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003782 abort = abort
3783 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
3784 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003785 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003786 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003787 }
3788 return abort;
3789}
3790
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003791/*
3792 * Set "copyID" in all local vars and arguments in the call stack.
3793 */
3794 int
3795set_ref_in_call_stack(int copyID)
3796{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003797 int abort = FALSE;
3798 funccall_T *fc;
3799 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003800
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003801 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003802 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003803
3804 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003805 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
3806 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003807 abort = abort || set_ref_in_funccal(fc, copyID);
3808
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003809 return abort;
3810}
3811
3812/*
3813 * Set "copyID" in all functions available by name.
3814 */
3815 int
3816set_ref_in_functions(int copyID)
3817{
3818 int todo;
3819 hashitem_T *hi = NULL;
3820 int abort = FALSE;
3821 ufunc_T *fp;
3822
3823 todo = (int)func_hashtab.ht_used;
3824 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003825 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003826 if (!HASHITEM_EMPTY(hi))
3827 {
3828 --todo;
3829 fp = HI2UF(hi);
3830 if (!func_name_refcount(fp->uf_name))
3831 abort = abort || set_ref_in_func(NULL, fp, copyID);
3832 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003833 }
3834 return abort;
3835}
3836
3837/*
3838 * Set "copyID" in all function arguments.
3839 */
3840 int
3841set_ref_in_func_args(int copyID)
3842{
3843 int i;
3844 int abort = FALSE;
3845
3846 for (i = 0; i < funcargs.ga_len; ++i)
3847 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3848 copyID, NULL, NULL);
3849 return abort;
3850}
3851
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003852/*
3853 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003854 * Returns TRUE if setting references failed somehow.
3855 */
3856 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003857set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003858{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003859 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003860 funccall_T *fc;
3861 int error = ERROR_NONE;
3862 char_u fname_buf[FLEN_FIXED + 1];
3863 char_u *tofree = NULL;
3864 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003865 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003866
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003867 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003868 return FALSE;
3869
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003870 if (fp_in == NULL)
3871 {
3872 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3873 fp = find_func(fname);
3874 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003875 if (fp != NULL)
3876 {
3877 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003878 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003879 }
3880 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003881 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003882}
3883
Bram Moolenaare38eab22019-12-05 21:50:01 +01003884#endif // FEAT_EVAL