-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.c
7139 lines (6500 loc) · 161 KB
/
eval.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: Expression evaluation.
*/
#define USING_FLOAT_STUFF
#include "vim.h"
#if defined(FEAT_EVAL) || defined(PROTO)
#ifdef VMS
# include <float.h>
#endif
#define NAMESPACE_CHAR (char_u *)"abglstvw"
/*
* When recursively copying lists and dicts we need to remember which ones we
* have done to avoid endless recursiveness. This unique ID is used for that.
* The last bit is used for previous_funccal, ignored when comparing.
*/
static int current_copyID = 0;
static int eval2(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval3(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval4(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval5(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval6(char_u **arg, typval_T *rettv, evalarg_T *evalarg);
static int eval7(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
static int eval8(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
static int eval9(char_u **arg, typval_T *rettv, evalarg_T *evalarg, int want_string);
static int eval9_leader(typval_T *rettv, int numeric_only, char_u *start_leader, char_u **end_leaderp);
static int free_unref_items(int copyID);
static char_u *make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
/*
* Return "n1" divided by "n2", taking care of dividing by zero.
* If "failed" is not NULL set it to TRUE when dividing by zero fails.
*/
varnumber_T
num_divide(varnumber_T n1, varnumber_T n2, int *failed)
{
varnumber_T result;
if (n2 == 0)
{
if (in_vim9script())
{
emsg(_(e_divide_by_zero));
if (failed != NULL)
*failed = TRUE;
}
if (n1 == 0)
result = VARNUM_MIN; // similar to NaN
else if (n1 < 0)
result = -VARNUM_MAX;
else
result = VARNUM_MAX;
}
else
result = n1 / n2;
return result;
}
/*
* Return "n1" modulus "n2", taking care of dividing by zero.
* If "failed" is not NULL set it to TRUE when dividing by zero fails.
*/
varnumber_T
num_modulus(varnumber_T n1, varnumber_T n2, int *failed)
{
if (n2 == 0 && in_vim9script())
{
emsg(_(e_divide_by_zero));
if (failed != NULL)
*failed = TRUE;
}
return (n2 == 0) ? 0 : (n1 % n2);
}
/*
* Initialize the global and v: variables.
*/
void
eval_init(void)
{
evalvars_init();
func_init();
}
#if defined(EXITFREE) || defined(PROTO)
void
eval_clear(void)
{
evalvars_clear();
free_scriptnames(); // must come after evalvars_clear().
free_locales();
// autoloaded script names
free_autoload_scriptnames();
// unreferenced lists and dicts
(void)garbage_collect(FALSE);
// functions not garbage collected
free_all_functions();
}
#endif
void
fill_evalarg_from_eap(evalarg_T *evalarg, exarg_T *eap, int skip)
{
init_evalarg(evalarg);
evalarg->eval_flags = skip ? 0 : EVAL_EVALUATE;
if (eap != NULL)
{
evalarg->eval_cstack = eap->cstack;
if (sourcing_a_script(eap) || eap->getline == get_list_line)
{
evalarg->eval_getline = eap->getline;
evalarg->eval_cookie = eap->cookie;
}
}
}
/*
* Top level evaluation function, returning a boolean.
* Sets "error" to TRUE if there was an error.
* Return TRUE or FALSE.
*/
int
eval_to_bool(
char_u *arg,
int *error,
exarg_T *eap,
int skip, // only parse, don't execute
int use_simple_function)
{
typval_T tv;
varnumber_T retval = FALSE;
evalarg_T evalarg;
int r;
fill_evalarg_from_eap(&evalarg, eap, skip);
if (skip)
++emsg_skip;
if (use_simple_function)
r = eval0_simple_funccal(arg, &tv, eap, &evalarg);
else
r = eval0(arg, &tv, eap, &evalarg);
if (r == FAIL)
*error = TRUE;
else
{
*error = FALSE;
if (!skip)
{
if (in_vim9script())
retval = tv_get_bool_chk(&tv, error);
else
retval = (tv_get_number_chk(&tv, error) != 0);
clear_tv(&tv);
}
}
if (skip)
--emsg_skip;
clear_evalarg(&evalarg, eap);
return (int)retval;
}
/*
* Call eval1() and give an error message if not done at a lower level.
*/
static int
eval1_emsg(char_u **arg, typval_T *rettv, exarg_T *eap)
{
char_u *start = *arg;
int ret;
int did_emsg_before = did_emsg;
int called_emsg_before = called_emsg;
evalarg_T evalarg;
fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
ret = eval1(arg, rettv, &evalarg);
if (ret == FAIL)
{
// Report the invalid expression unless the expression evaluation has
// been cancelled due to an aborting error, an interrupt, or an
// exception, or we already gave a more specific error.
// Also check called_emsg for when using assert_fails().
if (!aborting() && did_emsg == did_emsg_before
&& called_emsg == called_emsg_before)
semsg(_(e_invalid_expression_str), start);
}
clear_evalarg(&evalarg, eap);
return ret;
}
/*
* Return whether a typval is a valid expression to pass to eval_expr_typval()
* or eval_expr_to_bool(). An empty string returns FALSE;
*/
int
eval_expr_valid_arg(typval_T *tv)
{
return tv->v_type != VAR_UNKNOWN
&& (tv->v_type != VAR_STRING
|| (tv->vval.v_string != NULL && *tv->vval.v_string != NUL));
}
/*
* When calling eval_expr_typval() many times we only need one funccall_T.
* Returns NULL when no funccall_T is to be used.
* When returning non-NULL remove_funccal() must be called later.
*/
funccall_T *
eval_expr_get_funccal(typval_T *expr, typval_T *rettv)
{
if (expr->v_type != VAR_PARTIAL)
return NULL;
partial_T *partial = expr->vval.v_partial;
if (partial == NULL)
return NULL;
if (partial->pt_func == NULL
|| partial->pt_func->uf_def_status == UF_NOT_COMPILED)
return NULL;
return create_funccal(partial->pt_func, rettv);
}
/*
* Evaluate an expression, which can be a function, partial or string.
* Pass arguments "argv[argc]".
* "fc_arg" is from eval_expr_get_funccal() or NULL;
* Return the result in "rettv" and OK or FAIL.
*/
int
eval_expr_typval(
typval_T *expr,
typval_T *argv,
int argc,
funccall_T *fc_arg,
typval_T *rettv)
{
char_u *s;
char_u buf[NUMBUFLEN];
funcexe_T funcexe;
if (expr->v_type == VAR_FUNC)
{
s = expr->vval.v_string;
if (s == NULL || *s == NUL)
return FAIL;
CLEAR_FIELD(funcexe);
funcexe.fe_evaluate = TRUE;
if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
return FAIL;
}
else if (expr->v_type == VAR_PARTIAL)
{
partial_T *partial = expr->vval.v_partial;
if (partial == NULL)
return FAIL;
if (partial->pt_func != NULL
&& partial->pt_func->uf_def_status != UF_NOT_COMPILED)
{
funccall_T *fc = fc_arg != NULL ? fc_arg
: create_funccal(partial->pt_func, rettv);
int r;
if (fc == NULL)
return FAIL;
// Shortcut to call a compiled function with minimal overhead.
r = call_def_function(partial->pt_func, argc, argv,
DEF_USE_PT_ARGV, partial, fc, rettv);
if (fc_arg == NULL)
remove_funccal();
if (r == FAIL)
return FAIL;
}
else
{
s = partial_name(partial);
if (s == NULL || *s == NUL)
return FAIL;
CLEAR_FIELD(funcexe);
funcexe.fe_evaluate = TRUE;
funcexe.fe_partial = partial;
if (call_func(s, -1, rettv, argc, argv, &funcexe) == FAIL)
return FAIL;
}
}
else if (expr->v_type == VAR_INSTR)
{
return exe_typval_instr(expr, rettv);
}
else
{
s = tv_get_string_buf_chk_strict(expr, buf, in_vim9script());
if (s == NULL)
return FAIL;
s = skipwhite(s);
if (eval1_emsg(&s, rettv, NULL) == FAIL)
return FAIL;
if (*skipwhite(s) != NUL) // check for trailing chars after expr
{
clear_tv(rettv);
semsg(_(e_invalid_expression_str), s);
return FAIL;
}
}
return OK;
}
/*
* Like eval_to_bool() but using a typval_T instead of a string.
* Works for string, funcref and partial.
*/
int
eval_expr_to_bool(typval_T *expr, int *error)
{
typval_T rettv;
int res;
if (eval_expr_typval(expr, NULL, 0, NULL, &rettv) == FAIL)
{
*error = TRUE;
return FALSE;
}
res = (tv_get_bool_chk(&rettv, error) != 0);
clear_tv(&rettv);
return res;
}
/*
* Top level evaluation function, returning a string. If "skip" is TRUE,
* only parsing to "nextcmd" is done, without reporting errors. Return
* pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
*/
char_u *
eval_to_string_skip(
char_u *arg,
exarg_T *eap,
int skip) // only parse, don't execute
{
typval_T tv;
char_u *retval;
evalarg_T evalarg;
fill_evalarg_from_eap(&evalarg, eap, skip);
if (skip)
++emsg_skip;
if (eval0(arg, &tv, eap, &evalarg) == FAIL || skip)
retval = NULL;
else
{
retval = vim_strsave(tv_get_string(&tv));
clear_tv(&tv);
}
if (skip)
--emsg_skip;
clear_evalarg(&evalarg, eap);
return retval;
}
/*
* Initialize "evalarg" for use.
*/
void
init_evalarg(evalarg_T *evalarg)
{
CLEAR_POINTER(evalarg);
ga_init2(&evalarg->eval_tofree_ga, sizeof(char_u *), 20);
}
/*
* If "evalarg->eval_tofree" is not NULL free it later.
* Caller is expected to overwrite "evalarg->eval_tofree" next.
*/
static void
free_eval_tofree_later(evalarg_T *evalarg)
{
if (evalarg->eval_tofree != NULL)
{
if (ga_grow(&evalarg->eval_tofree_ga, 1) == OK)
((char_u **)evalarg->eval_tofree_ga.ga_data)
[evalarg->eval_tofree_ga.ga_len++]
= evalarg->eval_tofree;
else
vim_free(evalarg->eval_tofree);
}
}
/*
* After using "evalarg" filled from "eap": free the memory.
*/
void
clear_evalarg(evalarg_T *evalarg, exarg_T *eap)
{
if (evalarg != NULL)
{
garray_T *etga = &evalarg->eval_tofree_ga;
if (evalarg->eval_tofree != NULL || evalarg->eval_using_cmdline)
{
if (eap != NULL)
{
// We may need to keep the original command line, e.g. for
// ":let" it has the variable names. But we may also need
// the new one, "nextcmd" points into it. Keep both.
vim_free(eap->cmdline_tofree);
eap->cmdline_tofree = *eap->cmdlinep;
if (evalarg->eval_using_cmdline && etga->ga_len > 0)
{
// "nextcmd" points into the last line in eval_tofree_ga,
// need to keep it around.
--etga->ga_len;
*eap->cmdlinep = ((char_u **)etga->ga_data)[etga->ga_len];
vim_free(evalarg->eval_tofree);
}
else
*eap->cmdlinep = evalarg->eval_tofree;
}
else
vim_free(evalarg->eval_tofree);
evalarg->eval_tofree = NULL;
}
ga_clear_strings(etga);
VIM_CLEAR(evalarg->eval_tofree_lambda);
}
}
/*
* Skip over an expression at "*pp".
* Return FAIL for an error, OK otherwise.
*/
int
skip_expr(char_u **pp, evalarg_T *evalarg)
{
typval_T rettv;
*pp = skipwhite(*pp);
return eval1(pp, &rettv, evalarg);
}
/*
* Skip over an expression at "*arg".
* If in Vim9 script and line breaks are encountered, the lines are
* concatenated. "evalarg->eval_tofree" will be set accordingly.
* "arg" is advanced to just after the expression.
* "start" is set to the start of the expression, "end" to just after the end.
* Also when the expression is copied to allocated memory.
* Return FAIL for an error, OK otherwise.
*/
int
skip_expr_concatenate(
char_u **arg,
char_u **start,
char_u **end,
evalarg_T *evalarg)
{
typval_T rettv;
int res;
int vim9script = in_vim9script();
garray_T *gap = evalarg == NULL ? NULL : &evalarg->eval_ga;
garray_T *freegap = evalarg == NULL ? NULL : &evalarg->eval_freega;
int save_flags = evalarg == NULL ? 0 : evalarg->eval_flags;
int evaluate = evalarg == NULL
? FALSE : (evalarg->eval_flags & EVAL_EVALUATE);
if (vim9script && evaluate
&& (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
{
ga_init2(gap, sizeof(char_u *), 10);
// leave room for "start"
if (ga_grow(gap, 1) == OK)
++gap->ga_len;
ga_init2(freegap, sizeof(char_u *), 10);
}
*start = *arg;
// Don't evaluate the expression.
if (evalarg != NULL)
evalarg->eval_flags &= ~EVAL_EVALUATE;
*arg = skipwhite(*arg);
res = eval1(arg, &rettv, evalarg);
*end = *arg;
if (evalarg != NULL)
evalarg->eval_flags = save_flags;
if (vim9script && evaluate
&& (evalarg->eval_cookie != NULL || evalarg->eval_cctx != NULL))
{
if (evalarg->eval_ga.ga_len == 1)
{
// just the one line, no need to concatenate
ga_clear(gap);
gap->ga_itemsize = 0;
}
else
{
char_u *p;
size_t endoff = STRLEN(*arg);
// Line breaks encountered, concatenate all the lines.
*((char_u **)gap->ga_data) = *start;
p = ga_concat_strings(gap, " ");
// free the lines only when using getsourceline()
if (evalarg->eval_cookie != NULL)
{
// Do not free the first line, the caller can still use it.
*((char_u **)gap->ga_data) = NULL;
// Do not free the last line, "arg" points into it, free it
// later. Also free "eval_tofree" later if needed.
free_eval_tofree_later(evalarg);
evalarg->eval_tofree =
((char_u **)gap->ga_data)[gap->ga_len - 1];
((char_u **)gap->ga_data)[gap->ga_len - 1] = NULL;
ga_clear_strings(gap);
}
else
{
ga_clear(gap);
// free lines that were explicitly marked for freeing
ga_clear_strings(freegap);
}
gap->ga_itemsize = 0;
if (p == NULL)
return FAIL;
*start = p;
vim_free(evalarg->eval_tofree_lambda);
evalarg->eval_tofree_lambda = p;
// Compute "end" relative to the end.
*end = *start + STRLEN(*start) - endoff;
}
}
return res;
}
/*
* Convert "tv" to a string.
* When "convert" is TRUE convert a List into a sequence of lines and convert
* a Float to a String.
* Returns an allocated string (NULL when out of memory).
*/
char_u *
typval2string(typval_T *tv, int convert)
{
garray_T ga;
char_u *retval;
char_u numbuf[NUMBUFLEN];
if (convert && tv->v_type == VAR_LIST)
{
ga_init2(&ga, sizeof(char), 80);
if (tv->vval.v_list != NULL)
{
list_join(&ga, tv->vval.v_list, (char_u *)"\n", TRUE, FALSE, 0);
if (tv->vval.v_list->lv_len > 0)
ga_append(&ga, NL);
}
ga_append(&ga, NUL);
retval = (char_u *)ga.ga_data;
}
else if (convert && tv->v_type == VAR_FLOAT)
{
vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
retval = vim_strsave(numbuf);
}
else
retval = vim_strsave(tv_get_string(tv));
return retval;
}
/*
* Top level evaluation function, returning a string. Does not handle line
* breaks.
* When "convert" is TRUE convert a List into a sequence of lines and convert
* a Float to a String.
* Return pointer to allocated memory, or NULL for failure.
*/
char_u *
eval_to_string_eap(
char_u *arg,
int convert,
exarg_T *eap,
int use_simple_function)
{
typval_T tv;
char_u *retval;
evalarg_T evalarg;
int r;
fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
if (use_simple_function)
r = eval0_simple_funccal(arg, &tv, NULL, &evalarg);
else
r = eval0(arg, &tv, NULL, &evalarg);
if (r == FAIL)
retval = NULL;
else
{
retval = typval2string(&tv, convert);
clear_tv(&tv);
}
clear_evalarg(&evalarg, NULL);
return retval;
}
char_u *
eval_to_string(
char_u *arg,
int convert,
int use_simple_function)
{
return eval_to_string_eap(arg, convert, NULL, use_simple_function);
}
/*
* Call eval_to_string() without using current local variables and using
* textlock. When "use_sandbox" is TRUE use the sandbox.
* Use legacy Vim script syntax.
*/
char_u *
eval_to_string_safe(
char_u *arg,
int use_sandbox,
int keep_script_version,
int use_simple_function)
{
char_u *retval;
funccal_entry_T funccal_entry;
int save_sc_version = current_sctx.sc_version;
int save_garbage = may_garbage_collect;
if (!keep_script_version)
current_sctx.sc_version = 1;
save_funccal(&funccal_entry);
if (use_sandbox)
++sandbox;
++textlock;
may_garbage_collect = FALSE;
retval = eval_to_string(arg, FALSE, use_simple_function);
if (use_sandbox)
--sandbox;
--textlock;
may_garbage_collect = save_garbage;
restore_funccal();
current_sctx.sc_version = save_sc_version;
return retval;
}
/*
* Top level evaluation function, returning a number.
* Evaluates "expr" silently.
* Returns -1 for an error.
*/
varnumber_T
eval_to_number(char_u *expr, int use_simple_function)
{
typval_T rettv;
varnumber_T retval;
char_u *p = skipwhite(expr);
int r = NOTDONE;
++emsg_off;
if (use_simple_function)
r = may_call_simple_func(expr, &rettv);
if (r == NOTDONE)
r = eval1(&p, &rettv, &EVALARG_EVALUATE);
if (r == FAIL)
retval = -1;
else
{
retval = tv_get_number_chk(&rettv, NULL);
clear_tv(&rettv);
}
--emsg_off;
return retval;
}
/*
* Top level evaluation function.
* Returns an allocated typval_T with the result.
* Returns NULL when there is an error.
*/
typval_T *
eval_expr(char_u *arg, exarg_T *eap)
{
return eval_expr_ext(arg, eap, FALSE);
}
typval_T *
eval_expr_ext(char_u *arg, exarg_T *eap, int use_simple_function)
{
typval_T *tv;
evalarg_T evalarg;
fill_evalarg_from_eap(&evalarg, eap, eap != NULL && eap->skip);
tv = ALLOC_ONE(typval_T);
if (tv != NULL)
{
int r = NOTDONE;
if (use_simple_function)
r = eval0_simple_funccal(arg, tv, eap, &evalarg);
if (r == NOTDONE)
r = eval0(arg, tv, eap, &evalarg);
if (r == FAIL)
VIM_CLEAR(tv);
}
clear_evalarg(&evalarg, eap);
return tv;
}
/*
* "*arg" points to what can be a function name in the form of "import.Name" or
* "Funcref". Return the name of the function. Set "tofree" to something that
* was allocated.
* If "verbose" is FALSE no errors are given.
* Return NULL for any failure.
*/
static char_u *
deref_function_name(
char_u **arg,
char_u **tofree,
evalarg_T *evalarg,
int verbose)
{
typval_T ref;
char_u *name = *arg;
int save_flags = 0;
ref.v_type = VAR_UNKNOWN;
if (evalarg != NULL)
{
// need to evaluate this to get an import, like in "a.Func"
save_flags = evalarg->eval_flags;
evalarg->eval_flags |= EVAL_EVALUATE;
}
if (eval9(arg, &ref, evalarg, FALSE) == FAIL)
{
dictitem_T *v;
// If <SID>VarName was used it would not be found, try another way.
v = find_var_also_in_script(name, NULL, FALSE);
if (v == NULL)
{
name = NULL;
goto theend;
}
copy_tv(&v->di_tv, &ref);
}
if (*skipwhite(*arg) != NUL)
{
if (verbose)
semsg(_(e_trailing_characters_str), *arg);
name = NULL;
}
else if (ref.v_type == VAR_FUNC && ref.vval.v_string != NULL)
{
name = ref.vval.v_string;
ref.vval.v_string = NULL;
*tofree = name;
}
else if (ref.v_type == VAR_PARTIAL && ref.vval.v_partial != NULL)
{
if (ref.vval.v_partial->pt_argc > 0
|| ref.vval.v_partial->pt_dict != NULL)
{
if (verbose)
emsg(_(e_cannot_use_partial_here));
name = NULL;
}
else
{
name = vim_strsave(partial_name(ref.vval.v_partial));
*tofree = name;
}
}
else
{
if (verbose)
semsg(_(e_not_callable_type_str), name);
name = NULL;
}
theend:
clear_tv(&ref);
if (evalarg != NULL)
evalarg->eval_flags = save_flags;
return name;
}
/*
* Call some Vim script function and return the result in "*rettv".
* Uses argv[0] to argv[argc - 1] for the function arguments. argv[argc]
* should have type VAR_UNKNOWN.
* Returns OK or FAIL.
*/
int
call_vim_function(
char_u *func,
int argc,
typval_T *argv,
typval_T *rettv)
{
int ret;
funcexe_T funcexe;
char_u *arg;
char_u *name;
char_u *tofree = NULL;
int ignore_errors;
rettv->v_type = VAR_UNKNOWN; // clear_tv() uses this
CLEAR_FIELD(funcexe);
funcexe.fe_firstline = curwin->w_cursor.lnum;
funcexe.fe_lastline = curwin->w_cursor.lnum;
funcexe.fe_evaluate = TRUE;
// The name might be "import.Func" or "Funcref". We don't know, we need to
// ignore errors for an undefined name. But we do want errors when an
// autoload script has errors. Guess that when there is a dot in the name
// showing errors is the right choice.
ignore_errors = vim_strchr(func, '.') == NULL;
arg = func;
if (ignore_errors)
++emsg_off;
name = deref_function_name(&arg, &tofree, &EVALARG_EVALUATE, FALSE);
if (ignore_errors)
--emsg_off;
if (name == NULL)
name = func;
ret = call_func(name, -1, rettv, argc, argv, &funcexe);
if (ret == FAIL)
clear_tv(rettv);
vim_free(tofree);
return ret;
}
/*
* Call Vim script function "func" and return the result as a string.
* Uses "argv[0]" to "argv[argc - 1]" for the function arguments. "argv[argc]"
* should have type VAR_UNKNOWN.
* Returns NULL when calling the function fails.
*/
void *
call_func_retstr(
char_u *func,
int argc,
typval_T *argv)
{
typval_T rettv;
char_u *retval;
if (call_vim_function(func, argc, argv, &rettv) == FAIL)
return NULL;
retval = vim_strsave(tv_get_string(&rettv));
clear_tv(&rettv);
return retval;
}
/*
* Call Vim script function "func" and return the result as a List.
* Uses "argv" and "argc" as call_func_retstr().
* Returns NULL when there is something wrong.
* Gives an error when the returned value is not a list.
*/
void *
call_func_retlist(
char_u *func,
int argc,
typval_T *argv)
{
typval_T rettv;
if (call_vim_function(func, argc, argv, &rettv) == FAIL)
return NULL;
if (rettv.v_type != VAR_LIST)
{
semsg(_(e_custom_list_completion_function_does_not_return_list_but_str),
vartype_name(rettv.v_type));
clear_tv(&rettv);
return NULL;
}
return rettv.vval.v_list;
}
#if defined(FEAT_FOLDING) || defined(PROTO)
/*
* Evaluate "arg", which is 'foldexpr'.
* Note: caller must set "curwin" to match "arg".
* Returns the foldlevel, and any character preceding it in "*cp". Doesn't
* give error messages.
*/
int
eval_foldexpr(win_T *wp, int *cp)
{
char_u *arg;
typval_T tv;
varnumber_T retval;
char_u *s;
sctx_T saved_sctx = current_sctx;
int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
OPT_LOCAL);
arg = skipwhite(wp->w_p_fde);
current_sctx = wp->w_p_script_ctx[WV_FDE];
++emsg_off;
if (use_sandbox)
++sandbox;
++textlock;
*cp = NUL;
// Evaluate the expression. If the expression is "FuncName()" call the
// function directly.
if (eval0_simple_funccal(arg, &tv, NULL, &EVALARG_EVALUATE) == FAIL)
retval = 0;
else
{
// If the result is a number, just return the number.
if (tv.v_type == VAR_NUMBER)
retval = tv.vval.v_number;
else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
retval = 0;
else
{
// If the result is a string, check if there is a non-digit before
// the number.
s = tv.vval.v_string;
if (!VIM_ISDIGIT(*s) && *s != '-')
*cp = *s++;
retval = atol((char *)s);
}
clear_tv(&tv);
}
--emsg_off;
if (use_sandbox)
--sandbox;
--textlock;
clear_evalarg(&EVALARG_EVALUATE, NULL);
current_sctx = saved_sctx;
return (int)retval;
}
#endif
/*
* Get an lval: variable, Dict item or List item that can be assigned a value
* to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
* "name.key", "name.key[expr]" etc.
* Indexing only works if "name" is an existing List or Dictionary.
* "name" points to the start of the name.
* If "rettv" is not NULL it points to the value to be assigned.
* "unlet" is TRUE for ":unlet": slightly different behavior when something is
* wrong; must end in space or cmd separator.
*
* flags:
* GLV_QUIET: do not give error messages
* GLV_READ_ONLY: will not change the variable
* GLV_NO_AUTOLOAD: do not use script autoloading
*
* Returns a pointer to just after the name, including indexes.