-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserfunc.c
3892 lines (3541 loc) · 93.2 KB
/
userfunc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vi:set ts=8 sts=4 sw=4 noet:
*
* VIM - Vi IMproved by Bram Moolenaar
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
* See README.txt for an overview of the Vim source code.
*/
/*
* eval.c: User defined function support
*/
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
/* function flags */
#define FC_ABORT 0x01 /* abort function on error */
#define FC_RANGE 0x02 /* function accepts range */
#define FC_DICT 0x04 /* Dict function, uses "self" */
#define FC_CLOSURE 0x08 /* closure, uses outer scope variables */
#define FC_DELETED 0x10 /* :delfunction used while uf_refcount > 0 */
#define FC_REMOVED 0x20 /* function redefined while uf_refcount > 0 */
/* From user function to hashitem and back. */
#define UF2HIKEY(fp) ((fp)->uf_name)
#define HIKEY2UF(p) ((ufunc_T *)(p - offsetof(ufunc_T, uf_name)))
#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
/*
* All user-defined functions are found in this hashtable.
*/
static hashtab_T func_hashtab;
/* Used by get_func_tv() */
static garray_T funcargs = GA_EMPTY;
/* pointer to funccal for currently active function */
funccall_T *current_funccal = NULL;
/* Pointer to list of previously used funccal, still around because some
* item in it is still being used. */
funccall_T *previous_funccal = NULL;
static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
static char *e_funcdict = N_("E717: Dictionary entry already exists");
static char *e_funcref = N_("E718: Funcref required");
static char *e_nofunc = N_("E130: Unknown function: %s");
#ifdef FEAT_PROFILE
static void func_do_profile(ufunc_T *fp);
static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
static int
# ifdef __BORLANDC__
_RTLENTRYF
# endif
prof_total_cmp(const void *s1, const void *s2);
static int
# ifdef __BORLANDC__
_RTLENTRYF
# endif
prof_self_cmp(const void *s1, const void *s2);
#endif
static void funccal_unref(funccall_T *fc, ufunc_T *fp, int force);
void
func_init()
{
hash_init(&func_hashtab);
}
/*
* Get function arguments.
*/
static int
get_function_args(
char_u **argp,
char_u endchar,
garray_T *newargs,
int *varargs,
int skip)
{
int mustend = FALSE;
char_u *arg = *argp;
char_u *p = arg;
int c;
int i;
if (newargs != NULL)
ga_init2(newargs, (int)sizeof(char_u *), 3);
if (varargs != NULL)
*varargs = FALSE;
/*
* Isolate the arguments: "arg1, arg2, ...)"
*/
while (*p != endchar)
{
if (p[0] == '.' && p[1] == '.' && p[2] == '.')
{
if (varargs != NULL)
*varargs = TRUE;
p += 3;
mustend = TRUE;
}
else
{
arg = p;
while (ASCII_ISALNUM(*p) || *p == '_')
++p;
if (arg == p || isdigit(*arg)
|| (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
|| (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
{
if (!skip)
EMSG2(_("E125: Illegal argument: %s"), arg);
break;
}
if (newargs != NULL && ga_grow(newargs, 1) == FAIL)
goto err_ret;
if (newargs != NULL)
{
c = *p;
*p = NUL;
arg = vim_strsave(arg);
if (arg == NULL)
{
*p = c;
goto err_ret;
}
/* Check for duplicate argument name. */
for (i = 0; i < newargs->ga_len; ++i)
if (STRCMP(((char_u **)(newargs->ga_data))[i], arg) == 0)
{
EMSG2(_("E853: Duplicate argument name: %s"), arg);
vim_free(arg);
goto err_ret;
}
((char_u **)(newargs->ga_data))[newargs->ga_len] = arg;
newargs->ga_len++;
*p = c;
}
if (*p == ',')
++p;
else
mustend = TRUE;
}
p = skipwhite(p);
if (mustend && *p != endchar)
{
if (!skip)
EMSG2(_(e_invarg2), *argp);
break;
}
}
if (*p != endchar)
goto err_ret;
++p; /* skip "endchar" */
*argp = p;
return OK;
err_ret:
if (newargs != NULL)
ga_clear_strings(newargs);
return FAIL;
}
/*
* Register function "fp" as using "current_funccal" as its scope.
*/
static int
register_closure(ufunc_T *fp)
{
if (fp->uf_scoped == current_funccal)
/* no change */
return OK;
funccal_unref(fp->uf_scoped, fp, FALSE);
fp->uf_scoped = current_funccal;
current_funccal->fc_refcount++;
if (ga_grow(¤t_funccal->fc_funcs, 1) == FAIL)
return FAIL;
((ufunc_T **)current_funccal->fc_funcs.ga_data)
[current_funccal->fc_funcs.ga_len++] = fp;
return OK;
}
/*
* Parse a lambda expression and get a Funcref from "*arg".
* Return OK or FAIL. Returns NOTDONE for dict or {expr}.
*/
int
get_lambda_tv(char_u **arg, typval_T *rettv, int evaluate)
{
garray_T newargs;
garray_T newlines;
garray_T *pnewargs;
ufunc_T *fp = NULL;
int varargs;
int ret;
char_u *start = skipwhite(*arg + 1);
char_u *s, *e;
static int lambda_no = 0;
int *old_eval_lavars = eval_lavars_used;
int eval_lavars = FALSE;
ga_init(&newargs);
ga_init(&newlines);
/* First, check if this is a lambda expression. "->" must exist. */
ret = get_function_args(&start, '-', NULL, NULL, TRUE);
if (ret == FAIL || *start != '>')
return NOTDONE;
/* Parse the arguments again. */
if (evaluate)
pnewargs = &newargs;
else
pnewargs = NULL;
*arg = skipwhite(*arg + 1);
ret = get_function_args(arg, '-', pnewargs, &varargs, FALSE);
if (ret == FAIL || **arg != '>')
goto errret;
/* Set up a flag for checking local variables and arguments. */
if (evaluate)
eval_lavars_used = &eval_lavars;
/* Get the start and the end of the expression. */
*arg = skipwhite(*arg + 1);
s = *arg;
ret = skip_expr(arg);
if (ret == FAIL)
goto errret;
e = *arg;
*arg = skipwhite(*arg);
if (**arg != '}')
goto errret;
++*arg;
if (evaluate)
{
int len, flags = 0;
char_u *p;
char_u name[20];
partial_T *pt;
sprintf((char*)name, "<lambda>%d", ++lambda_no);
fp = (ufunc_T *)alloc_clear((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
if (fp == NULL)
goto errret;
pt = (partial_T *)alloc_clear((unsigned)sizeof(partial_T));
if (pt == NULL)
{
vim_free(fp);
goto errret;
}
ga_init2(&newlines, (int)sizeof(char_u *), 1);
if (ga_grow(&newlines, 1) == FAIL)
goto errret;
/* Add "return " before the expression. */
len = 7 + e - s + 1;
p = (char_u *)alloc(len);
if (p == NULL)
goto errret;
((char_u **)(newlines.ga_data))[newlines.ga_len++] = p;
STRCPY(p, "return ");
vim_strncpy(p + 7, s, e - s);
fp->uf_refcount = 1;
STRCPY(fp->uf_name, name);
hash_add(&func_hashtab, UF2HIKEY(fp));
fp->uf_args = newargs;
fp->uf_lines = newlines;
if (current_funccal != NULL && eval_lavars)
{
flags |= FC_CLOSURE;
if (register_closure(fp) == FAIL)
goto errret;
}
else
fp->uf_scoped = NULL;
#ifdef FEAT_PROFILE
fp->uf_tml_count = NULL;
fp->uf_tml_total = NULL;
fp->uf_tml_self = NULL;
fp->uf_profiling = FALSE;
if (prof_def_func())
func_do_profile(fp);
#endif
fp->uf_varargs = TRUE;
fp->uf_flags = flags;
fp->uf_calls = 0;
fp->uf_script_ID = current_SID;
pt->pt_func = fp;
pt->pt_refcount = 1;
rettv->vval.v_partial = pt;
rettv->v_type = VAR_PARTIAL;
}
eval_lavars_used = old_eval_lavars;
return OK;
errret:
ga_clear_strings(&newargs);
ga_clear_strings(&newlines);
vim_free(fp);
eval_lavars_used = old_eval_lavars;
return FAIL;
}
/*
* Check if "name" is a variable of type VAR_FUNC. If so, return the function
* name it contains, otherwise return "name".
* If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
* "partialp".
*/
char_u *
deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
{
dictitem_T *v;
int cc;
char_u *s;
if (partialp != NULL)
*partialp = NULL;
cc = name[*lenp];
name[*lenp] = NUL;
v = find_var(name, NULL, no_autoload);
name[*lenp] = cc;
if (v != NULL && v->di_tv.v_type == VAR_FUNC)
{
if (v->di_tv.vval.v_string == NULL)
{
*lenp = 0;
return (char_u *)""; /* just in case */
}
s = v->di_tv.vval.v_string;
*lenp = (int)STRLEN(s);
return s;
}
if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
{
partial_T *pt = v->di_tv.vval.v_partial;
if (pt == NULL)
{
*lenp = 0;
return (char_u *)""; /* just in case */
}
if (partialp != NULL)
*partialp = pt;
s = partial_name(pt);
*lenp = (int)STRLEN(s);
return s;
}
return name;
}
/*
* Give an error message with a function name. Handle <SNR> things.
* "ermsg" is to be passed without translation, use N_() instead of _().
*/
static void
emsg_funcname(char *ermsg, char_u *name)
{
char_u *p;
if (*name == K_SPECIAL)
p = concat_str((char_u *)"<SNR>", name + 3);
else
p = name;
EMSG2(_(ermsg), p);
if (p != name)
vim_free(p);
}
/*
* Allocate a variable for the result of a function.
* Return OK or FAIL.
*/
int
get_func_tv(
char_u *name, /* name of the function */
int len, /* length of "name" */
typval_T *rettv,
char_u **arg, /* argument, pointing to the '(' */
linenr_T firstline, /* first line of range */
linenr_T lastline, /* last line of range */
int *doesrange, /* return: function handled range */
int evaluate,
partial_T *partial, /* for extra arguments */
dict_T *selfdict) /* Dictionary for "self" */
{
char_u *argp;
int ret = OK;
typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
int argcount = 0; /* number of arguments found */
/*
* Get the arguments.
*/
argp = *arg;
while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
{
argp = skipwhite(argp + 1); /* skip the '(' or ',' */
if (*argp == ')' || *argp == ',' || *argp == NUL)
break;
if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
{
ret = FAIL;
break;
}
++argcount;
if (*argp != ',')
break;
}
if (*argp == ')')
++argp;
else
ret = FAIL;
if (ret == OK)
{
int i = 0;
if (get_vim_var_nr(VV_TESTING))
{
/* Prepare for calling test_garbagecollect_now(), need to know
* what variables are used on the call stack. */
if (funcargs.ga_itemsize == 0)
ga_init2(&funcargs, (int)sizeof(typval_T *), 50);
for (i = 0; i < argcount; ++i)
if (ga_grow(&funcargs, 1) == OK)
((typval_T **)funcargs.ga_data)[funcargs.ga_len++] =
&argvars[i];
}
ret = call_func(name, len, rettv, argcount, argvars, NULL,
firstline, lastline, doesrange, evaluate, partial, selfdict);
funcargs.ga_len -= i;
}
else if (!aborting())
{
if (argcount == MAX_FUNC_ARGS)
emsg_funcname(N_("E740: Too many arguments for function %s"), name);
else
emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
}
while (--argcount >= 0)
clear_tv(&argvars[argcount]);
*arg = skipwhite(argp);
return ret;
}
#define FLEN_FIXED 40
/*
* Return TRUE if "p" starts with "<SID>" or "s:".
* Only works if eval_fname_script() returned non-zero for "p"!
*/
static int
eval_fname_sid(char_u *p)
{
return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
}
/*
* In a script change <SID>name() and s:name() to K_SNR 123_name().
* Change <SNR>123_name() to K_SNR 123_name().
* Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
* (slow).
*/
static char_u *
fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
{
int llen;
char_u *fname;
int i;
llen = eval_fname_script(name);
if (llen > 0)
{
fname_buf[0] = K_SPECIAL;
fname_buf[1] = KS_EXTRA;
fname_buf[2] = (int)KE_SNR;
i = 3;
if (eval_fname_sid(name)) /* "<SID>" or "s:" */
{
if (current_SID <= 0)
*error = ERROR_SCRIPT;
else
{
sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
i = (int)STRLEN(fname_buf);
}
}
if (i + STRLEN(name + llen) < FLEN_FIXED)
{
STRCPY(fname_buf + i, name + llen);
fname = fname_buf;
}
else
{
fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
if (fname == NULL)
*error = ERROR_OTHER;
else
{
*tofree = fname;
mch_memmove(fname, fname_buf, (size_t)i);
STRCPY(fname + i, name + llen);
}
}
}
else
fname = name;
return fname;
}
/*
* Find a function by name, return pointer to it in ufuncs.
* Return NULL for unknown function.
*/
ufunc_T *
find_func(char_u *name)
{
hashitem_T *hi;
hi = hash_find(&func_hashtab, name);
if (!HASHITEM_EMPTY(hi))
return HI2UF(hi);
return NULL;
}
/*
* Copy the function name of "fp" to buffer "buf".
* "buf" must be able to hold the function name plus three bytes.
* Takes care of script-local function names.
*/
static void
cat_func_name(char_u *buf, ufunc_T *fp)
{
if (fp->uf_name[0] == K_SPECIAL)
{
STRCPY(buf, "<SNR>");
STRCAT(buf, fp->uf_name + 3);
}
else
STRCPY(buf, fp->uf_name);
}
/*
* Add a number variable "name" to dict "dp" with value "nr".
*/
static void
add_nr_var(
dict_T *dp,
dictitem_T *v,
char *name,
varnumber_T nr)
{
STRCPY(v->di_key, name);
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
hash_add(&dp->dv_hashtab, DI2HIKEY(v));
v->di_tv.v_type = VAR_NUMBER;
v->di_tv.v_lock = VAR_FIXED;
v->di_tv.vval.v_number = nr;
}
/*
* Free "fc" and what it contains.
*/
static void
free_funccal(
funccall_T *fc,
int free_val) /* a: vars were allocated */
{
listitem_T *li;
int i;
for (i = 0; i < fc->fc_funcs.ga_len; ++i)
{
ufunc_T *fp = ((ufunc_T **)(fc->fc_funcs.ga_data))[i];
/* When garbage collecting a funccall_T may be freed before the
* function that references it, clear its uf_scoped field.
* The function may have been redefined and point to another
* funccall_T, don't clear it then. */
if (fp != NULL && fp->uf_scoped == fc)
fp->uf_scoped = NULL;
}
ga_clear(&fc->fc_funcs);
/* The a: variables typevals may not have been allocated, only free the
* allocated variables. */
vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
/* free all l: variables */
vars_clear(&fc->l_vars.dv_hashtab);
/* Free the a:000 variables if they were allocated. */
if (free_val)
for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
clear_tv(&li->li_tv);
func_ptr_unref(fc->func);
vim_free(fc);
}
/*
* Handle the last part of returning from a function: free the local hashtable.
* Unless it is still in use by a closure.
*/
static void
cleanup_function_call(funccall_T *fc)
{
current_funccal = fc->caller;
/* If the a:000 list and the l: and a: dicts are not referenced and there
* is no closure using it, we can free the funccall_T and what's in it. */
if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
&& fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
&& fc->l_avars.dv_refcount == DO_NOT_FREE_CNT
&& fc->fc_refcount <= 0)
{
free_funccal(fc, FALSE);
}
else
{
hashitem_T *hi;
listitem_T *li;
int todo;
dictitem_T *v;
/* "fc" is still in use. This can happen when returning "a:000",
* assigning "l:" to a global variable or defining a closure.
* Link "fc" in the list for garbage collection later. */
fc->caller = previous_funccal;
previous_funccal = fc;
/* Make a copy of the a: variables, since we didn't do that above. */
todo = (int)fc->l_avars.dv_hashtab.ht_used;
for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
{
if (!HASHITEM_EMPTY(hi))
{
--todo;
v = HI2DI(hi);
copy_tv(&v->di_tv, &v->di_tv);
}
}
/* Make a copy of the a:000 items, since we didn't do that above. */
for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
copy_tv(&li->li_tv, &li->li_tv);
}
}
/*
* Call a user function.
*/
static void
call_user_func(
ufunc_T *fp, /* pointer to function */
int argcount, /* nr of args */
typval_T *argvars, /* arguments */
typval_T *rettv, /* return value */
linenr_T firstline, /* first line of range */
linenr_T lastline, /* last line of range */
dict_T *selfdict) /* Dictionary for "self" */
{
char_u *save_sourcing_name;
linenr_T save_sourcing_lnum;
scid_T save_current_SID;
funccall_T *fc;
int save_did_emsg;
static int depth = 0;
dictitem_T *v;
int fixvar_idx = 0; /* index in fixvar[] */
int i;
int ai;
int islambda = FALSE;
char_u numbuf[NUMBUFLEN];
char_u *name;
size_t len;
#ifdef FEAT_PROFILE
proftime_T wait_start;
proftime_T call_start;
#endif
/* If depth of calling is getting too high, don't execute the function */
if (depth >= p_mfd)
{
EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
rettv->v_type = VAR_NUMBER;
rettv->vval.v_number = -1;
return;
}
++depth;
line_breakcheck(); /* check for CTRL-C hit */
fc = (funccall_T *)alloc(sizeof(funccall_T));
fc->caller = current_funccal;
current_funccal = fc;
fc->func = fp;
fc->rettv = rettv;
rettv->vval.v_number = 0;
fc->linenr = 0;
fc->returned = FALSE;
fc->level = ex_nesting_level;
/* Check if this function has a breakpoint. */
fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
fc->dbg_tick = debug_tick;
/* Set up fields for closure. */
fc->fc_refcount = 0;
fc->fc_copyID = 0;
ga_init2(&fc->fc_funcs, sizeof(ufunc_T *), 1);
func_ptr_ref(fp);
if (STRNCMP(fp->uf_name, "<lambda>", 8) == 0)
islambda = TRUE;
/*
* Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
* with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
* each argument variable and saves a lot of time.
*/
/*
* Init l: variables.
*/
init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
if (selfdict != NULL)
{
/* Set l:self to "selfdict". Use "name" to avoid a warning from
* some compiler that checks the destination size. */
v = &fc->fixvar[fixvar_idx++].var;
name = v->di_key;
STRCPY(name, "self");
v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
v->di_tv.v_type = VAR_DICT;
v->di_tv.v_lock = 0;
v->di_tv.vval.v_dict = selfdict;
++selfdict->dv_refcount;
}
/*
* Init a: variables.
* Set a:0 to "argcount".
* Set a:000 to a list with room for the "..." arguments.
*/
init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
(varnumber_T)(argcount - fp->uf_args.ga_len));
/* Use "name" to avoid a warning from some compiler that checks the
* destination size. */
v = &fc->fixvar[fixvar_idx++].var;
name = v->di_key;
STRCPY(name, "000");
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
v->di_tv.v_type = VAR_LIST;
v->di_tv.v_lock = VAR_FIXED;
v->di_tv.vval.v_list = &fc->l_varlist;
vim_memset(&fc->l_varlist, 0, sizeof(list_T));
fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
fc->l_varlist.lv_lock = VAR_FIXED;
/*
* Set a:firstline to "firstline" and a:lastline to "lastline".
* Set a:name to named arguments.
* Set a:N to the "..." arguments.
*/
add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
(varnumber_T)firstline);
add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
(varnumber_T)lastline);
for (i = 0; i < argcount; ++i)
{
int addlocal = FALSE;
ai = i - fp->uf_args.ga_len;
if (ai < 0)
{
/* named argument a:name */
name = FUNCARG(fp, i);
if (islambda)
addlocal = TRUE;
}
else
{
/* "..." argument a:1, a:2, etc. */
sprintf((char *)numbuf, "%d", ai + 1);
name = numbuf;
}
if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
{
v = &fc->fixvar[fixvar_idx++].var;
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
}
else
{
v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
+ STRLEN(name)));
if (v == NULL)
break;
v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
}
STRCPY(v->di_key, name);
/* Note: the values are copied directly to avoid alloc/free.
* "argvars" must have VAR_FIXED for v_lock. */
v->di_tv = argvars[i];
v->di_tv.v_lock = VAR_FIXED;
if (addlocal)
{
/* Named arguments should be accessed without the "a:" prefix in
* lambda expressions. Add to the l: dict. */
copy_tv(&v->di_tv, &v->di_tv);
hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
}
else
hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
if (ai >= 0 && ai < MAX_FUNC_ARGS)
{
list_append(&fc->l_varlist, &fc->l_listitems[ai]);
fc->l_listitems[ai].li_tv = argvars[i];
fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
}
}
/* Don't redraw while executing the function. */
++RedrawingDisabled;
save_sourcing_name = sourcing_name;
save_sourcing_lnum = sourcing_lnum;
sourcing_lnum = 1;
/* need space for function name + ("function " + 3) or "[number]" */
len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
+ STRLEN(fp->uf_name) + 20;
sourcing_name = alloc((unsigned)len);
if (sourcing_name != NULL)
{
if (save_sourcing_name != NULL
&& STRNCMP(save_sourcing_name, "function ", 9) == 0)
sprintf((char *)sourcing_name, "%s[%d]..",
save_sourcing_name, (int)save_sourcing_lnum);
else
STRCPY(sourcing_name, "function ");
cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
if (p_verbose >= 12)
{
++no_wait_return;
verbose_enter_scroll();
smsg((char_u *)_("calling %s"), sourcing_name);
if (p_verbose >= 14)
{
char_u buf[MSG_BUF_LEN];
char_u numbuf2[NUMBUFLEN];
char_u *tofree;
char_u *s;
msg_puts((char_u *)"(");
for (i = 0; i < argcount; ++i)
{
if (i > 0)
msg_puts((char_u *)", ");
if (argvars[i].v_type == VAR_NUMBER)
msg_outnum((long)argvars[i].vval.v_number);
else
{
/* Do not want errors such as E724 here. */
++emsg_off;
s = tv2string(&argvars[i], &tofree, numbuf2, 0);
--emsg_off;
if (s != NULL)
{
if (vim_strsize(s) > MSG_BUF_CLEN)
{
trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
s = buf;
}
msg_puts(s);
vim_free(tofree);
}
}
}
msg_puts((char_u *)")");
}
msg_puts((char_u *)"\n"); /* don't overwrite this either */
verbose_leave_scroll();
--no_wait_return;
}
}
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES)
{
if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
func_do_profile(fp);
if (fp->uf_profiling
|| (fc->caller != NULL && fc->caller->func->uf_profiling))
{
++fp->uf_tm_count;
profile_start(&call_start);
profile_zero(&fp->uf_tm_children);
}
script_prof_save(&wait_start);
}
#endif
save_current_SID = current_SID;
current_SID = fp->uf_script_ID;
save_did_emsg = did_emsg;
did_emsg = FALSE;
/* call do_cmdline() to execute the lines */
do_cmdline(NULL, get_func_line, (void *)fc,
DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
--RedrawingDisabled;
/* when the function was aborted because of an error, return -1 */
if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
{
clear_tv(rettv);
rettv->v_type = VAR_NUMBER;
rettv->vval.v_number = -1;
}
#ifdef FEAT_PROFILE
if (do_profiling == PROF_YES && (fp->uf_profiling
|| (fc->caller != NULL && fc->caller->func->uf_profiling)))
{
profile_end(&call_start);
profile_sub_wait(&wait_start, &call_start);
profile_add(&fp->uf_tm_total, &call_start);
profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
if (fc->caller != NULL && fc->caller->func->uf_profiling)
{
profile_add(&fc->caller->func->uf_tm_children, &call_start);
profile_add(&fc->caller->func->uf_tml_children, &call_start);
}
}
#endif
/* when being verbose, mention the return value */
if (p_verbose >= 12)
{
++no_wait_return;
verbose_enter_scroll();
if (aborting())
smsg((char_u *)_("%s aborted"), sourcing_name);
else if (fc->rettv->v_type == VAR_NUMBER)
smsg((char_u *)_("%s returning #%ld"), sourcing_name,
(long)fc->rettv->vval.v_number);
else
{
char_u buf[MSG_BUF_LEN];
char_u numbuf2[NUMBUFLEN];
char_u *tofree;
char_u *s;
/* The value may be very long. Skip the middle part, so that we
* have some idea how it starts and ends. smsg() would always
* truncate it at the end. Don't want errors such as E724 here. */
++emsg_off;
s = tv2string(fc->rettv, &tofree, numbuf2, 0);
--emsg_off;
if (s != NULL)
{
if (vim_strsize(s) > MSG_BUF_CLEN)
{
trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
s = buf;