blob: 29e0fac050e85a4dbf1385616205cb49fd628f30 [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
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100229 static void
230set_ufunc_name(ufunc_T *fp, char_u *name)
231{
232 STRCPY(fp->uf_name, name);
233
234 if (name[0] == K_SPECIAL)
235 {
236 fp->uf_name_exp = alloc(STRLEN(name) + 3);
237 if (fp->uf_name_exp != NULL)
238 {
239 STRCPY(fp->uf_name_exp, "<SNR>");
240 STRCAT(fp->uf_name_exp, fp->uf_name + 3);
241 }
242 }
243}
244
Bram Moolenaar58016442016-07-31 18:30:22 +0200245/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200246 * Parse a lambda expression and get a Funcref from "*arg".
247 * Return OK or FAIL. Returns NOTDONE for dict or {expr}.
248 */
249 int
250get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
251{
252 garray_T newargs;
253 garray_T newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200254 garray_T *pnewargs;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200255 ufunc_T *fp = NULL;
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100256 partial_T *pt = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200257 int varargs;
258 int ret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200259 char_u *start = skipwhite(*arg + 1);
260 char_u *s, *e;
261 static int lambda_no = 0;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200262 int *old_eval_lavars = eval_lavars_used;
263 int eval_lavars = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200264
265 ga_init(&newargs);
266 ga_init(&newlines);
267
Bram Moolenaare38eab22019-12-05 21:50:01 +0100268 // First, check if this is a lambda expression. "->" must exist.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200269 ret = get_function_args(&start, '-', NULL, NULL, NULL, TRUE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200270 if (ret == FAIL || *start != '>')
271 return NOTDONE;
272
Bram Moolenaare38eab22019-12-05 21:50:01 +0100273 // Parse the arguments again.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200274 if (evaluate)
275 pnewargs = &newargs;
276 else
277 pnewargs = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200278 *arg = skipwhite(*arg + 1);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200279 ret = get_function_args(arg, '-', pnewargs, &varargs, NULL, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200280 if (ret == FAIL || **arg != '>')
281 goto errret;
282
Bram Moolenaare38eab22019-12-05 21:50:01 +0100283 // Set up a flag for checking local variables and arguments.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200284 if (evaluate)
285 eval_lavars_used = &eval_lavars;
286
Bram Moolenaare38eab22019-12-05 21:50:01 +0100287 // Get the start and the end of the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200288 *arg = skipwhite(*arg + 1);
289 s = *arg;
290 ret = skip_expr(arg);
291 if (ret == FAIL)
292 goto errret;
293 e = *arg;
294 *arg = skipwhite(*arg);
295 if (**arg != '}')
296 goto errret;
297 ++*arg;
298
299 if (evaluate)
300 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200301 int len, flags = 0;
302 char_u *p;
303 char_u name[20];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200304
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200305 sprintf((char*)name, "<lambda>%d", ++lambda_no);
306
Bram Moolenaar47ed5532019-08-08 20:49:14 +0200307 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200308 if (fp == NULL)
309 goto errret;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200310 pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200311 if (pt == NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200312 goto errret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200313
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200314 ga_init2(&newlines, (int)sizeof(char_u *), 1);
315 if (ga_grow(&newlines, 1) == FAIL)
316 goto errret;
317
Bram Moolenaare38eab22019-12-05 21:50:01 +0100318 // Add "return " before the expression.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200319 len = 7 + e - s + 1;
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200320 p = alloc(len);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200321 if (p == NULL)
322 goto errret;
323 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
324 STRCPY(p, "return ");
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200325 vim_strncpy(p + 7, s, e - s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200326
327 fp->uf_refcount = 1;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100328 set_ufunc_name(fp, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200329 hash_add(&func_hashtab, UF2HIKEY(fp));
330 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200331 ga_init(&fp->uf_def_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200332 fp->uf_lines = newlines;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200333 if (current_funccal != NULL && eval_lavars)
334 {
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200335 flags |= FC_CLOSURE;
Bram Moolenaar58016442016-07-31 18:30:22 +0200336 if (register_closure(fp) == FAIL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200337 goto errret;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200338 }
339 else
340 fp->uf_scoped = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200341
342#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200343 if (prof_def_func())
344 func_do_profile(fp);
345#endif
Bram Moolenaar93343722018-07-10 19:39:18 +0200346 if (sandbox)
347 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200348 fp->uf_varargs = TRUE;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +0200349 fp->uf_flags = flags;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200350 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200351 fp->uf_script_ctx = current_sctx;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100352 fp->uf_script_ctx.sc_lnum += SOURCING_LNUM - newlines.ga_len;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200353
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200354 pt->pt_func = fp;
355 pt->pt_refcount = 1;
356 rettv->vval.v_partial = pt;
357 rettv->v_type = VAR_PARTIAL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200358 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200359
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200360 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200361 return OK;
362
363errret:
364 ga_clear_strings(&newargs);
365 ga_clear_strings(&newlines);
366 vim_free(fp);
Bram Moolenaar445e71c2019-02-14 13:43:36 +0100367 vim_free(pt);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200368 eval_lavars_used = old_eval_lavars;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200369 return FAIL;
370}
371
372/*
373 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
374 * name it contains, otherwise return "name".
375 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
376 * "partialp".
377 */
378 char_u *
379deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
380{
381 dictitem_T *v;
382 int cc;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200383 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200384
385 if (partialp != NULL)
386 *partialp = NULL;
387
388 cc = name[*lenp];
389 name[*lenp] = NUL;
390 v = find_var(name, NULL, no_autoload);
391 name[*lenp] = cc;
392 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
393 {
394 if (v->di_tv.vval.v_string == NULL)
395 {
396 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100397 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200398 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200399 s = v->di_tv.vval.v_string;
400 *lenp = (int)STRLEN(s);
401 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200402 }
403
404 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
405 {
406 partial_T *pt = v->di_tv.vval.v_partial;
407
408 if (pt == NULL)
409 {
410 *lenp = 0;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100411 return (char_u *)""; // just in case
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200412 }
413 if (partialp != NULL)
414 *partialp = pt;
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200415 s = partial_name(pt);
416 *lenp = (int)STRLEN(s);
417 return s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200418 }
419
420 return name;
421}
422
423/*
424 * Give an error message with a function name. Handle <SNR> things.
425 * "ermsg" is to be passed without translation, use N_() instead of _().
426 */
Bram Moolenaar4c054e92019-11-10 00:13:50 +0100427 void
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200428emsg_funcname(char *ermsg, char_u *name)
429{
430 char_u *p;
431
432 if (*name == K_SPECIAL)
433 p = concat_str((char_u *)"<SNR>", name + 3);
434 else
435 p = name;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100436 semsg(_(ermsg), p);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200437 if (p != name)
438 vim_free(p);
439}
440
441/*
442 * Allocate a variable for the result of a function.
443 * Return OK or FAIL.
444 */
445 int
446get_func_tv(
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200447 char_u *name, // name of the function
448 int len, // length of "name" or -1 to use strlen()
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200449 typval_T *rettv,
Bram Moolenaar6ed88192019-05-11 18:37:44 +0200450 char_u **arg, // argument, pointing to the '('
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200451 funcexe_T *funcexe) // various values
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200452{
453 char_u *argp;
454 int ret = OK;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100455 typval_T argvars[MAX_FUNC_ARGS + 1]; // vars for arguments
456 int argcount = 0; // number of arguments found
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200457
458 /*
459 * Get the arguments.
460 */
461 argp = *arg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200462 while (argcount < MAX_FUNC_ARGS - (funcexe->partial == NULL ? 0
463 : funcexe->partial->pt_argc))
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200464 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100465 argp = skipwhite(argp + 1); // skip the '(' or ','
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200466 if (*argp == ')' || *argp == ',' || *argp == NUL)
467 break;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200468 if (eval1(&argp, &argvars[argcount], funcexe->evaluate) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200469 {
470 ret = FAIL;
471 break;
472 }
473 ++argcount;
474 if (*argp != ',')
475 break;
476 }
477 if (*argp == ')')
478 ++argp;
479 else
480 ret = FAIL;
481
482 if (ret == OK)
483 {
484 int i = 0;
485
486 if (get_vim_var_nr(VV_TESTING))
487 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100488 // Prepare for calling test_garbagecollect_now(), need to know
489 // what variables are used on the call stack.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200490 if (funcargs.ga_itemsize == 0)
491 ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
492 for (i = 0; i < argcount; ++i)
493 if (ga_grow(&funcargs, 1) == OK)
494 ((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
495 &argvars[i];
496 }
497
Bram Moolenaarc6538bc2019-08-03 18:17:11 +0200498 ret = call_func(name, len, rettv, argcount, argvars, funcexe);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200499
500 funcargs.ga_len -= i;
501 }
502 else if (!aborting())
503 {
504 if (argcount == MAX_FUNC_ARGS)
505 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
506 else
507 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
508 }
509
510 while (--argcount >= 0)
511 clear_tv(&argvars[argcount]);
512
513 *arg = skipwhite(argp);
514 return ret;
515}
516
517#define FLEN_FIXED 40
518
519/*
520 * Return TRUE if "p" starts with "<SID>" or "s:".
521 * Only works if eval_fname_script() returned non-zero for "p"!
522 */
523 static int
524eval_fname_sid(char_u *p)
525{
526 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
527}
528
529/*
530 * In a script change <SID>name() and s:name() to K_SNR 123_name().
531 * Change <SNR>123_name() to K_SNR 123_name().
532 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
533 * (slow).
534 */
535 static char_u *
536fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
537{
538 int llen;
539 char_u *fname;
540 int i;
541
542 llen = eval_fname_script(name);
543 if (llen > 0)
544 {
545 fname_buf[0] = K_SPECIAL;
546 fname_buf[1] = KS_EXTRA;
547 fname_buf[2] = (int)KE_SNR;
548 i = 3;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100549 if (eval_fname_sid(name)) // "<SID>" or "s:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200550 {
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200551 if (current_sctx.sc_sid <= 0)
Bram Moolenaaref140542019-12-31 21:27:13 +0100552 *error = FCERR_SCRIPT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200553 else
554 {
Bram Moolenaarad3ec762019-04-21 00:00:13 +0200555 sprintf((char *)fname_buf + 3, "%ld_",
556 (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200557 i = (int)STRLEN(fname_buf);
558 }
559 }
560 if (i + STRLEN(name + llen) < FLEN_FIXED)
561 {
562 STRCPY(fname_buf + i, name + llen);
563 fname = fname_buf;
564 }
565 else
566 {
Bram Moolenaar964b3742019-05-24 18:54:09 +0200567 fname = alloc(i + STRLEN(name + llen) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200568 if (fname == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +0100569 *error = FCERR_OTHER;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200570 else
571 {
572 *tofree = fname;
573 mch_memmove(fname, fname_buf, (size_t)i);
574 STRCPY(fname + i, name + llen);
575 }
576 }
577 }
578 else
579 fname = name;
580 return fname;
581}
582
583/*
584 * Find a function by name, return pointer to it in ufuncs.
585 * Return NULL for unknown function.
586 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200587 ufunc_T *
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200588find_func(char_u *name)
589{
590 hashitem_T *hi;
591
592 hi = hash_find(&func_hashtab, name);
593 if (!HASHITEM_EMPTY(hi))
594 return HI2UF(hi);
595 return NULL;
596}
597
598/*
599 * Copy the function name of "fp" to buffer "buf".
600 * "buf" must be able to hold the function name plus three bytes.
601 * Takes care of script-local function names.
602 */
603 static void
604cat_func_name(char_u *buf, ufunc_T *fp)
605{
606 if (fp->uf_name[0] == K_SPECIAL)
607 {
608 STRCPY(buf, "<SNR>");
609 STRCAT(buf, fp->uf_name + 3);
610 }
611 else
612 STRCPY(buf, fp->uf_name);
613}
614
615/*
616 * Add a number variable "name" to dict "dp" with value "nr".
617 */
618 static void
619add_nr_var(
620 dict_T *dp,
621 dictitem_T *v,
622 char *name,
623 varnumber_T nr)
624{
625 STRCPY(v->di_key, name);
626 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
627 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
628 v->di_tv.v_type = VAR_NUMBER;
629 v->di_tv.v_lock = VAR_FIXED;
630 v->di_tv.vval.v_number = nr;
631}
632
633/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100634 * Free "fc".
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200635 */
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100636 static void
637free_funccal(funccall_T *fc)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200638{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100639 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200640
641 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
642 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100643 ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200644
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100645 // When garbage collecting a funccall_T may be freed before the
646 // function that references it, clear its uf_scoped field.
647 // The function may have been redefined and point to another
648 // funccall_T, don't clear it then.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +0200649 if (fp != NULL && fp->uf_scoped == fc)
650 fp->uf_scoped = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200651 }
Bram Moolenaar58016442016-07-31 18:30:22 +0200652 ga_clear(&fc->fc_funcs);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200653
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200654 func_ptr_unref(fc->func);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200655 vim_free(fc);
656}
657
658/*
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100659 * Free "fc" and what it contains.
660 * Can be called only when "fc" is kept beyond the period of it called,
661 * i.e. after cleanup_function_call(fc).
662 */
663 static void
664free_funccal_contents(funccall_T *fc)
665{
666 listitem_T *li;
667
668 // Free all l: variables.
669 vars_clear(&fc->l_vars.dv_hashtab);
670
671 // Free all a: variables.
672 vars_clear(&fc->l_avars.dv_hashtab);
673
674 // Free the a:000 variables.
675 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
676 clear_tv(&li->li_tv);
677
678 free_funccal(fc);
679}
680
681/*
Bram Moolenaar6914c642017-04-01 21:21:30 +0200682 * Handle the last part of returning from a function: free the local hashtable.
683 * Unless it is still in use by a closure.
684 */
685 static void
686cleanup_function_call(funccall_T *fc)
687{
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100688 int may_free_fc = fc->fc_refcount <= 0;
689 int free_fc = TRUE;
690
Bram Moolenaar6914c642017-04-01 21:21:30 +0200691 current_funccal = fc->caller;
692
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100693 // Free all l: variables if not referred.
694 if (may_free_fc && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT)
695 vars_clear(&fc->l_vars.dv_hashtab);
696 else
697 free_fc = FALSE;
698
699 // If the a:000 list and the l: and a: dicts are not referenced and
700 // there is no closure using it, we can free the funccall_T and what's
701 // in it.
702 if (may_free_fc && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
703 vars_clear_ext(&fc->l_avars.dv_hashtab, FALSE);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200704 else
705 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100706 int todo;
707 hashitem_T *hi;
708 dictitem_T *di;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200709
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100710 free_fc = FALSE;
Bram Moolenaar6914c642017-04-01 21:21:30 +0200711
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100712 // Make a copy of the a: variables, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200713 todo = (int)fc->l_avars.dv_hashtab.ht_used;
714 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
715 {
716 if (!HASHITEM_EMPTY(hi))
717 {
718 --todo;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100719 di = HI2DI(hi);
720 copy_tv(&di->di_tv, &di->di_tv);
Bram Moolenaar6914c642017-04-01 21:21:30 +0200721 }
722 }
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100723 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200724
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100725 if (may_free_fc && fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT)
726 fc->l_varlist.lv_first = NULL;
727 else
728 {
729 listitem_T *li;
730
731 free_fc = FALSE;
732
733 // Make a copy of the a:000 items, since we didn't do that above.
Bram Moolenaar6914c642017-04-01 21:21:30 +0200734 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
735 copy_tv(&li->li_tv, &li->li_tv);
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100736 }
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100737
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100738 if (free_fc)
739 free_funccal(fc);
740 else
741 {
742 static int made_copy = 0;
743
744 // "fc" is still in use. This can happen when returning "a:000",
745 // assigning "l:" to a global variable or defining a closure.
746 // Link "fc" in the list for garbage collection later.
747 fc->caller = previous_funccal;
748 previous_funccal = fc;
749
750 if (want_garbage_collect)
751 // If garbage collector is ready, clear count.
752 made_copy = 0;
753 else if (++made_copy >= (int)((4096 * 1024) / sizeof(*fc)))
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100754 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100755 // We have made a lot of copies, worth 4 Mbyte. This can happen
756 // when repetitively calling a function that creates a reference to
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100757 // itself somehow. Call the garbage collector soon to avoid using
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100758 // too much memory.
759 made_copy = 0;
Bram Moolenaar889da2f2019-02-02 14:02:30 +0100760 want_garbage_collect = TRUE;
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100761 }
Bram Moolenaar6914c642017-04-01 21:21:30 +0200762 }
763}
764
765/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200766 * Call a user function.
767 */
768 static void
769call_user_func(
Bram Moolenaare38eab22019-12-05 21:50:01 +0100770 ufunc_T *fp, // pointer to function
771 int argcount, // nr of args
772 typval_T *argvars, // arguments
773 typval_T *rettv, // return value
774 linenr_T firstline, // first line of range
775 linenr_T lastline, // last line of range
776 dict_T *selfdict) // Dictionary for "self"
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200777{
Bram Moolenaarf29c1c62018-09-10 21:05:02 +0200778 sctx_T save_current_sctx;
Bram Moolenaar93343722018-07-10 19:39:18 +0200779 int using_sandbox = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200780 funccall_T *fc;
781 int save_did_emsg;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200782 int default_arg_err = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200783 static int depth = 0;
784 dictitem_T *v;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100785 int fixvar_idx = 0; // index in fixvar[]
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200786 int i;
787 int ai;
788 int islambda = FALSE;
789 char_u numbuf[NUMBUFLEN];
790 char_u *name;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200791#ifdef FEAT_PROFILE
792 proftime_T wait_start;
793 proftime_T call_start;
Bram Moolenaarad648092018-06-30 18:28:03 +0200794 int started_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200795#endif
Bram Moolenaare31ee862020-01-07 20:59:34 +0100796 ESTACK_CHECK_DECLARATION
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200797
Bram Moolenaare38eab22019-12-05 21:50:01 +0100798 // If depth of calling is getting too high, don't execute the function
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200799 if (depth >= p_mfd)
800 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100801 emsg(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200802 rettv->v_type = VAR_NUMBER;
803 rettv->vval.v_number = -1;
804 return;
805 }
806 ++depth;
807
Bram Moolenaare38eab22019-12-05 21:50:01 +0100808 line_breakcheck(); // check for CTRL-C hit
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200809
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200810 fc = ALLOC_CLEAR_ONE(funccall_T);
Bram Moolenaar4456ab52019-01-23 23:00:30 +0100811 if (fc == NULL)
812 return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200813 fc->caller = current_funccal;
814 current_funccal = fc;
815 fc->func = fp;
816 fc->rettv = rettv;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200817 fc->level = ex_nesting_level;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100818 // Check if this function has a breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200819 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
820 fc->dbg_tick = debug_tick;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100821 // Set up fields for closure.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200822 ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
Bram Moolenaar437bafe2016-08-01 15:40:54 +0200823 func_ptr_ref(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200824
825 if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
826 islambda = TRUE;
827
828 /*
829 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
830 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
831 * each argument variable and saves a lot of time.
832 */
833 /*
834 * Init l: variables.
835 */
836 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
837 if (selfdict != NULL)
838 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100839 // Set l:self to "selfdict". Use "name" to avoid a warning from
840 // some compiler that checks the destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200841 v = &fc->fixvar[fixvar_idx++].var;
842 name = v->di_key;
843 STRCPY(name, "self");
Bram Moolenaar31b81602019-02-10 22:14:27 +0100844 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200845 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
846 v->di_tv.v_type = VAR_DICT;
847 v->di_tv.v_lock = 0;
848 v->di_tv.vval.v_dict = selfdict;
849 ++selfdict->dv_refcount;
850 }
851
852 /*
853 * Init a: variables.
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200854 * Set a:0 to "argcount" less number of named arguments, if >= 0.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200855 * Set a:000 to a list with room for the "..." arguments.
856 */
857 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
858 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200859 (varnumber_T)(argcount >= fp->uf_args.ga_len
860 ? argcount - fp->uf_args.ga_len : 0));
Bram Moolenaar31b81602019-02-10 22:14:27 +0100861 fc->l_avars.dv_lock = VAR_FIXED;
Bram Moolenaare38eab22019-12-05 21:50:01 +0100862 // Use "name" to avoid a warning from some compiler that checks the
863 // destination size.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200864 v = &fc->fixvar[fixvar_idx++].var;
865 name = v->di_key;
866 STRCPY(name, "000");
867 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
868 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
869 v->di_tv.v_type = VAR_LIST;
870 v->di_tv.v_lock = VAR_FIXED;
871 v->di_tv.vval.v_list = &fc->l_varlist;
872 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
873 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
874 fc->l_varlist.lv_lock = VAR_FIXED;
875
876 /*
877 * Set a:firstline to "firstline" and a:lastline to "lastline".
878 * Set a:name to named arguments.
879 * Set a:N to the "..." arguments.
880 */
881 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
882 (varnumber_T)firstline);
883 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
884 (varnumber_T)lastline);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200885 for (i = 0; i < argcount || i < fp->uf_args.ga_len; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200886 {
887 int addlocal = FALSE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200888 typval_T def_rettv;
889 int isdefault = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200890
891 ai = i - fp->uf_args.ga_len;
892 if (ai < 0)
893 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100894 // named argument a:name
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200895 name = FUNCARG(fp, i);
896 if (islambda)
897 addlocal = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +0200898
899 // evaluate named argument default expression
900 isdefault = ai + fp->uf_def_args.ga_len >= 0
901 && (i >= argcount || (argvars[i].v_type == VAR_SPECIAL
902 && argvars[i].vval.v_number == VVAL_NONE));
903 if (isdefault)
904 {
905 char_u *default_expr = NULL;
906 def_rettv.v_type = VAR_NUMBER;
907 def_rettv.vval.v_number = -1;
908
909 default_expr = ((char_u **)(fp->uf_def_args.ga_data))
910 [ai + fp->uf_def_args.ga_len];
911 if (eval1(&default_expr, &def_rettv, TRUE) == FAIL)
912 {
913 default_arg_err = 1;
914 break;
915 }
916 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200917 }
918 else
919 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100920 // "..." argument a:1, a:2, etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200921 sprintf((char *)numbuf, "%d", ai + 1);
922 name = numbuf;
923 }
924 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
925 {
926 v = &fc->fixvar[fixvar_idx++].var;
927 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100928 STRCPY(v->di_key, name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200929 }
930 else
931 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100932 v = dictitem_alloc(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200933 if (v == NULL)
934 break;
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100935 v->di_flags |= DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200936 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200937
Bram Moolenaar6e5000d2019-06-17 21:18:41 +0200938 // Note: the values are copied directly to avoid alloc/free.
939 // "argvars" must have VAR_FIXED for v_lock.
940 v->di_tv = isdefault ? def_rettv : argvars[i];
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200941 v->di_tv.v_lock = VAR_FIXED;
942
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200943 if (addlocal)
944 {
Bram Moolenaare38eab22019-12-05 21:50:01 +0100945 // Named arguments should be accessed without the "a:" prefix in
946 // lambda expressions. Add to the l: dict.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200947 copy_tv(&v->di_tv, &v->di_tv);
948 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200949 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +0200950 else
951 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200952
953 if (ai >= 0 && ai < MAX_FUNC_ARGS)
954 {
Bram Moolenaar209b8e32019-03-14 13:43:24 +0100955 listitem_T *li = &fc->l_listitems[ai];
956
957 li->li_tv = argvars[i];
958 li->li_tv.v_lock = VAR_FIXED;
959 list_append(&fc->l_varlist, li);
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200960 }
961 }
962
Bram Moolenaare38eab22019-12-05 21:50:01 +0100963 // Don't redraw while executing the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200964 ++RedrawingDisabled;
Bram Moolenaar93343722018-07-10 19:39:18 +0200965
966 if (fp->uf_flags & FC_SANDBOX)
967 {
968 using_sandbox = TRUE;
969 ++sandbox;
970 }
971
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100972 estack_push_ufunc(ETYPE_UFUNC, fp, 1);
Bram Moolenaare31ee862020-01-07 20:59:34 +0100973 ESTACK_CHECK_SETUP
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100974 if (p_verbose >= 12)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200975 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100976 ++no_wait_return;
977 verbose_enter_scroll();
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200978
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100979 smsg(_("calling %s"), SOURCING_NAME);
980 if (p_verbose >= 14)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200981 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100982 char_u buf[MSG_BUF_LEN];
983 char_u numbuf2[NUMBUFLEN];
984 char_u *tofree;
985 char_u *s;
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200986
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100987 msg_puts("(");
988 for (i = 0; i < argcount; ++i)
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200989 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100990 if (i > 0)
991 msg_puts(", ");
992 if (argvars[i].v_type == VAR_NUMBER)
993 msg_outnum((long)argvars[i].vval.v_number);
994 else
Bram Moolenaara9b579f2016-07-17 18:29:19 +0200995 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +0100996 // Do not want errors such as E724 here.
997 ++emsg_off;
998 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
999 --emsg_off;
1000 if (s != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001001 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001002 if (vim_strsize(s) > MSG_BUF_CLEN)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001003 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001004 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1005 s = buf;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001006 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001007 msg_puts((char *)s);
1008 vim_free(tofree);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001009 }
1010 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001011 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001012 msg_puts(")");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001013 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001014 msg_puts("\n"); // don't overwrite this either
1015
1016 verbose_leave_scroll();
1017 --no_wait_return;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001018 }
1019#ifdef FEAT_PROFILE
1020 if (do_profiling == PROF_YES)
1021 {
1022 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
Bram Moolenaarad648092018-06-30 18:28:03 +02001023 {
1024 started_profiling = TRUE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001025 func_do_profile(fp);
Bram Moolenaarad648092018-06-30 18:28:03 +02001026 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001027 if (fp->uf_profiling
1028 || (fc->caller != NULL && fc->caller->func->uf_profiling))
1029 {
1030 ++fp->uf_tm_count;
1031 profile_start(&call_start);
1032 profile_zero(&fp->uf_tm_children);
1033 }
1034 script_prof_save(&wait_start);
1035 }
1036#endif
1037
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001038 save_current_sctx = current_sctx;
1039 current_sctx = fp->uf_script_ctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001040 save_did_emsg = did_emsg;
1041 did_emsg = FALSE;
1042
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001043 if (default_arg_err && (fp->uf_flags & FC_ABORT))
1044 did_emsg = TRUE;
1045 else
1046 // call do_cmdline() to execute the lines
1047 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001048 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
1049
1050 --RedrawingDisabled;
1051
Bram Moolenaare38eab22019-12-05 21:50:01 +01001052 // when the function was aborted because of an error, return -1
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001053 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
1054 {
1055 clear_tv(rettv);
1056 rettv->v_type = VAR_NUMBER;
1057 rettv->vval.v_number = -1;
1058 }
1059
1060#ifdef FEAT_PROFILE
1061 if (do_profiling == PROF_YES && (fp->uf_profiling
1062 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
1063 {
1064 profile_end(&call_start);
1065 profile_sub_wait(&wait_start, &call_start);
1066 profile_add(&fp->uf_tm_total, &call_start);
1067 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
1068 if (fc->caller != NULL && fc->caller->func->uf_profiling)
1069 {
1070 profile_add(&fc->caller->func->uf_tm_children, &call_start);
1071 profile_add(&fc->caller->func->uf_tml_children, &call_start);
1072 }
Bram Moolenaarad648092018-06-30 18:28:03 +02001073 if (started_profiling)
1074 // make a ":profdel func" stop profiling the function
1075 fp->uf_profiling = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001076 }
1077#endif
1078
Bram Moolenaare38eab22019-12-05 21:50:01 +01001079 // when being verbose, mention the return value
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001080 if (p_verbose >= 12)
1081 {
1082 ++no_wait_return;
1083 verbose_enter_scroll();
1084
1085 if (aborting())
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001086 smsg(_("%s aborted"), SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001087 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001088 smsg(_("%s returning #%ld"), SOURCING_NAME,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001089 (long)fc->rettv->vval.v_number);
1090 else
1091 {
1092 char_u buf[MSG_BUF_LEN];
1093 char_u numbuf2[NUMBUFLEN];
1094 char_u *tofree;
1095 char_u *s;
1096
Bram Moolenaare38eab22019-12-05 21:50:01 +01001097 // The value may be very long. Skip the middle part, so that we
1098 // have some idea how it starts and ends. smsg() would always
1099 // truncate it at the end. Don't want errors such as E724 here.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001100 ++emsg_off;
1101 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
1102 --emsg_off;
1103 if (s != NULL)
1104 {
1105 if (vim_strsize(s) > MSG_BUF_CLEN)
1106 {
1107 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
1108 s = buf;
1109 }
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001110 smsg(_("%s returning %s"), SOURCING_NAME, s);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001111 vim_free(tofree);
1112 }
1113 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001114 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001115
1116 verbose_leave_scroll();
1117 --no_wait_return;
1118 }
1119
Bram Moolenaare31ee862020-01-07 20:59:34 +01001120 ESTACK_CHECK_NOW
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001121 estack_pop();
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001122 current_sctx = save_current_sctx;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001123#ifdef FEAT_PROFILE
1124 if (do_profiling == PROF_YES)
1125 script_prof_restore(&wait_start);
1126#endif
Bram Moolenaar93343722018-07-10 19:39:18 +02001127 if (using_sandbox)
1128 --sandbox;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001129
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001130 if (p_verbose >= 12 && SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001131 {
1132 ++no_wait_return;
1133 verbose_enter_scroll();
1134
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001135 smsg(_("continuing in %s"), SOURCING_NAME);
Bram Moolenaare38eab22019-12-05 21:50:01 +01001136 msg_puts("\n"); // don't overwrite this either
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001137
1138 verbose_leave_scroll();
1139 --no_wait_return;
1140 }
1141
1142 did_emsg |= save_did_emsg;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001143 --depth;
1144
Bram Moolenaar6914c642017-04-01 21:21:30 +02001145 cleanup_function_call(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001146}
1147
1148/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001149 * Unreference "fc": decrement the reference count and free it when it
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001150 * becomes zero. "fp" is detached from "fc".
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001151 * When "force" is TRUE we are exiting.
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001152 */
1153 static void
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001154funccal_unref(funccall_T *fc, ufunc_T *fp, int force)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001155{
1156 funccall_T **pfc;
1157 int i;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001158
1159 if (fc == NULL)
1160 return;
1161
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001162 if (--fc->fc_refcount <= 0 && (force || (
1163 fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001164 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001165 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001166 for (pfc = &previous_funccal; *pfc != NULL; pfc = &(*pfc)->caller)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001167 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001168 if (fc == *pfc)
1169 {
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001170 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01001171 free_funccal_contents(fc);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001172 return;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001173 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001174 }
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001175 for (i = 0; i < fc->fc_funcs.ga_len; ++i)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001176 if (((ufunc_T **)(fc->fc_funcs.ga_data))[i] == fp)
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001177 ((ufunc_T **)(fc->fc_funcs.ga_data))[i] = NULL;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02001178}
1179
1180/*
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001181 * Remove the function from the function hashtable. If the function was
1182 * deleted while it still has references this was already done.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001183 * Return TRUE if the entry was deleted, FALSE if it wasn't found.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001184 */
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001185 static int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001186func_remove(ufunc_T *fp)
1187{
1188 hashitem_T *hi = hash_find(&func_hashtab, UF2HIKEY(fp));
1189
1190 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001191 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001192 hash_remove(&func_hashtab, hi);
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001193 return TRUE;
1194 }
1195 return FALSE;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001196}
1197
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001198 static void
1199func_clear_items(ufunc_T *fp)
1200{
1201 ga_clear_strings(&(fp->uf_args));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001202 ga_clear_strings(&(fp->uf_def_args));
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001203 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001204 VIM_CLEAR(fp->uf_name_exp);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001205#ifdef FEAT_PROFILE
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001206 VIM_CLEAR(fp->uf_tml_count);
1207 VIM_CLEAR(fp->uf_tml_total);
1208 VIM_CLEAR(fp->uf_tml_self);
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001209#endif
1210}
1211
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001212/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001213 * Free all things that a function contains. Does not free the function
1214 * itself, use func_free() for that.
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02001215 * When "force" is TRUE we are exiting.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001216 */
1217 static void
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001218func_clear(ufunc_T *fp, int force)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001219{
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001220 if (fp->uf_cleared)
1221 return;
1222 fp->uf_cleared = TRUE;
1223
Bram Moolenaare38eab22019-12-05 21:50:01 +01001224 // clear this function
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02001225 func_clear_items(fp);
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001226 funccal_unref(fp->uf_scoped, fp, force);
1227}
1228
1229/*
1230 * Free a function and remove it from the list of functions. Does not free
1231 * what a function contains, call func_clear() first.
1232 */
1233 static void
1234func_free(ufunc_T *fp)
1235{
Bram Moolenaare38eab22019-12-05 21:50:01 +01001236 // only remove it when not done already, otherwise we would remove a newer
1237 // version of the function
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02001238 if ((fp->uf_flags & (FC_DELETED | FC_REMOVED)) == 0)
1239 func_remove(fp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001240
1241 vim_free(fp);
1242}
1243
Bram Moolenaarc2574872016-08-11 22:51:05 +02001244/*
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001245 * Free all things that a function contains and free the function itself.
1246 * When "force" is TRUE we are exiting.
1247 */
1248 static void
1249func_clear_free(ufunc_T *fp, int force)
1250{
1251 func_clear(fp, force);
1252 func_free(fp);
1253}
1254
1255/*
Bram Moolenaarc2574872016-08-11 22:51:05 +02001256 * There are two kinds of function names:
1257 * 1. ordinary names, function defined with :function
1258 * 2. numbered functions and lambdas
1259 * For the first we only count the name stored in func_hashtab as a reference,
1260 * using function() does not count as a reference, because the function is
1261 * looked up by name.
1262 */
1263 static int
1264func_name_refcount(char_u *name)
1265{
1266 return isdigit(*name) || *name == '<';
1267}
1268
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001269static funccal_entry_T *funccal_stack = NULL;
1270
1271/*
1272 * Save the current function call pointer, and set it to NULL.
1273 * Used when executing autocommands and for ":source".
1274 */
1275 void
1276save_funccal(funccal_entry_T *entry)
1277{
1278 entry->top_funccal = current_funccal;
1279 entry->next = funccal_stack;
1280 funccal_stack = entry;
1281 current_funccal = NULL;
1282}
1283
1284 void
1285restore_funccal(void)
1286{
1287 if (funccal_stack == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001288 iemsg("INTERNAL: restore_funccal()");
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001289 else
1290 {
1291 current_funccal = funccal_stack->top_funccal;
1292 funccal_stack = funccal_stack->next;
1293 }
1294}
1295
Bram Moolenaarfa55cfc2019-07-13 22:59:32 +02001296 funccall_T *
1297get_current_funccal(void)
1298{
1299 return current_funccal;
1300}
1301
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001302#if defined(EXITFREE) || defined(PROTO)
1303 void
1304free_all_functions(void)
1305{
1306 hashitem_T *hi;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001307 ufunc_T *fp;
1308 long_u skipped = 0;
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001309 long_u todo = 1;
1310 long_u used;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001311
Bram Moolenaare38eab22019-12-05 21:50:01 +01001312 // Clean up the current_funccal chain and the funccal stack.
Bram Moolenaar6914c642017-04-01 21:21:30 +02001313 while (current_funccal != NULL)
1314 {
1315 clear_tv(current_funccal->rettv);
1316 cleanup_function_call(current_funccal);
Bram Moolenaar27e80c82018-10-14 21:41:01 +02001317 if (current_funccal == NULL && funccal_stack != NULL)
1318 restore_funccal();
Bram Moolenaar6914c642017-04-01 21:21:30 +02001319 }
1320
Bram Moolenaare38eab22019-12-05 21:50:01 +01001321 // First clear what the functions contain. Since this may lower the
1322 // reference count of a function, it may also free a function and change
1323 // the hash table. Restart if that happens.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001324 while (todo > 0)
1325 {
1326 todo = func_hashtab.ht_used;
1327 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
1328 if (!HASHITEM_EMPTY(hi))
1329 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001330 // Only free functions that are not refcounted, those are
1331 // supposed to be freed when no longer referenced.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001332 fp = HI2UF(hi);
1333 if (func_name_refcount(fp->uf_name))
1334 ++skipped;
1335 else
1336 {
1337 used = func_hashtab.ht_used;
1338 func_clear(fp, TRUE);
1339 if (used != func_hashtab.ht_used)
1340 {
1341 skipped = 0;
1342 break;
1343 }
1344 }
1345 --todo;
1346 }
1347 }
1348
Bram Moolenaare38eab22019-12-05 21:50:01 +01001349 // Now actually free the functions. Need to start all over every time,
1350 // because func_free() may change the hash table.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001351 skipped = 0;
Bram Moolenaarc2574872016-08-11 22:51:05 +02001352 while (func_hashtab.ht_used > skipped)
1353 {
1354 todo = func_hashtab.ht_used;
1355 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001356 if (!HASHITEM_EMPTY(hi))
1357 {
Bram Moolenaarc2574872016-08-11 22:51:05 +02001358 --todo;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001359 // Only free functions that are not refcounted, those are
1360 // supposed to be freed when no longer referenced.
Bram Moolenaarc2574872016-08-11 22:51:05 +02001361 fp = HI2UF(hi);
1362 if (func_name_refcount(fp->uf_name))
1363 ++skipped;
1364 else
1365 {
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001366 func_free(fp);
Bram Moolenaarc2574872016-08-11 22:51:05 +02001367 skipped = 0;
1368 break;
1369 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001370 }
Bram Moolenaarc2574872016-08-11 22:51:05 +02001371 }
1372 if (skipped == 0)
1373 hash_clear(&func_hashtab);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001374}
1375#endif
1376
1377/*
1378 * Return TRUE if "name" looks like a builtin function name: starts with a
1379 * lower case letter and doesn't contain AUTOLOAD_CHAR.
1380 * "len" is the length of "name", or -1 for NUL terminated.
1381 */
1382 static int
1383builtin_function(char_u *name, int len)
1384{
1385 char_u *p;
1386
1387 if (!ASCII_ISLOWER(name[0]))
1388 return FALSE;
1389 p = vim_strchr(name, AUTOLOAD_CHAR);
1390 return p == NULL || (len > 0 && p > name + len);
1391}
1392
1393 int
1394func_call(
1395 char_u *name,
1396 typval_T *args,
1397 partial_T *partial,
1398 dict_T *selfdict,
1399 typval_T *rettv)
1400{
1401 listitem_T *item;
1402 typval_T argv[MAX_FUNC_ARGS + 1];
1403 int argc = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001404 int r = 0;
1405
1406 for (item = args->vval.v_list->lv_first; item != NULL;
1407 item = item->li_next)
1408 {
1409 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
1410 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001411 emsg(_("E699: Too many arguments"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001412 break;
1413 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001414 // Make a copy of each argument. This is needed to be able to set
1415 // v_lock to VAR_FIXED in the copy without changing the original list.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001416 copy_tv(&item->li_tv, &argv[argc++]);
1417 }
1418
1419 if (item == NULL)
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001420 {
1421 funcexe_T funcexe;
1422
Bram Moolenaarac92e252019-08-03 21:58:38 +02001423 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001424 funcexe.firstline = curwin->w_cursor.lnum;
1425 funcexe.lastline = curwin->w_cursor.lnum;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001426 funcexe.evaluate = TRUE;
1427 funcexe.partial = partial;
1428 funcexe.selfdict = selfdict;
1429 r = call_func(name, -1, rettv, argc, argv, &funcexe);
1430 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001431
Bram Moolenaare38eab22019-12-05 21:50:01 +01001432 // Free the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001433 while (argc > 0)
1434 clear_tv(&argv[--argc]);
1435
1436 return r;
1437}
1438
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001439static int callback_depth = 0;
1440
1441 int
1442get_callback_depth(void)
1443{
1444 return callback_depth;
1445}
1446
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001447/*
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001448 * Invoke call_func() with a callback.
1449 */
1450 int
1451call_callback(
1452 callback_T *callback,
1453 int len, // length of "name" or -1 to use strlen()
1454 typval_T *rettv, // return value goes here
1455 int argcount, // number of "argvars"
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001456 typval_T *argvars) // vars for arguments, must have "argcount"
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001457 // PLUS ONE elements!
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001458{
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001459 funcexe_T funcexe;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001460 int ret;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001461
1462 vim_memset(&funcexe, 0, sizeof(funcexe));
1463 funcexe.evaluate = TRUE;
1464 funcexe.partial = callback->cb_partial;
Bram Moolenaar0e57dd82019-09-16 22:56:03 +02001465 ++callback_depth;
1466 ret = call_func(callback->cb_name, len, rettv, argcount, argvars, &funcexe);
1467 --callback_depth;
1468 return ret;
Bram Moolenaar3a97bb32019-06-01 13:28:35 +02001469}
1470
1471/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001472 * Call a function with its resolved parameters
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001473 *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001474 * Return FAIL when the function can't be called, OK otherwise.
1475 * Also returns OK when an error was encountered while executing the function.
1476 */
1477 int
1478call_func(
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001479 char_u *funcname, // name of the function
1480 int len, // length of "name" or -1 to use strlen()
1481 typval_T *rettv, // return value goes here
1482 int argcount_in, // number of "argvars"
1483 typval_T *argvars_in, // vars for arguments, must have "argcount"
1484 // PLUS ONE elements!
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001485 funcexe_T *funcexe) // more arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001486{
1487 int ret = FAIL;
Bram Moolenaaref140542019-12-31 21:27:13 +01001488 int error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001489 int i;
1490 ufunc_T *fp;
1491 char_u fname_buf[FLEN_FIXED + 1];
1492 char_u *tofree = NULL;
1493 char_u *fname;
1494 char_u *name;
1495 int argcount = argcount_in;
1496 typval_T *argvars = argvars_in;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001497 dict_T *selfdict = funcexe->selfdict;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001498 typval_T argv[MAX_FUNC_ARGS + 1]; // used when "partial" or
1499 // "funcexe->basetv" is not NULL
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001500 int argv_clear = 0;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001501 int argv_base = 0;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001502 partial_T *partial = funcexe->partial;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001503
Bram Moolenaarc507a2d2019-08-29 21:32:55 +02001504 // Initialize rettv so that it is safe for caller to invoke clear_tv(rettv)
1505 // even when call_func() returns FAIL.
1506 rettv->v_type = VAR_UNKNOWN;
1507
Bram Moolenaar6ed88192019-05-11 18:37:44 +02001508 // Make a copy of the name, if it comes from a funcref variable it could
1509 // be changed or deleted in the called function.
1510 name = len > 0 ? vim_strnsave(funcname, len) : vim_strsave(funcname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001511 if (name == NULL)
1512 return ret;
1513
1514 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
1515
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001516 if (funcexe->doesrange != NULL)
1517 *funcexe->doesrange = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001518
1519 if (partial != NULL)
1520 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001521 // When the function has a partial with a dict and there is a dict
1522 // argument, use the dict argument. That is backwards compatible.
1523 // When the dict was bound explicitly use the one from the partial.
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001524 if (partial->pt_dict != NULL && (selfdict == NULL || !partial->pt_auto))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001525 selfdict = partial->pt_dict;
Bram Moolenaaref140542019-12-31 21:27:13 +01001526 if (error == FCERR_NONE && partial->pt_argc > 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001527 {
1528 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001529 {
1530 if (argv_clear + argcount_in >= MAX_FUNC_ARGS)
1531 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001532 error = FCERR_TOOMANY;
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001533 goto theend;
1534 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001535 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001536 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001537 for (i = 0; i < argcount_in; ++i)
1538 argv[i + argv_clear] = argvars_in[i];
1539 argvars = argv;
1540 argcount = partial->pt_argc + argcount_in;
1541 }
1542 }
1543
Bram Moolenaaref140542019-12-31 21:27:13 +01001544 if (error == FCERR_NONE && funcexe->evaluate)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001545 {
1546 char_u *rfname = fname;
1547
Bram Moolenaare38eab22019-12-05 21:50:01 +01001548 // Ignore "g:" before a function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001549 if (fname[0] == 'g' && fname[1] == ':')
1550 rfname = fname + 2;
1551
Bram Moolenaare38eab22019-12-05 21:50:01 +01001552 rettv->v_type = VAR_NUMBER; // default rettv is number zero
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001553 rettv->vval.v_number = 0;
Bram Moolenaaref140542019-12-31 21:27:13 +01001554 error = FCERR_UNKNOWN;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001555
1556 if (!builtin_function(rfname, -1))
1557 {
1558 /*
1559 * User defined function.
1560 */
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001561 if (partial != NULL && partial->pt_func != NULL)
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001562 fp = partial->pt_func;
1563 else
1564 fp = find_func(rfname);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001565
Bram Moolenaare38eab22019-12-05 21:50:01 +01001566 // Trigger FuncUndefined event, may load the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001567 if (fp == NULL
1568 && apply_autocmds(EVENT_FUNCUNDEFINED,
1569 rfname, rfname, TRUE, NULL)
1570 && !aborting())
1571 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001572 // executed an autocommand, search for the function again
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001573 fp = find_func(rfname);
1574 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01001575 // Try loading a package.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001576 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
1577 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001578 // loaded a package, search for the function again
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001579 fp = find_func(rfname);
1580 }
1581
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001582 if (fp != NULL && (fp->uf_flags & FC_DELETED))
Bram Moolenaaref140542019-12-31 21:27:13 +01001583 error = FCERR_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001584 else if (fp != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001585 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001586 if (funcexe->argv_func != NULL)
Bram Moolenaarb0745b22019-11-09 22:28:11 +01001587 // postponed filling in the arguments, do it now
1588 argcount = funcexe->argv_func(argcount, argvars, argv_clear,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001589 fp->uf_args.ga_len);
Bram Moolenaardf48fb42016-07-22 21:50:18 +02001590
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001591 if (funcexe->basetv != NULL)
1592 {
1593 // Method call: base->Method()
1594 mch_memmove(&argv[1], argvars, sizeof(typval_T) * argcount);
1595 argv[0] = *funcexe->basetv;
1596 argcount++;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001597 argvars = argv;
1598 argv_base = 1;
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001599 }
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001600
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001601 if (fp->uf_flags & FC_RANGE && funcexe->doesrange != NULL)
1602 *funcexe->doesrange = TRUE;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001603 if (argcount < fp->uf_args.ga_len - fp->uf_def_args.ga_len)
Bram Moolenaaref140542019-12-31 21:27:13 +01001604 error = FCERR_TOOFEW;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001605 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaaref140542019-12-31 21:27:13 +01001606 error = FCERR_TOOMANY;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001607 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaaref140542019-12-31 21:27:13 +01001608 error = FCERR_DICT;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001609 else
1610 {
1611 int did_save_redo = FALSE;
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001612 save_redo_T save_redo;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001613
1614 /*
1615 * Call the user function.
1616 * Save and restore search patterns, script variables and
1617 * redo buffer.
1618 */
1619 save_search_patterns();
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001620 if (!ins_compl_active())
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001621 {
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001622 saveRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001623 did_save_redo = TRUE;
1624 }
1625 ++fp->uf_calls;
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001626 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02001627 funcexe->firstline, funcexe->lastline,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001628 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001629 if (--fp->uf_calls <= 0 && fp->uf_refcount <= 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001630 // Function was unreferenced while being used, free it
1631 // now.
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01001632 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001633 if (did_save_redo)
Bram Moolenaard4863aa2017-04-07 19:50:12 +02001634 restoreRedobuff(&save_redo);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001635 restore_search_patterns();
Bram Moolenaaref140542019-12-31 21:27:13 +01001636 error = FCERR_NONE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001637 }
1638 }
1639 }
Bram Moolenaarac92e252019-08-03 21:58:38 +02001640 else if (funcexe->basetv != NULL)
1641 {
1642 /*
Bram Moolenaarfcfe1a92019-08-04 23:04:39 +02001643 * expr->method(): Find the method name in the table, call its
1644 * implementation with the base as one of the arguments.
Bram Moolenaarac92e252019-08-03 21:58:38 +02001645 */
1646 error = call_internal_method(fname, argcount, argvars, rettv,
1647 funcexe->basetv);
1648 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001649 else
1650 {
1651 /*
1652 * Find the function name in the table, call its implementation.
1653 */
1654 error = call_internal_func(fname, argcount, argvars, rettv);
1655 }
1656 /*
1657 * The function call (or "FuncUndefined" autocommand sequence) might
1658 * have been aborted by an error, an interrupt, or an explicitly thrown
1659 * exception that has not been caught so far. This situation can be
1660 * tested for by calling aborting(). For an error in an internal
1661 * function or for the "E132" error in call_user_func(), however, the
1662 * throw point at which the "force_abort" flag (temporarily reset by
1663 * emsg()) is normally updated has not been reached yet. We need to
1664 * update that flag first to make aborting() reliable.
1665 */
1666 update_force_abort();
1667 }
Bram Moolenaaref140542019-12-31 21:27:13 +01001668 if (error == FCERR_NONE)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001669 ret = OK;
1670
Bram Moolenaar4c054e92019-11-10 00:13:50 +01001671theend:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001672 /*
1673 * Report an error unless the argument evaluation or function call has been
1674 * cancelled due to an aborting error, an interrupt, or an exception.
1675 */
1676 if (!aborting())
1677 {
1678 switch (error)
1679 {
Bram Moolenaaref140542019-12-31 21:27:13 +01001680 case FCERR_UNKNOWN:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001681 emsg_funcname(N_("E117: Unknown function: %s"), name);
1682 break;
Bram Moolenaaref140542019-12-31 21:27:13 +01001683 case FCERR_NOTMETHOD:
Bram Moolenaar91746392019-08-16 22:22:31 +02001684 emsg_funcname(
1685 N_("E276: Cannot use function as a method: %s"),
1686 name);
1687 break;
Bram Moolenaaref140542019-12-31 21:27:13 +01001688 case FCERR_DELETED:
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001689 emsg_funcname(N_("E933: Function was deleted: %s"), name);
1690 break;
Bram Moolenaaref140542019-12-31 21:27:13 +01001691 case FCERR_TOOMANY:
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001692 emsg_funcname((char *)e_toomanyarg, name);
1693 break;
Bram Moolenaaref140542019-12-31 21:27:13 +01001694 case FCERR_TOOFEW:
Bram Moolenaar91746392019-08-16 22:22:31 +02001695 emsg_funcname(
1696 N_("E119: Not enough arguments for function: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001697 name);
1698 break;
Bram Moolenaaref140542019-12-31 21:27:13 +01001699 case FCERR_SCRIPT:
Bram Moolenaar91746392019-08-16 22:22:31 +02001700 emsg_funcname(
1701 N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001702 name);
1703 break;
Bram Moolenaaref140542019-12-31 21:27:13 +01001704 case FCERR_DICT:
Bram Moolenaar91746392019-08-16 22:22:31 +02001705 emsg_funcname(
1706 N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001707 name);
1708 break;
1709 }
1710 }
1711
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001712 // clear the copies made from the partial
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001713 while (argv_clear > 0)
Bram Moolenaar761fdf02019-08-05 23:10:16 +02001714 clear_tv(&argv[--argv_clear + argv_base]);
1715
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001716 vim_free(tofree);
1717 vim_free(name);
1718
1719 return ret;
1720}
1721
1722/*
1723 * List the head of the function: "name(arg1, arg2)".
1724 */
1725 static void
1726list_func_head(ufunc_T *fp, int indent)
1727{
1728 int j;
1729
1730 msg_start();
1731 if (indent)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001732 msg_puts(" ");
1733 msg_puts("function ");
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01001734 if (fp->uf_name_exp != NULL)
1735 msg_puts((char *)fp->uf_name_exp);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001736 else
Bram Moolenaar32526b32019-01-19 17:43:09 +01001737 msg_puts((char *)fp->uf_name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001738 msg_putchar('(');
1739 for (j = 0; j < fp->uf_args.ga_len; ++j)
1740 {
1741 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001742 msg_puts(", ");
1743 msg_puts((char *)FUNCARG(fp, j));
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02001744 if (j >= fp->uf_args.ga_len - fp->uf_def_args.ga_len)
1745 {
1746 msg_puts(" = ");
1747 msg_puts(((char **)(fp->uf_def_args.ga_data))
1748 [j - fp->uf_args.ga_len + fp->uf_def_args.ga_len]);
1749 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001750 }
1751 if (fp->uf_varargs)
1752 {
1753 if (j)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001754 msg_puts(", ");
1755 msg_puts("...");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001756 }
1757 msg_putchar(')');
1758 if (fp->uf_flags & FC_ABORT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001759 msg_puts(" abort");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001760 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001761 msg_puts(" range");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001762 if (fp->uf_flags & FC_DICT)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001763 msg_puts(" dict");
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02001764 if (fp->uf_flags & FC_CLOSURE)
Bram Moolenaar32526b32019-01-19 17:43:09 +01001765 msg_puts(" closure");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001766 msg_clr_eos();
1767 if (p_verbose > 0)
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001768 last_set_msg(fp->uf_script_ctx);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001769}
1770
1771/*
1772 * Get a function name, translating "<SID>" and "<SNR>".
1773 * Also handles a Funcref in a List or Dictionary.
1774 * Returns the function name in allocated memory, or NULL for failure.
1775 * flags:
1776 * TFN_INT: internal function name OK
1777 * TFN_QUIET: be quiet
1778 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001779 * TFN_NO_DEREF: do not dereference a Funcref
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001780 * Advances "pp" to just after the function name (if no error).
1781 */
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001782 char_u *
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001783trans_function_name(
1784 char_u **pp,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001785 int skip, // only find the end, don't evaluate
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001786 int flags,
Bram Moolenaare38eab22019-12-05 21:50:01 +01001787 funcdict_T *fdp, // return: info about dictionary used
1788 partial_T **partial) // return: partial of a FuncRef
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001789{
1790 char_u *name = NULL;
1791 char_u *start;
1792 char_u *end;
1793 int lead;
1794 char_u sid_buf[20];
1795 int len;
1796 lval_T lv;
1797
1798 if (fdp != NULL)
1799 vim_memset(fdp, 0, sizeof(funcdict_T));
1800 start = *pp;
1801
Bram Moolenaare38eab22019-12-05 21:50:01 +01001802 // Check for hard coded <SNR>: already translated function ID (from a user
1803 // command).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001804 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
1805 && (*pp)[2] == (int)KE_SNR)
1806 {
1807 *pp += 3;
1808 len = get_id_len(pp) + 3;
1809 return vim_strnsave(start, len);
1810 }
1811
Bram Moolenaare38eab22019-12-05 21:50:01 +01001812 // A name starting with "<SID>" or "<SNR>" is local to a script. But
1813 // don't skip over "s:", get_lval() needs it for "s:dict.func".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001814 lead = eval_fname_script(start);
1815 if (lead > 2)
1816 start += lead;
1817
Bram Moolenaare38eab22019-12-05 21:50:01 +01001818 // Note that TFN_ flags use the same values as GLV_ flags.
Bram Moolenaar6e65d592017-12-07 22:11:27 +01001819 end = get_lval(start, NULL, &lv, FALSE, skip, flags | GLV_READ_ONLY,
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001820 lead > 2 ? 0 : FNE_CHECK_START);
1821 if (end == start)
1822 {
1823 if (!skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001824 emsg(_("E129: Function name required"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001825 goto theend;
1826 }
1827 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
1828 {
1829 /*
1830 * Report an invalid expression in braces, unless the expression
1831 * evaluation has been cancelled due to an aborting error, an
1832 * interrupt, or an exception.
1833 */
1834 if (!aborting())
1835 {
1836 if (end != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001837 semsg(_(e_invarg2), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001838 }
1839 else
1840 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
1841 goto theend;
1842 }
1843
1844 if (lv.ll_tv != NULL)
1845 {
1846 if (fdp != NULL)
1847 {
1848 fdp->fd_dict = lv.ll_dict;
1849 fdp->fd_newkey = lv.ll_newkey;
1850 lv.ll_newkey = NULL;
1851 fdp->fd_di = lv.ll_di;
1852 }
1853 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
1854 {
1855 name = vim_strsave(lv.ll_tv->vval.v_string);
1856 *pp = end;
1857 }
1858 else if (lv.ll_tv->v_type == VAR_PARTIAL
1859 && lv.ll_tv->vval.v_partial != NULL)
1860 {
Bram Moolenaar437bafe2016-08-01 15:40:54 +02001861 name = vim_strsave(partial_name(lv.ll_tv->vval.v_partial));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001862 *pp = end;
1863 if (partial != NULL)
1864 *partial = lv.ll_tv->vval.v_partial;
1865 }
1866 else
1867 {
1868 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
1869 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001870 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001871 else
1872 *pp = end;
1873 name = NULL;
1874 }
1875 goto theend;
1876 }
1877
1878 if (lv.ll_name == NULL)
1879 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001880 // Error found, but continue after the function name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001881 *pp = end;
1882 goto theend;
1883 }
1884
Bram Moolenaare38eab22019-12-05 21:50:01 +01001885 // Check if the name is a Funcref. If so, use the value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001886 if (lv.ll_exp_name != NULL)
1887 {
1888 len = (int)STRLEN(lv.ll_exp_name);
1889 name = deref_func_name(lv.ll_exp_name, &len, partial,
1890 flags & TFN_NO_AUTOLOAD);
1891 if (name == lv.ll_exp_name)
1892 name = NULL;
1893 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001894 else if (!(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001895 {
1896 len = (int)(end - *pp);
1897 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
1898 if (name == *pp)
1899 name = NULL;
1900 }
1901 if (name != NULL)
1902 {
1903 name = vim_strsave(name);
1904 *pp = end;
1905 if (STRNCMP(name, "<SNR>", 5) == 0)
1906 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001907 // Change "<SNR>" to the byte sequence.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001908 name[0] = K_SPECIAL;
1909 name[1] = KS_EXTRA;
1910 name[2] = (int)KE_SNR;
1911 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
1912 }
1913 goto theend;
1914 }
1915
1916 if (lv.ll_exp_name != NULL)
1917 {
1918 len = (int)STRLEN(lv.ll_exp_name);
1919 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
1920 && STRNCMP(lv.ll_name, "s:", 2) == 0)
1921 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001922 // When there was "s:" already or the name expanded to get a
1923 // leading "s:" then remove it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001924 lv.ll_name += 2;
1925 len -= 2;
1926 lead = 2;
1927 }
1928 }
1929 else
1930 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001931 // skip over "s:" and "g:"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001932 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
1933 lv.ll_name += 2;
1934 len = (int)(end - lv.ll_name);
1935 }
1936
1937 /*
1938 * Copy the function name to allocated memory.
1939 * Accept <SID>name() inside a script, translate into <SNR>123_name().
1940 * Accept <SNR>123_name() outside a script.
1941 */
1942 if (skip)
Bram Moolenaare38eab22019-12-05 21:50:01 +01001943 lead = 0; // do nothing
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001944 else if (lead > 0)
1945 {
1946 lead = 3;
1947 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
1948 || eval_fname_sid(*pp))
1949 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01001950 // It's "s:" or "<SID>"
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001951 if (current_sctx.sc_sid <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001952 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001953 emsg(_(e_usingsid));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001954 goto theend;
1955 }
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02001956 sprintf((char *)sid_buf, "%ld_", (long)current_sctx.sc_sid);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001957 lead += (int)STRLEN(sid_buf);
1958 }
1959 }
1960 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
1961 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001962 semsg(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001963 start);
1964 goto theend;
1965 }
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02001966 if (!skip && !(flags & TFN_QUIET) && !(flags & TFN_NO_DEREF))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001967 {
1968 char_u *cp = vim_strchr(lv.ll_name, ':');
1969
1970 if (cp != NULL && cp < end)
1971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001972 semsg(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001973 goto theend;
1974 }
1975 }
1976
Bram Moolenaar964b3742019-05-24 18:54:09 +02001977 name = alloc(len + lead + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001978 if (name != NULL)
1979 {
1980 if (lead > 0)
1981 {
1982 name[0] = K_SPECIAL;
1983 name[1] = KS_EXTRA;
1984 name[2] = (int)KE_SNR;
Bram Moolenaare38eab22019-12-05 21:50:01 +01001985 if (lead > 3) // If it's "<SID>"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02001986 STRCPY(name + 3, sid_buf);
1987 }
1988 mch_memmove(name + lead, lv.ll_name, (size_t)len);
1989 name[lead + len] = NUL;
1990 }
1991 *pp = end;
1992
1993theend:
1994 clear_lval(&lv);
1995 return name;
1996}
1997
1998/*
1999 * ":function"
2000 */
2001 void
2002ex_function(exarg_T *eap)
2003{
2004 char_u *theline;
Bram Moolenaar53564f72017-06-24 14:48:11 +02002005 char_u *line_to_free = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002006 int j;
2007 int c;
2008 int saved_did_emsg;
2009 int saved_wait_return = need_wait_return;
2010 char_u *name = NULL;
2011 char_u *p;
2012 char_u *arg;
2013 char_u *line_arg = NULL;
2014 garray_T newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002015 garray_T default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002016 garray_T newlines;
2017 int varargs = FALSE;
2018 int flags = 0;
2019 ufunc_T *fp;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002020 int overwrite = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002021 int indent;
2022 int nesting;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002023 dictitem_T *v;
2024 funcdict_T fudi;
Bram Moolenaare38eab22019-12-05 21:50:01 +01002025 static int func_nr = 0; // number for nameless function
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002026 int paren;
2027 hashtab_T *ht;
2028 int todo;
2029 hashitem_T *hi;
Bram Moolenaare96a2492019-06-25 04:12:16 +02002030 int do_concat = TRUE;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002031 linenr_T sourcing_lnum_off;
2032 linenr_T sourcing_lnum_top;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002033 int is_heredoc = FALSE;
2034 char_u *skip_until = NULL;
2035 char_u *heredoc_trimmed = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002036
2037 /*
2038 * ":function" without argument: list functions.
2039 */
2040 if (ends_excmd(*eap->arg))
2041 {
2042 if (!eap->skip)
2043 {
2044 todo = (int)func_hashtab.ht_used;
2045 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2046 {
2047 if (!HASHITEM_EMPTY(hi))
2048 {
2049 --todo;
2050 fp = HI2UF(hi);
Bram Moolenaarf86db782018-10-25 13:31:37 +02002051 if (message_filtered(fp->uf_name))
2052 continue;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02002053 if (!func_name_refcount(fp->uf_name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002054 list_func_head(fp, FALSE);
2055 }
2056 }
2057 }
2058 eap->nextcmd = check_nextcmd(eap->arg);
2059 return;
2060 }
2061
2062 /*
2063 * ":function /pat": list functions matching pattern.
2064 */
2065 if (*eap->arg == '/')
2066 {
2067 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
2068 if (!eap->skip)
2069 {
2070 regmatch_T regmatch;
2071
2072 c = *p;
2073 *p = NUL;
2074 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
2075 *p = c;
2076 if (regmatch.regprog != NULL)
2077 {
2078 regmatch.rm_ic = p_ic;
2079
2080 todo = (int)func_hashtab.ht_used;
2081 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
2082 {
2083 if (!HASHITEM_EMPTY(hi))
2084 {
2085 --todo;
2086 fp = HI2UF(hi);
2087 if (!isdigit(*fp->uf_name)
2088 && vim_regexec(&regmatch, fp->uf_name, 0))
2089 list_func_head(fp, FALSE);
2090 }
2091 }
2092 vim_regfree(regmatch.regprog);
2093 }
2094 }
2095 if (*p == '/')
2096 ++p;
2097 eap->nextcmd = check_nextcmd(p);
2098 return;
2099 }
2100
2101 /*
2102 * Get the function name. There are these situations:
2103 * func normal function name
2104 * "name" == func, "fudi.fd_dict" == NULL
2105 * dict.func new dictionary entry
2106 * "name" == NULL, "fudi.fd_dict" set,
2107 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
2108 * dict.func existing dict entry with a Funcref
2109 * "name" == func, "fudi.fd_dict" set,
2110 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2111 * dict.func existing dict entry that's not a Funcref
2112 * "name" == NULL, "fudi.fd_dict" set,
2113 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
2114 * s:func script-local function name
2115 * g:func global function name, same as "func"
2116 */
2117 p = eap->arg;
Bram Moolenaar3388d332017-12-07 22:23:04 +01002118 name = trans_function_name(&p, eap->skip, TFN_NO_AUTOLOAD, &fudi, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002119 paren = (vim_strchr(p, '(') != NULL);
2120 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
2121 {
2122 /*
2123 * Return on an invalid expression in braces, unless the expression
2124 * evaluation has been cancelled due to an aborting error, an
2125 * interrupt, or an exception.
2126 */
2127 if (!aborting())
2128 {
2129 if (!eap->skip && fudi.fd_newkey != NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002130 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002131 vim_free(fudi.fd_newkey);
2132 return;
2133 }
2134 else
2135 eap->skip = TRUE;
2136 }
2137
Bram Moolenaare38eab22019-12-05 21:50:01 +01002138 // An error in a function call during evaluation of an expression in magic
2139 // braces should not cause the function not to be defined.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002140 saved_did_emsg = did_emsg;
2141 did_emsg = FALSE;
2142
2143 /*
2144 * ":function func" with only function name: list function.
2145 */
2146 if (!paren)
2147 {
2148 if (!ends_excmd(*skipwhite(p)))
2149 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002150 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002151 goto ret_free;
2152 }
2153 eap->nextcmd = check_nextcmd(p);
2154 if (eap->nextcmd != NULL)
2155 *p = NUL;
2156 if (!eap->skip && !got_int)
2157 {
2158 fp = find_func(name);
2159 if (fp != NULL)
2160 {
2161 list_func_head(fp, TRUE);
2162 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
2163 {
2164 if (FUNCLINE(fp, j) == NULL)
2165 continue;
2166 msg_putchar('\n');
2167 msg_outnum((long)(j + 1));
2168 if (j < 9)
2169 msg_putchar(' ');
2170 if (j < 99)
2171 msg_putchar(' ');
2172 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaare38eab22019-12-05 21:50:01 +01002173 out_flush(); // show a line at a time
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002174 ui_breakcheck();
2175 }
2176 if (!got_int)
2177 {
2178 msg_putchar('\n');
Bram Moolenaar32526b32019-01-19 17:43:09 +01002179 msg_puts(" endfunction");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002180 }
2181 }
2182 else
2183 emsg_funcname(N_("E123: Undefined function: %s"), name);
2184 }
2185 goto ret_free;
2186 }
2187
2188 /*
2189 * ":function name(arg1, arg2)" Define function.
2190 */
2191 p = skipwhite(p);
2192 if (*p != '(')
2193 {
2194 if (!eap->skip)
2195 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002196 semsg(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002197 goto ret_free;
2198 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002199 // attempt to continue by skipping some text
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002200 if (vim_strchr(p, '(') != NULL)
2201 p = vim_strchr(p, '(');
2202 }
2203 p = skipwhite(p + 1);
2204
2205 ga_init2(&newlines, (int)sizeof(char_u *), 3);
2206
2207 if (!eap->skip)
2208 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002209 // Check the name of the function. Unless it's a dictionary function
2210 // (that we are overwriting).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002211 if (name != NULL)
2212 arg = name;
2213 else
2214 arg = fudi.fd_newkey;
2215 if (arg != NULL && (fudi.fd_di == NULL
2216 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
2217 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
2218 {
2219 if (*arg == K_SPECIAL)
2220 j = 3;
2221 else
2222 j = 0;
2223 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
2224 : eval_isnamec(arg[j])))
2225 ++j;
2226 if (arg[j] != NUL)
2227 emsg_funcname((char *)e_invarg2, arg);
2228 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002229 // Disallow using the g: dict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002230 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002231 emsg(_("E862: Cannot use g: here"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002232 }
2233
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002234 if (get_function_args(&p, ')', &newargs, &varargs,
2235 &default_args, eap->skip) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002236 goto errret_2;
2237
Bram Moolenaare38eab22019-12-05 21:50:01 +01002238 // find extra arguments "range", "dict", "abort" and "closure"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002239 for (;;)
2240 {
2241 p = skipwhite(p);
2242 if (STRNCMP(p, "range", 5) == 0)
2243 {
2244 flags |= FC_RANGE;
2245 p += 5;
2246 }
2247 else if (STRNCMP(p, "dict", 4) == 0)
2248 {
2249 flags |= FC_DICT;
2250 p += 4;
2251 }
2252 else if (STRNCMP(p, "abort", 5) == 0)
2253 {
2254 flags |= FC_ABORT;
2255 p += 5;
2256 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002257 else if (STRNCMP(p, "closure", 7) == 0)
2258 {
2259 flags |= FC_CLOSURE;
2260 p += 7;
Bram Moolenaar58016442016-07-31 18:30:22 +02002261 if (current_funccal == NULL)
2262 {
Bram Moolenaarba209902016-08-24 22:06:38 +02002263 emsg_funcname(N_("E932: Closure function should not be at top level: %s"),
Bram Moolenaar58016442016-07-31 18:30:22 +02002264 name == NULL ? (char_u *)"" : name);
2265 goto erret;
2266 }
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002267 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002268 else
2269 break;
2270 }
2271
Bram Moolenaare38eab22019-12-05 21:50:01 +01002272 // When there is a line break use what follows for the function body.
2273 // Makes 'exe "func Test()\n...\nendfunc"' work.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002274 if (*p == '\n')
2275 line_arg = p + 1;
2276 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002277 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002278
2279 /*
2280 * Read the body of the function, until ":endfunction" is found.
2281 */
2282 if (KeyTyped)
2283 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002284 // Check if the function already exists, don't let the user type the
2285 // whole function before telling him it doesn't work! For a script we
2286 // need to skip the body to be able to find what follows.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002287 if (!eap->skip && !eap->forceit)
2288 {
2289 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002290 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002291 else if (name != NULL && find_func(name) != NULL)
2292 emsg_funcname(e_funcexts, name);
2293 }
2294
2295 if (!eap->skip && did_emsg)
2296 goto erret;
2297
Bram Moolenaare38eab22019-12-05 21:50:01 +01002298 msg_putchar('\n'); // don't overwrite the function name
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002299 cmdline_row = msg_row;
2300 }
2301
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002302 // Save the starting line number.
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002303 sourcing_lnum_top = SOURCING_LNUM;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002304
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002305 indent = 2;
2306 nesting = 0;
2307 for (;;)
2308 {
2309 if (KeyTyped)
2310 {
2311 msg_scroll = TRUE;
2312 saved_wait_return = FALSE;
2313 }
2314 need_wait_return = FALSE;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002315
2316 if (line_arg != NULL)
2317 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002318 // Use eap->arg, split up in parts by line breaks.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002319 theline = line_arg;
2320 p = vim_strchr(theline, '\n');
2321 if (p == NULL)
2322 line_arg += STRLEN(line_arg);
2323 else
2324 {
2325 *p = NUL;
2326 line_arg = p + 1;
2327 }
2328 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002329 else
Bram Moolenaar53564f72017-06-24 14:48:11 +02002330 {
2331 vim_free(line_to_free);
2332 if (eap->getline == NULL)
Bram Moolenaare96a2492019-06-25 04:12:16 +02002333 theline = getcmdline(':', 0L, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002334 else
Bram Moolenaare96a2492019-06-25 04:12:16 +02002335 theline = eap->getline(':', eap->cookie, indent, do_concat);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002336 line_to_free = theline;
2337 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002338 if (KeyTyped)
2339 lines_left = Rows - 1;
2340 if (theline == NULL)
2341 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002342 emsg(_("E126: Missing :endfunction"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002343 goto erret;
2344 }
2345
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002346 // Detect line continuation: SOURCING_LNUM increased more than one.
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002347 sourcing_lnum_off = get_sourced_lnum(eap->getline, eap->cookie);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002348 if (SOURCING_LNUM < sourcing_lnum_off)
2349 sourcing_lnum_off -= SOURCING_LNUM;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002350 else
2351 sourcing_lnum_off = 0;
2352
2353 if (skip_until != NULL)
2354 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002355 // Don't check for ":endfunc" between
2356 // * ":append" and "."
2357 // * ":python <<EOF" and "EOF"
2358 // * ":let {var-name} =<< [trim] {marker}" and "{marker}"
2359 if (heredoc_trimmed == NULL
2360 || (is_heredoc && skipwhite(theline) == theline)
2361 || STRNCMP(theline, heredoc_trimmed,
2362 STRLEN(heredoc_trimmed)) == 0)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002363 {
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002364 if (heredoc_trimmed == NULL)
2365 p = theline;
2366 else if (is_heredoc)
2367 p = skipwhite(theline) == theline
2368 ? theline : theline + STRLEN(heredoc_trimmed);
2369 else
2370 p = theline + STRLEN(heredoc_trimmed);
Bram Moolenaar8471e572019-05-19 21:37:18 +02002371 if (STRCMP(p, skip_until) == 0)
2372 {
2373 VIM_CLEAR(skip_until);
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002374 VIM_CLEAR(heredoc_trimmed);
Bram Moolenaare96a2492019-06-25 04:12:16 +02002375 do_concat = TRUE;
Bram Moolenaarecaa75b2019-07-21 23:04:21 +02002376 is_heredoc = FALSE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002377 }
2378 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002379 }
2380 else
2381 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002382 // skip ':' and blanks
Bram Moolenaar1c465442017-03-12 20:10:05 +01002383 for (p = theline; VIM_ISWHITE(*p) || *p == ':'; ++p)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002384 ;
2385
Bram Moolenaare38eab22019-12-05 21:50:01 +01002386 // Check for "endfunction".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002387 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
2388 {
Bram Moolenaar53564f72017-06-24 14:48:11 +02002389 char_u *nextcmd = NULL;
2390
Bram Moolenaar663bb232017-06-22 19:12:10 +02002391 if (*p == '|')
Bram Moolenaar53564f72017-06-24 14:48:11 +02002392 nextcmd = p + 1;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002393 else if (line_arg != NULL && *skipwhite(line_arg) != NUL)
Bram Moolenaar53564f72017-06-24 14:48:11 +02002394 nextcmd = line_arg;
Bram Moolenaar663bb232017-06-22 19:12:10 +02002395 else if (*p != NUL && *p != '"' && p_verbose > 0)
Bram Moolenaarf8be4612017-06-23 20:52:40 +02002396 give_warning2(
2397 (char_u *)_("W22: Text found after :endfunction: %s"),
2398 p, TRUE);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002399 if (nextcmd != NULL)
2400 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002401 // Another command follows. If the line came from "eap" we
2402 // can simply point into it, otherwise we need to change
2403 // "eap->cmdlinep".
Bram Moolenaar53564f72017-06-24 14:48:11 +02002404 eap->nextcmd = nextcmd;
2405 if (line_to_free != NULL)
2406 {
2407 vim_free(*eap->cmdlinep);
2408 *eap->cmdlinep = line_to_free;
2409 line_to_free = NULL;
2410 }
2411 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002412 break;
2413 }
2414
Bram Moolenaare38eab22019-12-05 21:50:01 +01002415 // Increase indent inside "if", "while", "for" and "try", decrease
2416 // at "end".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002417 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
2418 indent -= 2;
2419 else if (STRNCMP(p, "if", 2) == 0
2420 || STRNCMP(p, "wh", 2) == 0
2421 || STRNCMP(p, "for", 3) == 0
2422 || STRNCMP(p, "try", 3) == 0)
2423 indent += 2;
2424
Bram Moolenaare38eab22019-12-05 21:50:01 +01002425 // Check for defining a function inside this function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002426 if (checkforcmd(&p, "function", 2))
2427 {
2428 if (*p == '!')
2429 p = skipwhite(p + 1);
2430 p += eval_fname_script(p);
2431 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
2432 if (*skipwhite(p) == '(')
2433 {
2434 ++nesting;
2435 indent += 2;
2436 }
2437 }
2438
Bram Moolenaare38eab22019-12-05 21:50:01 +01002439 // Check for ":append", ":change", ":insert".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002440 p = skip_range(p, NULL);
2441 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
Bram Moolenaar70bcd732017-01-12 22:20:54 +01002442 || (p[0] == 'c'
2443 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'h'
2444 && (!ASCII_ISALPHA(p[2]) || (p[2] == 'a'
2445 && (STRNCMP(&p[3], "nge", 3) != 0
2446 || !ASCII_ISALPHA(p[6])))))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002447 || (p[0] == 'i'
2448 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
2449 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
2450 skip_until = vim_strsave((char_u *)".");
2451
Bram Moolenaare38eab22019-12-05 21:50:01 +01002452 // Check for ":python <<EOF", ":tcl <<EOF", etc.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002453 arg = skipwhite(skiptowhite(p));
2454 if (arg[0] == '<' && arg[1] =='<'
2455 && ((p[0] == 'p' && p[1] == 'y'
Bram Moolenaarf42dd3c2017-01-28 16:06:38 +01002456 && (!ASCII_ISALNUM(p[2]) || p[2] == 't'
2457 || ((p[2] == '3' || p[2] == 'x')
2458 && !ASCII_ISALPHA(p[3]))))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002459 || (p[0] == 'p' && p[1] == 'e'
2460 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
2461 || (p[0] == 't' && p[1] == 'c'
2462 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
2463 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
2464 && !ASCII_ISALPHA(p[3]))
2465 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
2466 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
2467 || (p[0] == 'm' && p[1] == 'z'
2468 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
2469 ))
2470 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002471 // ":python <<" continues until a dot, like ":append"
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002472 p = skipwhite(arg + 2);
2473 if (*p == NUL)
2474 skip_until = vim_strsave((char_u *)".");
2475 else
2476 skip_until = vim_strsave(p);
2477 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002478
2479 // Check for ":let v =<< [trim] EOF"
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002480 // and ":let [a, b] =<< [trim] EOF"
Bram Moolenaar8471e572019-05-19 21:37:18 +02002481 arg = skipwhite(skiptowhite(p));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002482 if (*arg == '[')
2483 arg = vim_strchr(arg, ']');
2484 if (arg != NULL)
Bram Moolenaar8471e572019-05-19 21:37:18 +02002485 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002486 arg = skipwhite(skiptowhite(arg));
2487 if ( arg[0] == '=' && arg[1] == '<' && arg[2] =='<'
2488 && ((p[0] == 'l'
2489 && p[1] == 'e'
2490 && (!ASCII_ISALNUM(p[2])
2491 || (p[2] == 't' && !ASCII_ISALNUM(p[3]))))))
Bram Moolenaar8471e572019-05-19 21:37:18 +02002492 {
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002493 p = skipwhite(arg + 3);
2494 if (STRNCMP(p, "trim", 4) == 0)
2495 {
2496 // Ignore leading white space.
2497 p = skipwhite(p + 4);
2498 heredoc_trimmed = vim_strnsave(theline,
Bram Moolenaar8471e572019-05-19 21:37:18 +02002499 (int)(skipwhite(theline) - theline));
Bram Moolenaar1e673b92019-11-06 15:02:50 +01002500 }
2501 skip_until = vim_strnsave(p, (int)(skiptowhite(p) - p));
2502 do_concat = FALSE;
2503 is_heredoc = TRUE;
Bram Moolenaar8471e572019-05-19 21:37:18 +02002504 }
Bram Moolenaar8471e572019-05-19 21:37:18 +02002505 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002506 }
2507
Bram Moolenaare38eab22019-12-05 21:50:01 +01002508 // Add the line to the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002509 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002510 goto erret;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002511
Bram Moolenaare38eab22019-12-05 21:50:01 +01002512 // Copy the line to newly allocated memory. get_one_sourceline()
2513 // allocates 250 bytes per line, this saves 80% on average. The cost
2514 // is an extra alloc/free.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002515 p = vim_strsave(theline);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002516 if (p == NULL)
2517 goto erret;
2518 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002519
Bram Moolenaare38eab22019-12-05 21:50:01 +01002520 // Add NULL lines for continuation lines, so that the line count is
2521 // equal to the index in the growarray.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002522 while (sourcing_lnum_off-- > 0)
2523 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
2524
Bram Moolenaare38eab22019-12-05 21:50:01 +01002525 // Check for end of eap->arg.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002526 if (line_arg != NULL && *line_arg == NUL)
2527 line_arg = NULL;
2528 }
2529
Bram Moolenaare38eab22019-12-05 21:50:01 +01002530 // Don't define the function when skipping commands or when an error was
2531 // detected.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002532 if (eap->skip || did_emsg)
2533 goto erret;
2534
2535 /*
2536 * If there are no errors, add the function
2537 */
2538 if (fudi.fd_dict == NULL)
2539 {
2540 v = find_var(name, &ht, FALSE);
2541 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
2542 {
2543 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
2544 name);
2545 goto erret;
2546 }
2547
2548 fp = find_func(name);
2549 if (fp != NULL)
2550 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002551 // Function can be replaced with "function!" and when sourcing the
2552 // same script again, but only once.
2553 if (!eap->forceit
2554 && (fp->uf_script_ctx.sc_sid != current_sctx.sc_sid
2555 || fp->uf_script_ctx.sc_seq == current_sctx.sc_seq))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002556 {
2557 emsg_funcname(e_funcexts, name);
2558 goto erret;
2559 }
2560 if (fp->uf_calls > 0)
2561 {
Bram Moolenaarded5f1b2018-11-10 17:33:29 +01002562 emsg_funcname(
2563 N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002564 name);
2565 goto erret;
2566 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002567 if (fp->uf_refcount > 1)
2568 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002569 // This function is referenced somewhere, don't redefine it but
2570 // create a new one.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002571 --fp->uf_refcount;
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002572 fp->uf_flags |= FC_REMOVED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002573 fp = NULL;
2574 overwrite = TRUE;
2575 }
2576 else
2577 {
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002578 char_u *exp_name = fp->uf_name_exp;
2579
2580 // redefine existing function, keep the expanded name
Bram Moolenaard23a8232018-02-10 18:45:26 +01002581 VIM_CLEAR(name);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002582 fp->uf_name_exp = NULL;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002583 func_clear_items(fp);
Bram Moolenaarb9adef72020-01-02 14:31:22 +01002584 fp->uf_name_exp = exp_name;
Bram Moolenaar79c2ad52018-07-29 17:40:43 +02002585#ifdef FEAT_PROFILE
2586 fp->uf_profiling = FALSE;
2587 fp->uf_prof_initialized = FALSE;
2588#endif
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002589 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002590 }
2591 }
2592 else
2593 {
2594 char numbuf[20];
2595
2596 fp = NULL;
2597 if (fudi.fd_newkey == NULL && !eap->forceit)
2598 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002599 emsg(_(e_funcdict));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002600 goto erret;
2601 }
2602 if (fudi.fd_di == NULL)
2603 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002604 // Can't add a function to a locked dictionary
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002605 if (var_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002606 goto erret;
2607 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01002608 // Can't change an existing function if it is locked
Bram Moolenaar05c00c02019-02-11 22:00:11 +01002609 else if (var_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002610 goto erret;
2611
Bram Moolenaare38eab22019-12-05 21:50:01 +01002612 // Give the function a sequential number. Can only be used with a
2613 // Funcref!
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002614 vim_free(name);
2615 sprintf(numbuf, "%d", ++func_nr);
2616 name = vim_strsave((char_u *)numbuf);
2617 if (name == NULL)
2618 goto erret;
2619 }
2620
2621 if (fp == NULL)
2622 {
2623 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
2624 {
2625 int slen, plen;
2626 char_u *scriptname;
2627
Bram Moolenaare38eab22019-12-05 21:50:01 +01002628 // Check that the autoload name matches the script name.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002629 j = FAIL;
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002630 if (SOURCING_NAME != NULL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002631 {
2632 scriptname = autoload_name(name);
2633 if (scriptname != NULL)
2634 {
2635 p = vim_strchr(scriptname, '/');
2636 plen = (int)STRLEN(p);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002637 slen = (int)STRLEN(SOURCING_NAME);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002638 if (slen > plen && fnamecmp(p,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002639 SOURCING_NAME + slen - plen) == 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002640 j = OK;
2641 vim_free(scriptname);
2642 }
2643 }
2644 if (j == FAIL)
2645 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002646 semsg(_("E746: Function name does not match script file name: %s"), name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002647 goto erret;
2648 }
2649 }
2650
Bram Moolenaar47ed5532019-08-08 20:49:14 +02002651 fp = alloc_clear(offsetof(ufunc_T, uf_name) + STRLEN(name) + 1);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002652 if (fp == NULL)
2653 goto erret;
2654
2655 if (fudi.fd_dict != NULL)
2656 {
2657 if (fudi.fd_di == NULL)
2658 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002659 // add new dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002660 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
2661 if (fudi.fd_di == NULL)
2662 {
2663 vim_free(fp);
2664 goto erret;
2665 }
2666 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
2667 {
2668 vim_free(fudi.fd_di);
2669 vim_free(fp);
2670 goto erret;
2671 }
2672 }
2673 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01002674 // overwrite existing dict entry
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002675 clear_tv(&fudi.fd_di->di_tv);
2676 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002677 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002678
Bram Moolenaare38eab22019-12-05 21:50:01 +01002679 // behave like "dict" was used
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002680 flags |= FC_DICT;
2681 }
2682
Bram Moolenaare38eab22019-12-05 21:50:01 +01002683 // insert the new function in the function list
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01002684 set_ufunc_name(fp, name);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002685 if (overwrite)
2686 {
2687 hi = hash_find(&func_hashtab, name);
2688 hi->hi_key = UF2HIKEY(fp);
2689 }
2690 else if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002691 {
2692 vim_free(fp);
2693 goto erret;
2694 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002695 fp->uf_refcount = 1;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002696 }
2697 fp->uf_args = newargs;
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002698 fp->uf_def_args = default_args;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002699 fp->uf_lines = newlines;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002700 if ((flags & FC_CLOSURE) != 0)
2701 {
Bram Moolenaar58016442016-07-31 18:30:22 +02002702 if (register_closure(fp) == FAIL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002703 goto erret;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02002704 }
2705 else
2706 fp->uf_scoped = NULL;
2707
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002708#ifdef FEAT_PROFILE
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002709 if (prof_def_func())
2710 func_do_profile(fp);
2711#endif
2712 fp->uf_varargs = varargs;
Bram Moolenaar93343722018-07-10 19:39:18 +02002713 if (sandbox)
2714 flags |= FC_SANDBOX;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002715 fp->uf_flags = flags;
2716 fp->uf_calls = 0;
Bram Moolenaarf29c1c62018-09-10 21:05:02 +02002717 fp->uf_script_ctx = current_sctx;
Bram Moolenaarbc2cfe42019-07-04 14:57:12 +02002718 fp->uf_script_ctx.sc_lnum += sourcing_lnum_top;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002719 goto ret_free;
2720
2721erret:
2722 ga_clear_strings(&newargs);
Bram Moolenaar42ae78c2019-05-09 21:08:58 +02002723 ga_clear_strings(&default_args);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002724errret_2:
2725 ga_clear_strings(&newlines);
2726ret_free:
2727 vim_free(skip_until);
Bram Moolenaar53564f72017-06-24 14:48:11 +02002728 vim_free(line_to_free);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002729 vim_free(fudi.fd_newkey);
2730 vim_free(name);
2731 did_emsg |= saved_did_emsg;
2732 need_wait_return |= saved_wait_return;
2733}
2734
2735/*
2736 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
2737 * Return 2 if "p" starts with "s:".
2738 * Return 0 otherwise.
2739 */
2740 int
2741eval_fname_script(char_u *p)
2742{
Bram Moolenaare38eab22019-12-05 21:50:01 +01002743 // Use MB_STRICMP() because in Turkish comparing the "I" may not work with
2744 // the standard library function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002745 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
2746 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
2747 return 5;
2748 if (p[0] == 's' && p[1] == ':')
2749 return 2;
2750 return 0;
2751}
2752
2753 int
2754translated_function_exists(char_u *name)
2755{
2756 if (builtin_function(name, -1))
Bram Moolenaarac92e252019-08-03 21:58:38 +02002757 return has_internal_func(name);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002758 return find_func(name) != NULL;
2759}
2760
2761/*
2762 * Return TRUE if a function "name" exists.
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002763 * If "no_defef" is TRUE, do not dereference a Funcref.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002764 */
2765 int
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002766function_exists(char_u *name, int no_deref)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002767{
2768 char_u *nm = name;
2769 char_u *p;
2770 int n = FALSE;
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002771 int flag;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002772
Bram Moolenaarb54c3ff2016-07-31 14:11:58 +02002773 flag = TFN_INT | TFN_QUIET | TFN_NO_AUTOLOAD;
2774 if (no_deref)
2775 flag |= TFN_NO_DEREF;
2776 p = trans_function_name(&nm, FALSE, flag, NULL, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002777 nm = skipwhite(nm);
2778
Bram Moolenaare38eab22019-12-05 21:50:01 +01002779 // Only accept "funcname", "funcname ", "funcname (..." and
2780 // "funcname(...", not "funcname!...".
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002781 if (p != NULL && (*nm == NUL || *nm == '('))
2782 n = translated_function_exists(p);
2783 vim_free(p);
2784 return n;
2785}
2786
Bram Moolenaar113e1072019-01-20 15:30:40 +01002787#if defined(FEAT_PYTHON) || defined(FEAT_PYTHON3) || defined(PROTO)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002788 char_u *
2789get_expanded_name(char_u *name, int check)
2790{
2791 char_u *nm = name;
2792 char_u *p;
2793
2794 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
2795
2796 if (p != NULL && *nm == NUL)
2797 if (!check || translated_function_exists(p))
2798 return p;
2799
2800 vim_free(p);
2801 return NULL;
2802}
Bram Moolenaar113e1072019-01-20 15:30:40 +01002803#endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002804
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002805/*
2806 * Function given to ExpandGeneric() to obtain the list of user defined
2807 * function names.
2808 */
2809 char_u *
2810get_user_func_name(expand_T *xp, int idx)
2811{
2812 static long_u done;
2813 static hashitem_T *hi;
2814 ufunc_T *fp;
2815
2816 if (idx == 0)
2817 {
2818 done = 0;
2819 hi = func_hashtab.ht_array;
2820 }
2821 if (done < func_hashtab.ht_used)
2822 {
2823 if (done++ > 0)
2824 ++hi;
2825 while (HASHITEM_EMPTY(hi))
2826 ++hi;
2827 fp = HI2UF(hi);
2828
Bram Moolenaarb49edc12016-07-23 15:47:34 +02002829 if ((fp->uf_flags & FC_DICT)
2830 || STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002831 return (char_u *)""; // don't show dict and lambda functions
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002832
2833 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
Bram Moolenaare38eab22019-12-05 21:50:01 +01002834 return fp->uf_name; // prevents overflow
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002835
2836 cat_func_name(IObuff, fp);
2837 if (xp->xp_context != EXPAND_USER_FUNC)
2838 {
2839 STRCAT(IObuff, "(");
2840 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
2841 STRCAT(IObuff, ")");
2842 }
2843 return IObuff;
2844 }
2845 return NULL;
2846}
2847
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002848/*
2849 * ":delfunction {name}"
2850 */
2851 void
2852ex_delfunction(exarg_T *eap)
2853{
2854 ufunc_T *fp = NULL;
2855 char_u *p;
2856 char_u *name;
2857 funcdict_T fudi;
2858
2859 p = eap->arg;
2860 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
2861 vim_free(fudi.fd_newkey);
2862 if (name == NULL)
2863 {
2864 if (fudi.fd_dict != NULL && !eap->skip)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002865 emsg(_(e_funcref));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002866 return;
2867 }
2868 if (!ends_excmd(*skipwhite(p)))
2869 {
2870 vim_free(name);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002871 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002872 return;
2873 }
2874 eap->nextcmd = check_nextcmd(p);
2875 if (eap->nextcmd != NULL)
2876 *p = NUL;
2877
2878 if (!eap->skip)
2879 fp = find_func(name);
2880 vim_free(name);
2881
2882 if (!eap->skip)
2883 {
2884 if (fp == NULL)
2885 {
Bram Moolenaard6abcd12017-06-22 19:15:24 +02002886 if (!eap->forceit)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002887 semsg(_(e_nofunc), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002888 return;
2889 }
2890 if (fp->uf_calls > 0)
2891 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002892 semsg(_("E131: Cannot delete function %s: It is in use"), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002893 return;
2894 }
2895
2896 if (fudi.fd_dict != NULL)
2897 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002898 // Delete the dict item that refers to the function, it will
2899 // invoke func_unref() and possibly delete the function.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002900 dictitem_remove(fudi.fd_dict, fudi.fd_di);
2901 }
2902 else
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002903 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002904 // A normal function (not a numbered function or lambda) has a
2905 // refcount of 1 for the entry in the hashtable. When deleting
2906 // it and the refcount is more than one, it should be kept.
2907 // A numbered function and lambda should be kept if the refcount is
2908 // one or more.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002909 if (fp->uf_refcount > (func_name_refcount(fp->uf_name) ? 0 : 1))
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002910 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002911 // Function is still referenced somewhere. Don't free it but
2912 // do remove it from the hashtable.
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002913 if (func_remove(fp))
2914 fp->uf_refcount--;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002915 fp->uf_flags |= FC_DELETED;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002916 }
2917 else
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002918 func_clear_free(fp, FALSE);
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002919 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002920 }
2921}
2922
2923/*
2924 * Unreference a Function: decrement the reference count and free it when it
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002925 * becomes zero.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002926 */
2927 void
2928func_unref(char_u *name)
2929{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002930 ufunc_T *fp = NULL;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002931
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002932 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002933 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002934 fp = find_func(name);
2935 if (fp == NULL && isdigit(*name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002936 {
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002937#ifdef EXITFREE
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002938 if (!entered_free_all_mem)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002939#endif
Bram Moolenaar95f09602016-11-10 20:01:45 +01002940 internal_error("func_unref()");
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002941 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002942 if (fp != NULL && --fp->uf_refcount <= 0)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002943 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002944 // Only delete it when it's not being used. Otherwise it's done
2945 // when "uf_calls" becomes zero.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002946 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002947 func_clear_free(fp, FALSE);
Bram Moolenaar97baee82016-07-26 20:46:08 +02002948 }
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002949}
2950
2951/*
2952 * Unreference a Function: decrement the reference count and free it when it
2953 * becomes zero.
2954 */
2955 void
2956func_ptr_unref(ufunc_T *fp)
2957{
Bram Moolenaar97baee82016-07-26 20:46:08 +02002958 if (fp != NULL && --fp->uf_refcount <= 0)
2959 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01002960 // Only delete it when it's not being used. Otherwise it's done
2961 // when "uf_calls" becomes zero.
Bram Moolenaar97baee82016-07-26 20:46:08 +02002962 if (fp->uf_calls == 0)
Bram Moolenaar03ff9bc2017-02-02 22:59:27 +01002963 func_clear_free(fp, FALSE);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002964 }
2965}
2966
2967/*
2968 * Count a reference to a Function.
2969 */
2970 void
2971func_ref(char_u *name)
2972{
2973 ufunc_T *fp;
2974
Bram Moolenaar8dd3a432016-08-01 20:46:25 +02002975 if (name == NULL || !func_name_refcount(name))
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002976 return;
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002977 fp = find_func(name);
2978 if (fp != NULL)
2979 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002980 else if (isdigit(*name))
Bram Moolenaare38eab22019-12-05 21:50:01 +01002981 // Only give an error for a numbered function.
2982 // Fail silently, when named or lambda function isn't found.
Bram Moolenaar95f09602016-11-10 20:01:45 +01002983 internal_error("func_ref()");
Bram Moolenaar437bafe2016-08-01 15:40:54 +02002984}
2985
2986/*
2987 * Count a reference to a Function.
2988 */
2989 void
2990func_ptr_ref(ufunc_T *fp)
2991{
2992 if (fp != NULL)
2993 ++fp->uf_refcount;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02002994}
2995
2996/*
2997 * Return TRUE if items in "fc" do not have "copyID". That means they are not
2998 * referenced from anywhere that is in use.
2999 */
3000 static int
3001can_free_funccal(funccall_T *fc, int copyID)
3002{
3003 return (fc->l_varlist.lv_copyID != copyID
3004 && fc->l_vars.dv_copyID != copyID
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003005 && fc->l_avars.dv_copyID != copyID
3006 && fc->fc_copyID != copyID);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003007}
3008
3009/*
3010 * ":return [expr]"
3011 */
3012 void
3013ex_return(exarg_T *eap)
3014{
3015 char_u *arg = eap->arg;
3016 typval_T rettv;
3017 int returning = FALSE;
3018
3019 if (current_funccal == NULL)
3020 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003021 emsg(_("E133: :return not inside a function"));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003022 return;
3023 }
3024
3025 if (eap->skip)
3026 ++emsg_skip;
3027
3028 eap->nextcmd = NULL;
3029 if ((*arg != NUL && *arg != '|' && *arg != '\n')
3030 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
3031 {
3032 if (!eap->skip)
3033 returning = do_return(eap, FALSE, TRUE, &rettv);
3034 else
3035 clear_tv(&rettv);
3036 }
Bram Moolenaare38eab22019-12-05 21:50:01 +01003037 // It's safer to return also on error.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003038 else if (!eap->skip)
3039 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003040 // In return statement, cause_abort should be force_abort.
Bram Moolenaarfabaf752017-12-23 17:26:11 +01003041 update_force_abort();
3042
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003043 /*
3044 * Return unless the expression evaluation has been cancelled due to an
3045 * aborting error, an interrupt, or an exception.
3046 */
3047 if (!aborting())
3048 returning = do_return(eap, FALSE, TRUE, NULL);
3049 }
3050
Bram Moolenaare38eab22019-12-05 21:50:01 +01003051 // When skipping or the return gets pending, advance to the next command
3052 // in this line (!returning). Otherwise, ignore the rest of the line.
3053 // Following lines will be ignored by get_func_line().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003054 if (returning)
3055 eap->nextcmd = NULL;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003056 else if (eap->nextcmd == NULL) // no argument
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003057 eap->nextcmd = check_nextcmd(arg);
3058
3059 if (eap->skip)
3060 --emsg_skip;
3061}
3062
3063/*
3064 * ":1,25call func(arg1, arg2)" function call.
3065 */
3066 void
3067ex_call(exarg_T *eap)
3068{
3069 char_u *arg = eap->arg;
3070 char_u *startarg;
3071 char_u *name;
3072 char_u *tofree;
3073 int len;
3074 typval_T rettv;
3075 linenr_T lnum;
3076 int doesrange;
3077 int failed = FALSE;
3078 funcdict_T fudi;
3079 partial_T *partial = NULL;
3080
3081 if (eap->skip)
3082 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003083 // trans_function_name() doesn't work well when skipping, use eval0()
3084 // instead to skip to any following command, e.g. for:
3085 // :if 0 | call dict.foo().bar() | endif
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003086 ++emsg_skip;
3087 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3088 clear_tv(&rettv);
3089 --emsg_skip;
3090 return;
3091 }
3092
3093 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
3094 if (fudi.fd_newkey != NULL)
3095 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003096 // Still need to give an error message for missing key.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003097 semsg(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003098 vim_free(fudi.fd_newkey);
3099 }
3100 if (tofree == NULL)
3101 return;
3102
Bram Moolenaare38eab22019-12-05 21:50:01 +01003103 // Increase refcount on dictionary, it could get deleted when evaluating
3104 // the arguments.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003105 if (fudi.fd_dict != NULL)
3106 ++fudi.fd_dict->dv_refcount;
3107
Bram Moolenaare38eab22019-12-05 21:50:01 +01003108 // If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3109 // contents. For VAR_PARTIAL get its partial, unless we already have one
3110 // from trans_function_name().
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003111 len = (int)STRLEN(tofree);
3112 name = deref_func_name(tofree, &len,
3113 partial != NULL ? NULL : &partial, FALSE);
3114
Bram Moolenaare38eab22019-12-05 21:50:01 +01003115 // Skip white space to allow ":call func ()". Not good, but required for
3116 // backward compatibility.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003117 startarg = skipwhite(arg);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003118 rettv.v_type = VAR_UNKNOWN; // clear_tv() uses this
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003119
3120 if (*startarg != '(')
3121 {
Bram Moolenaarac92e252019-08-03 21:58:38 +02003122 semsg(_(e_missingparen), eap->arg);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003123 goto end;
3124 }
3125
3126 /*
3127 * When skipping, evaluate the function once, to find the end of the
3128 * arguments.
3129 * When the function takes a range, this is discovered after the first
3130 * call, and the loop is broken.
3131 */
3132 if (eap->skip)
3133 {
3134 ++emsg_skip;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003135 lnum = eap->line2; // do it once, also with an invalid range
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003136 }
3137 else
3138 lnum = eap->line1;
3139 for ( ; lnum <= eap->line2; ++lnum)
3140 {
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003141 funcexe_T funcexe;
3142
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003143 if (!eap->skip && eap->addr_count > 0)
3144 {
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003145 if (lnum > curbuf->b_ml.ml_line_count)
3146 {
3147 // If the function deleted lines or switched to another buffer
3148 // the line number may become invalid.
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003149 emsg(_(e_invrange));
Bram Moolenaar9e353b52018-11-04 23:39:38 +01003150 break;
3151 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003152 curwin->w_cursor.lnum = lnum;
3153 curwin->w_cursor.col = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003154 curwin->w_cursor.coladd = 0;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003155 }
3156 arg = startarg;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003157
Bram Moolenaarac92e252019-08-03 21:58:38 +02003158 vim_memset(&funcexe, 0, sizeof(funcexe));
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003159 funcexe.firstline = eap->line1;
3160 funcexe.lastline = eap->line2;
3161 funcexe.doesrange = &doesrange;
3162 funcexe.evaluate = !eap->skip;
3163 funcexe.partial = partial;
3164 funcexe.selfdict = fudi.fd_dict;
3165 if (get_func_tv(name, -1, &rettv, &arg, &funcexe) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003166 {
3167 failed = TRUE;
3168 break;
3169 }
Bram Moolenaarc6f9f732018-02-11 19:06:26 +01003170 if (has_watchexpr())
3171 dbg_check_breakpoint(eap);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003172
Bram Moolenaar9cfe8f62019-08-17 21:04:16 +02003173 // Handle a function returning a Funcref, Dictionary or List.
3174 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE,
3175 name, &name) == FAIL)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003176 {
3177 failed = TRUE;
3178 break;
3179 }
3180
3181 clear_tv(&rettv);
3182 if (doesrange || eap->skip)
3183 break;
3184
Bram Moolenaare38eab22019-12-05 21:50:01 +01003185 // Stop when immediately aborting on error, or when an interrupt
3186 // occurred or an exception was thrown but not caught.
3187 // get_func_tv() returned OK, so that the check for trailing
3188 // characters below is executed.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003189 if (aborting())
3190 break;
3191 }
3192 if (eap->skip)
3193 --emsg_skip;
3194
3195 if (!failed)
3196 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003197 // Check for trailing illegal characters and a following command.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003198 if (!ends_excmd(*arg))
3199 {
3200 emsg_severe = TRUE;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003201 emsg(_(e_trailing));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003202 }
3203 else
3204 eap->nextcmd = check_nextcmd(arg);
3205 }
3206
3207end:
3208 dict_unref(fudi.fd_dict);
3209 vim_free(tofree);
3210}
3211
3212/*
3213 * Return from a function. Possibly makes the return pending. Also called
3214 * for a pending return at the ":endtry" or after returning from an extra
3215 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
3216 * when called due to a ":return" command. "rettv" may point to a typval_T
3217 * with the return rettv. Returns TRUE when the return can be carried out,
3218 * FALSE when the return gets pending.
3219 */
3220 int
3221do_return(
3222 exarg_T *eap,
3223 int reanimate,
3224 int is_cmd,
3225 void *rettv)
3226{
3227 int idx;
Bram Moolenaarddef1292019-12-16 17:10:33 +01003228 cstack_T *cstack = eap->cstack;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003229
3230 if (reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003231 // Undo the return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003232 current_funccal->returned = FALSE;
3233
3234 /*
3235 * Cleanup (and inactivate) conditionals, but stop when a try conditional
3236 * not in its finally clause (which then is to be executed next) is found.
3237 * In this case, make the ":return" pending for execution at the ":endtry".
3238 * Otherwise, return normally.
3239 */
3240 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
3241 if (idx >= 0)
3242 {
3243 cstack->cs_pending[idx] = CSTP_RETURN;
3244
3245 if (!is_cmd && !reanimate)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003246 // A pending return again gets pending. "rettv" points to an
3247 // allocated variable with the rettv of the original ":return"'s
3248 // argument if present or is NULL else.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003249 cstack->cs_rettv[idx] = rettv;
3250 else
3251 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003252 // When undoing a return in order to make it pending, get the stored
3253 // return rettv.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003254 if (reanimate)
3255 rettv = current_funccal->rettv;
3256
3257 if (rettv != NULL)
3258 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003259 // Store the value of the pending return.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003260 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
3261 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
3262 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003263 emsg(_(e_outofmem));
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003264 }
3265 else
3266 cstack->cs_rettv[idx] = NULL;
3267
3268 if (reanimate)
3269 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003270 // The pending return value could be overwritten by a ":return"
3271 // without argument in a finally clause; reset the default
3272 // return value.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003273 current_funccal->rettv->v_type = VAR_NUMBER;
3274 current_funccal->rettv->vval.v_number = 0;
3275 }
3276 }
3277 report_make_pending(CSTP_RETURN, rettv);
3278 }
3279 else
3280 {
3281 current_funccal->returned = TRUE;
3282
Bram Moolenaare38eab22019-12-05 21:50:01 +01003283 // If the return is carried out now, store the return value. For
3284 // a return immediately after reanimation, the value is already
3285 // there.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003286 if (!reanimate && rettv != NULL)
3287 {
3288 clear_tv(current_funccal->rettv);
3289 *current_funccal->rettv = *(typval_T *)rettv;
3290 if (!is_cmd)
3291 vim_free(rettv);
3292 }
3293 }
3294
3295 return idx < 0;
3296}
3297
3298/*
3299 * Free the variable with a pending return value.
3300 */
3301 void
3302discard_pending_return(void *rettv)
3303{
3304 free_tv((typval_T *)rettv);
3305}
3306
3307/*
3308 * Generate a return command for producing the value of "rettv". The result
3309 * is an allocated string. Used by report_pending() for verbose messages.
3310 */
3311 char_u *
3312get_return_cmd(void *rettv)
3313{
3314 char_u *s = NULL;
3315 char_u *tofree = NULL;
3316 char_u numbuf[NUMBUFLEN];
3317
3318 if (rettv != NULL)
3319 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
3320 if (s == NULL)
3321 s = (char_u *)"";
3322
3323 STRCPY(IObuff, ":return ");
3324 STRNCPY(IObuff + 8, s, IOSIZE - 8);
3325 if (STRLEN(s) + 8 >= IOSIZE)
3326 STRCPY(IObuff + IOSIZE - 4, "...");
3327 vim_free(tofree);
3328 return vim_strsave(IObuff);
3329}
3330
3331/*
3332 * Get next function line.
3333 * Called by do_cmdline() to get the next line.
3334 * Returns allocated string, or NULL for end of function.
3335 */
3336 char_u *
3337get_func_line(
3338 int c UNUSED,
3339 void *cookie,
Bram Moolenaare96a2492019-06-25 04:12:16 +02003340 int indent UNUSED,
3341 int do_concat UNUSED)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003342{
3343 funccall_T *fcp = (funccall_T *)cookie;
3344 ufunc_T *fp = fcp->func;
3345 char_u *retval;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003346 garray_T *gap; // growarray with function lines
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003347
Bram Moolenaare38eab22019-12-05 21:50:01 +01003348 // If breakpoints have been added/deleted need to check for it.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003349 if (fcp->dbg_tick != debug_tick)
3350 {
3351 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003352 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003353 fcp->dbg_tick = debug_tick;
3354 }
3355#ifdef FEAT_PROFILE
3356 if (do_profiling == PROF_YES)
3357 func_line_end(cookie);
3358#endif
3359
3360 gap = &fp->uf_lines;
3361 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3362 || fcp->returned)
3363 retval = NULL;
3364 else
3365 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003366 // Skip NULL lines (continuation lines).
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003367 while (fcp->linenr < gap->ga_len
3368 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
3369 ++fcp->linenr;
3370 if (fcp->linenr >= gap->ga_len)
3371 retval = NULL;
3372 else
3373 {
3374 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003375 SOURCING_LNUM = fcp->linenr;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003376#ifdef FEAT_PROFILE
3377 if (do_profiling == PROF_YES)
3378 func_line_start(cookie);
3379#endif
3380 }
3381 }
3382
Bram Moolenaare38eab22019-12-05 21:50:01 +01003383 // Did we encounter a breakpoint?
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003384 if (fcp->breakpoint != 0 && fcp->breakpoint <= SOURCING_LNUM)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003385 {
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003386 dbg_breakpoint(fp->uf_name, SOURCING_LNUM);
Bram Moolenaare38eab22019-12-05 21:50:01 +01003387 // Find next breakpoint.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003388 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar1a47ae32019-12-29 23:04:25 +01003389 SOURCING_LNUM);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003390 fcp->dbg_tick = debug_tick;
3391 }
3392
3393 return retval;
3394}
3395
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003396/*
3397 * Return TRUE if the currently active function should be ended, because a
3398 * return was encountered or an error occurred. Used inside a ":while".
3399 */
3400 int
3401func_has_ended(void *cookie)
3402{
3403 funccall_T *fcp = (funccall_T *)cookie;
3404
Bram Moolenaare38eab22019-12-05 21:50:01 +01003405 // Ignore the "abort" flag if the abortion behavior has been changed due to
3406 // an error inside a try conditional.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003407 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
3408 || fcp->returned);
3409}
3410
3411/*
3412 * return TRUE if cookie indicates a function which "abort"s on errors.
3413 */
3414 int
3415func_has_abort(
3416 void *cookie)
3417{
3418 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
3419}
3420
3421
3422/*
3423 * Turn "dict.Func" into a partial for "Func" bound to "dict".
3424 * Don't do this when "Func" is already a partial that was bound
3425 * explicitly (pt_auto is FALSE).
3426 * Changes "rettv" in-place.
3427 * Returns the updated "selfdict_in".
3428 */
3429 dict_T *
3430make_partial(dict_T *selfdict_in, typval_T *rettv)
3431{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003432 char_u *fname;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003433 char_u *tofree = NULL;
3434 ufunc_T *fp;
3435 char_u fname_buf[FLEN_FIXED + 1];
3436 int error;
3437 dict_T *selfdict = selfdict_in;
3438
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003439 if (rettv->v_type == VAR_PARTIAL && rettv->vval.v_partial->pt_func != NULL)
3440 fp = rettv->vval.v_partial->pt_func;
3441 else
3442 {
3443 fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
3444 : rettv->vval.v_partial->pt_name;
Bram Moolenaare38eab22019-12-05 21:50:01 +01003445 // Translate "s:func" to the stored function name.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003446 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
3447 fp = find_func(fname);
3448 vim_free(tofree);
3449 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003450
3451 if (fp != NULL && (fp->uf_flags & FC_DICT))
3452 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003453 partial_T *pt = ALLOC_CLEAR_ONE(partial_T);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003454
3455 if (pt != NULL)
3456 {
3457 pt->pt_refcount = 1;
3458 pt->pt_dict = selfdict;
3459 pt->pt_auto = TRUE;
3460 selfdict = NULL;
3461 if (rettv->v_type == VAR_FUNC)
3462 {
Bram Moolenaare38eab22019-12-05 21:50:01 +01003463 // Just a function: Take over the function name and use
3464 // selfdict.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003465 pt->pt_name = rettv->vval.v_string;
3466 }
3467 else
3468 {
3469 partial_T *ret_pt = rettv->vval.v_partial;
3470 int i;
3471
Bram Moolenaare38eab22019-12-05 21:50:01 +01003472 // Partial: copy the function name, use selfdict and copy
3473 // args. Can't take over name or args, the partial might
3474 // be referenced elsewhere.
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003475 if (ret_pt->pt_name != NULL)
3476 {
3477 pt->pt_name = vim_strsave(ret_pt->pt_name);
3478 func_ref(pt->pt_name);
3479 }
3480 else
3481 {
3482 pt->pt_func = ret_pt->pt_func;
3483 func_ptr_ref(pt->pt_func);
3484 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003485 if (ret_pt->pt_argc > 0)
3486 {
Bram Moolenaarc799fe22019-05-28 23:08:19 +02003487 pt->pt_argv = ALLOC_MULT(typval_T, ret_pt->pt_argc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003488 if (pt->pt_argv == NULL)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003489 // out of memory: drop the arguments
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003490 pt->pt_argc = 0;
3491 else
3492 {
3493 pt->pt_argc = ret_pt->pt_argc;
3494 for (i = 0; i < pt->pt_argc; i++)
3495 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
3496 }
3497 }
3498 partial_unref(ret_pt);
3499 }
3500 rettv->v_type = VAR_PARTIAL;
3501 rettv->vval.v_partial = pt;
3502 }
3503 }
3504 return selfdict;
3505}
3506
3507/*
3508 * Return the name of the executed function.
3509 */
3510 char_u *
3511func_name(void *cookie)
3512{
3513 return ((funccall_T *)cookie)->func->uf_name;
3514}
3515
3516/*
3517 * Return the address holding the next breakpoint line for a funccall cookie.
3518 */
3519 linenr_T *
3520func_breakpoint(void *cookie)
3521{
3522 return &((funccall_T *)cookie)->breakpoint;
3523}
3524
3525/*
3526 * Return the address holding the debug tick for a funccall cookie.
3527 */
3528 int *
3529func_dbg_tick(void *cookie)
3530{
3531 return &((funccall_T *)cookie)->dbg_tick;
3532}
3533
3534/*
3535 * Return the nesting level for a funccall cookie.
3536 */
3537 int
3538func_level(void *cookie)
3539{
3540 return ((funccall_T *)cookie)->level;
3541}
3542
3543/*
3544 * Return TRUE when a function was ended by a ":return" command.
3545 */
3546 int
3547current_func_returned(void)
3548{
3549 return current_funccal->returned;
3550}
3551
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003552 int
3553free_unref_funccal(int copyID, int testing)
3554{
3555 int did_free = FALSE;
3556 int did_free_funccal = FALSE;
3557 funccall_T *fc, **pfc;
3558
3559 for (pfc = &previous_funccal; *pfc != NULL; )
3560 {
3561 if (can_free_funccal(*pfc, copyID))
3562 {
3563 fc = *pfc;
3564 *pfc = fc->caller;
Bram Moolenaar209b8e32019-03-14 13:43:24 +01003565 free_funccal_contents(fc);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003566 did_free = TRUE;
3567 did_free_funccal = TRUE;
3568 }
3569 else
3570 pfc = &(*pfc)->caller;
3571 }
3572 if (did_free_funccal)
Bram Moolenaare38eab22019-12-05 21:50:01 +01003573 // When a funccal was freed some more items might be garbage
3574 // collected, so run again.
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003575 (void)garbage_collect(testing);
3576
3577 return did_free;
3578}
3579
3580/*
Bram Moolenaarba209902016-08-24 22:06:38 +02003581 * Get function call environment based on backtrace debug level
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003582 */
3583 static funccall_T *
3584get_funccal(void)
3585{
3586 int i;
3587 funccall_T *funccal;
3588 funccall_T *temp_funccal;
3589
3590 funccal = current_funccal;
3591 if (debug_backtrace_level > 0)
3592 {
3593 for (i = 0; i < debug_backtrace_level; i++)
3594 {
3595 temp_funccal = funccal->caller;
3596 if (temp_funccal)
3597 funccal = temp_funccal;
3598 else
Bram Moolenaare38eab22019-12-05 21:50:01 +01003599 // backtrace level overflow. reset to max
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003600 debug_backtrace_level = i;
3601 }
3602 }
3603 return funccal;
3604}
3605
3606/*
3607 * Return the hashtable used for local variables in the current funccal.
3608 * Return NULL if there is no current funccal.
3609 */
3610 hashtab_T *
3611get_funccal_local_ht()
3612{
3613 if (current_funccal == NULL)
3614 return NULL;
3615 return &get_funccal()->l_vars.dv_hashtab;
3616}
3617
3618/*
3619 * Return the l: scope variable.
3620 * Return NULL if there is no current funccal.
3621 */
3622 dictitem_T *
3623get_funccal_local_var()
3624{
3625 if (current_funccal == NULL)
3626 return NULL;
3627 return &get_funccal()->l_vars_var;
3628}
3629
3630/*
3631 * Return the hashtable used for argument in the current funccal.
3632 * Return NULL if there is no current funccal.
3633 */
3634 hashtab_T *
3635get_funccal_args_ht()
3636{
3637 if (current_funccal == NULL)
3638 return NULL;
3639 return &get_funccal()->l_avars.dv_hashtab;
3640}
3641
3642/*
3643 * Return the a: scope variable.
3644 * Return NULL if there is no current funccal.
3645 */
3646 dictitem_T *
3647get_funccal_args_var()
3648{
3649 if (current_funccal == NULL)
3650 return NULL;
Bram Moolenaarc7d9eac2017-02-01 20:26:51 +01003651 return &get_funccal()->l_avars_var;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003652}
3653
3654/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003655 * List function variables, if there is a function.
3656 */
3657 void
3658list_func_vars(int *first)
3659{
3660 if (current_funccal != NULL)
3661 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar32526b32019-01-19 17:43:09 +01003662 "l:", FALSE, first);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003663}
3664
3665/*
3666 * If "ht" is the hashtable for local variables in the current funccal, return
3667 * the dict that contains it.
3668 * Otherwise return NULL.
3669 */
3670 dict_T *
3671get_current_funccal_dict(hashtab_T *ht)
3672{
3673 if (current_funccal != NULL
3674 && ht == &current_funccal->l_vars.dv_hashtab)
3675 return &current_funccal->l_vars;
3676 return NULL;
3677}
3678
3679/*
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003680 * Search hashitem in parent scope.
3681 */
3682 hashitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003683find_hi_in_scoped_ht(char_u *name, hashtab_T **pht)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003684{
3685 funccall_T *old_current_funccal = current_funccal;
3686 hashtab_T *ht;
3687 hashitem_T *hi = NULL;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003688 char_u *varname;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003689
3690 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3691 return NULL;
3692
Bram Moolenaare38eab22019-12-05 21:50:01 +01003693 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003694 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar58016442016-07-31 18:30:22 +02003695 while (current_funccal != NULL)
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003696 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003697 ht = find_var_ht(name, &varname);
3698 if (ht != NULL && *varname != NUL)
Bram Moolenaar58016442016-07-31 18:30:22 +02003699 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003700 hi = hash_find(ht, varname);
Bram Moolenaar58016442016-07-31 18:30:22 +02003701 if (!HASHITEM_EMPTY(hi))
3702 {
3703 *pht = ht;
3704 break;
3705 }
3706 }
3707 if (current_funccal == current_funccal->func->uf_scoped)
3708 break;
3709 current_funccal = current_funccal->func->uf_scoped;
Bram Moolenaar10ce39a2016-07-29 22:37:06 +02003710 }
3711 current_funccal = old_current_funccal;
3712
3713 return hi;
3714}
3715
3716/*
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003717 * Search variable in parent scope.
3718 */
3719 dictitem_T *
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003720find_var_in_scoped_ht(char_u *name, int no_autoload)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003721{
3722 dictitem_T *v = NULL;
3723 funccall_T *old_current_funccal = current_funccal;
3724 hashtab_T *ht;
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003725 char_u *varname;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003726
3727 if (current_funccal == NULL || current_funccal->func->uf_scoped == NULL)
3728 return NULL;
3729
Bram Moolenaare38eab22019-12-05 21:50:01 +01003730 // Search in parent scope which is possible to reference from lambda
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003731 current_funccal = current_funccal->func->uf_scoped;
3732 while (current_funccal)
3733 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003734 ht = find_var_ht(name, &varname);
3735 if (ht != NULL && *varname != NUL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003736 {
Bram Moolenaarba96e9a2016-08-01 17:10:20 +02003737 v = find_var_in_ht(ht, *name, varname, no_autoload);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003738 if (v != NULL)
3739 break;
3740 }
3741 if (current_funccal == current_funccal->func->uf_scoped)
3742 break;
3743 current_funccal = current_funccal->func->uf_scoped;
3744 }
3745 current_funccal = old_current_funccal;
3746
3747 return v;
3748}
3749
3750/*
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003751 * Set "copyID + 1" in previous_funccal and callers.
3752 */
3753 int
3754set_ref_in_previous_funccal(int copyID)
3755{
3756 int abort = FALSE;
3757 funccall_T *fc;
3758
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003759 for (fc = previous_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003760 {
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003761 fc->fc_copyID = copyID + 1;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003762 abort = abort
3763 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1, NULL)
3764 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003765 || set_ref_in_list_items(&fc->l_varlist, copyID + 1, NULL);
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003766 }
3767 return abort;
3768}
3769
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003770 static int
3771set_ref_in_funccal(funccall_T *fc, int copyID)
3772{
3773 int abort = FALSE;
3774
3775 if (fc->fc_copyID != copyID)
3776 {
3777 fc->fc_copyID = copyID;
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003778 abort = abort
3779 || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL)
3780 || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL)
Bram Moolenaar7be3ab22019-06-23 01:46:15 +02003781 || set_ref_in_list_items(&fc->l_varlist, copyID, NULL)
Bram Moolenaar6e5000d2019-06-17 21:18:41 +02003782 || set_ref_in_func(NULL, fc->func, copyID);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003783 }
3784 return abort;
3785}
3786
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003787/*
3788 * Set "copyID" in all local vars and arguments in the call stack.
3789 */
3790 int
3791set_ref_in_call_stack(int copyID)
3792{
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003793 int abort = FALSE;
3794 funccall_T *fc;
3795 funccal_entry_T *entry;
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003796
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003797 for (fc = current_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003798 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003799
3800 // Also go through the funccal_stack.
Bram Moolenaar75a1a942019-06-20 03:45:36 +02003801 for (entry = funccal_stack; !abort && entry != NULL; entry = entry->next)
3802 for (fc = entry->top_funccal; !abort && fc != NULL; fc = fc->caller)
Bram Moolenaarc07f67a2019-06-06 19:03:17 +02003803 abort = abort || set_ref_in_funccal(fc, copyID);
3804
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003805 return abort;
3806}
3807
3808/*
3809 * Set "copyID" in all functions available by name.
3810 */
3811 int
3812set_ref_in_functions(int copyID)
3813{
3814 int todo;
3815 hashitem_T *hi = NULL;
3816 int abort = FALSE;
3817 ufunc_T *fp;
3818
3819 todo = (int)func_hashtab.ht_used;
3820 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003821 {
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003822 if (!HASHITEM_EMPTY(hi))
3823 {
3824 --todo;
3825 fp = HI2UF(hi);
3826 if (!func_name_refcount(fp->uf_name))
3827 abort = abort || set_ref_in_func(NULL, fp, copyID);
3828 }
Bram Moolenaara9b579f2016-07-17 18:29:19 +02003829 }
3830 return abort;
3831}
3832
3833/*
3834 * Set "copyID" in all function arguments.
3835 */
3836 int
3837set_ref_in_func_args(int copyID)
3838{
3839 int i;
3840 int abort = FALSE;
3841
3842 for (i = 0; i < funcargs.ga_len; ++i)
3843 abort = abort || set_ref_in_item(((typval_T **)funcargs.ga_data)[i],
3844 copyID, NULL, NULL);
3845 return abort;
3846}
3847
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003848/*
3849 * Mark all lists and dicts referenced through function "name" with "copyID".
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003850 * Returns TRUE if setting references failed somehow.
3851 */
3852 int
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003853set_ref_in_func(char_u *name, ufunc_T *fp_in, int copyID)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003854{
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003855 ufunc_T *fp = fp_in;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003856 funccall_T *fc;
Bram Moolenaaref140542019-12-31 21:27:13 +01003857 int error = FCERR_NONE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003858 char_u fname_buf[FLEN_FIXED + 1];
3859 char_u *tofree = NULL;
3860 char_u *fname;
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003861 int abort = FALSE;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003862
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003863 if (name == NULL && fp_in == NULL)
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003864 return FALSE;
3865
Bram Moolenaar437bafe2016-08-01 15:40:54 +02003866 if (fp_in == NULL)
3867 {
3868 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
3869 fp = find_func(fname);
3870 }
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003871 if (fp != NULL)
3872 {
3873 for (fc = fp->uf_scoped; fc != NULL; fc = fc->func->uf_scoped)
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003874 abort = abort || set_ref_in_funccal(fc, copyID);
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003875 }
3876 vim_free(tofree);
Bram Moolenaarbc7ce672016-08-01 22:49:22 +02003877 return abort;
Bram Moolenaar1e96d9b2016-07-29 22:15:09 +02003878}
3879
Bram Moolenaare38eab22019-12-05 21:50:01 +01003880#endif // FEAT_EVAL