-
Notifications
You must be signed in to change notification settings - Fork 561
/
Copy pathgv.c
4437 lines (3941 loc) · 146 KB
/
gv.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
/* gv.c
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
* 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
/*
* 'Mercy!' cried Gandalf. 'If the giving of information is to be the cure
* of your inquisitiveness, I shall spend all the rest of my days in answering
* you. What more do you want to know?'
* 'The names of all the stars, and of all living things, and the whole
* history of Middle-earth and Over-heaven and of the Sundering Seas,'
* laughed Pippin.
*
* [p.599 of _The Lord of the Rings_, III/xi: "The Palantír"]
*/
/*
=head1 GV Handling and Stashes
A GV is a structure which corresponds to a Perl typeglob, I<i.e.>, *foo.
It is a structure that holds a pointer to a scalar, an array, a hash etc,
corresponding to $foo, @foo, %foo.
GVs are usually found as values in stashes (symbol table hashes) where
Perl stores its global variables.
A B<stash> is a hash that contains all variables that are defined
within a package. See L<perlguts/Stashes and Globs>
=for apidoc Ayh||GV
=cut
*/
#include "EXTERN.h"
#define PERL_IN_GV_C
#include "perl.h"
#include "overload.inc"
#include "keywords.h"
#include "feature.h"
static const char S_autoload[] = "AUTOLOAD";
#define S_autolen (sizeof("AUTOLOAD")-1)
/*
=for apidoc gv_add_by_type
Make sure there is a slot of type C<type> in the GV C<gv>.
=cut
*/
GV *
Perl_gv_add_by_type(pTHX_ GV *gv, svtype type)
{
SV **where;
if (
!gv
|| (
SvTYPE((const SV *)gv) != SVt_PVGV
&& SvTYPE((const SV *)gv) != SVt_PVLV
)
) {
const char *what;
if (type == SVt_PVIO) {
/*
* if it walks like a dirhandle, then let's assume that
* this is a dirhandle.
*/
what = OP_IS_DIRHOP(PL_op->op_type) ?
"dirhandle" : "filehandle";
} else if (type == SVt_PVHV) {
what = "hash";
} else {
what = type == SVt_PVAV ? "array" : "scalar";
}
Perl_croak(aTHX_ "Bad symbol for %s", what);
}
if (type == SVt_PVHV) {
where = (SV **)&GvHV(gv);
} else if (type == SVt_PVAV) {
where = (SV **)&GvAV(gv);
} else if (type == SVt_PVIO) {
where = (SV **)&GvIOp(gv);
} else {
where = &GvSV(gv);
}
if (!*where)
{
*where = newSV_type(type);
if ( type == SVt_PVAV
&& memEQs(GvNAME(gv), GvNAMELEN(gv), "ISA"))
{
sv_magic(*where, (SV *)gv, PERL_MAGIC_isa, NULL, 0);
}
}
return gv;
}
/*
=for apidoc gv_fetchfile
=for apidoc_item gv_fetchfile_flags
These return the debugger glob for the file (compiled by Perl) whose name is
given by the C<name> parameter.
There are currently exactly two differences between these functions.
The C<name> parameter to C<gv_fetchfile> is a C string, meaning it is
C<NUL>-terminated; whereas the C<name> parameter to C<gv_fetchfile_flags> is a
Perl string, whose length (in bytes) is passed in via the C<namelen> parameter
This means the name may contain embedded C<NUL> characters.
C<namelen> doesn't exist in plain C<gv_fetchfile>).
The other difference is that C<gv_fetchfile_flags> has an extra C<flags>
parameter, which is currently completely ignored, but allows for possible
future extensions.
=cut
*/
GV *
Perl_gv_fetchfile(pTHX_ const char *name)
{
PERL_ARGS_ASSERT_GV_FETCHFILE;
return gv_fetchfile_flags(name, strlen(name), 0);
}
GV *
Perl_gv_fetchfile_flags(pTHX_ const char *const name, const STRLEN namelen,
const U32 flags)
{
char smallbuf[128];
char *tmpbuf;
const STRLEN tmplen = namelen + 2;
GV *gv;
PERL_ARGS_ASSERT_GV_FETCHFILE_FLAGS;
PERL_UNUSED_ARG(flags);
if (!PL_defstash)
return NULL;
if (tmplen <= sizeof smallbuf)
tmpbuf = smallbuf;
else
Newx(tmpbuf, tmplen, char);
/* This is where the debugger's %{"::_<$filename"} hash is created */
tmpbuf[0] = '_';
tmpbuf[1] = '<';
memcpy(tmpbuf + 2, name, namelen);
GV **gvp = (GV**)hv_fetch(PL_defstash, tmpbuf, tmplen, (flags & GVF_NOADD) ? FALSE : TRUE);
if (gvp) {
gv = *gvp;
if (!isGV(gv)) {
gv_init(gv, PL_defstash, tmpbuf, tmplen, FALSE);
#ifdef PERL_DONT_CREATE_GVSV
GvSV(gv) = newSVpvn(name, namelen);
#else
sv_setpvn(GvSV(gv), name, namelen);
#endif
}
if (PERLDB_LINE_OR_SAVESRC && !GvAV(gv))
hv_magic(GvHVn(gv), GvAVn(gv), PERL_MAGIC_dbfile);
}
else {
gv = NULL;
}
if (tmpbuf != smallbuf)
Safefree(tmpbuf);
return gv;
}
/*
=for apidoc gv_const_sv
If C<gv> is a typeglob whose subroutine entry is a constant sub eligible for
inlining, or C<gv> is a placeholder reference that would be promoted to such
a typeglob, then returns the value returned by the sub. Otherwise, returns
C<NULL>.
=cut
*/
SV *
Perl_gv_const_sv(pTHX_ GV *gv)
{
PERL_ARGS_ASSERT_GV_CONST_SV;
PERL_UNUSED_CONTEXT;
if (SvTYPE(gv) == SVt_PVGV)
return cv_const_sv(GvCVu(gv));
return SvROK(gv) && SvTYPE(SvRV(gv)) != SVt_PVAV && SvTYPE(SvRV(gv)) != SVt_PVCV ? SvRV(gv) : NULL;
}
GP *
Perl_newGP(pTHX_ GV *const gv)
{
GP *gp;
U32 hash;
const char *file;
STRLEN len;
PERL_ARGS_ASSERT_NEWGP;
Newxz(gp, 1, GP);
gp->gp_egv = gv; /* allow compiler to reuse gv after this */
#ifndef PERL_DONT_CREATE_GVSV
gp->gp_sv = newSV_type(SVt_NULL);
#endif
/* PL_curcop may be null here. E.g.,
INIT { bless {} and exit }
frees INIT before looking up DESTROY (and creating *DESTROY)
*/
if (PL_curcop) {
char *tmp= CopFILE(PL_curcop);
gp->gp_line = CopLINE(PL_curcop); /* 0 otherwise Newxz */
if (tmp) {
file = tmp;
len = CopFILE_LEN(PL_curcop);
}
else goto no_file;
}
else {
no_file:
file = "";
len = 0;
}
PERL_HASH(hash, file, len);
gp->gp_file_hek = share_hek(file, len, hash);
gp->gp_refcnt = 1;
return gp;
}
/* Assign CvGV(cv) = gv, handling weak references.
* See also S_anonymise_cv_maybe */
void
Perl_cvgv_set(pTHX_ CV* cv, GV* gv)
{
GV * const oldgv = CvNAMED(cv) ? NULL : SvANY(cv)->xcv_gv_u.xcv_gv;
HEK *hek;
PERL_ARGS_ASSERT_CVGV_SET;
if (oldgv == gv)
return;
if (oldgv) {
if (CvCVGV_RC(cv)) {
SvREFCNT_dec_NN(oldgv);
CvCVGV_RC_off(cv);
}
else {
sv_del_backref(MUTABLE_SV(oldgv), MUTABLE_SV(cv));
}
}
else if ((hek = CvNAME_HEK(cv))) {
unshare_hek(hek);
CvLEXICAL_off(cv);
}
CvNAMED_off(cv);
SvANY(cv)->xcv_gv_u.xcv_gv = gv;
assert(!CvCVGV_RC(cv));
if (!gv)
return;
if (isGV_with_GP(gv) && GvGP(gv) && (GvCV(gv) == cv || GvFORM(gv) == cv))
Perl_sv_add_backref(aTHX_ MUTABLE_SV(gv), MUTABLE_SV(cv));
else {
CvCVGV_RC_on(cv);
SvREFCNT_inc_simple_void_NN(gv);
}
}
/* Convert CvSTASH + CvNAME_HEK into a GV. Conceptually, all subs have a
GV, but for efficiency that GV may not in fact exist. This function,
called by CvGV, reifies it. */
GV *
Perl_cvgv_from_hek(pTHX_ CV *cv)
{
GV *gv;
SV **svp;
PERL_ARGS_ASSERT_CVGV_FROM_HEK;
assert(SvTYPE(cv) == SVt_PVCV);
if (!CvSTASH(cv)) return NULL;
ASSUME(CvNAME_HEK(cv));
svp = hv_fetchhek(CvSTASH(cv), CvNAME_HEK(cv), 0);
gv = MUTABLE_GV(svp && *svp ? *svp : newSV_type(SVt_NULL));
if (!isGV(gv))
gv_init_pvn(gv, CvSTASH(cv), HEK_KEY(CvNAME_HEK(cv)),
HEK_LEN(CvNAME_HEK(cv)),
SVf_UTF8 * cBOOL(HEK_UTF8(CvNAME_HEK(cv))));
if (!CvNAMED(cv)) { /* gv_init took care of it */
assert (SvANY(cv)->xcv_gv_u.xcv_gv == gv);
return gv;
}
unshare_hek(CvNAME_HEK(cv));
CvNAMED_off(cv);
SvANY(cv)->xcv_gv_u.xcv_gv = gv;
if (svp && *svp) SvREFCNT_inc_simple_void_NN(gv);
CvCVGV_RC_on(cv);
return gv;
}
/* Assign CvSTASH(cv) = st, handling weak references. */
void
Perl_cvstash_set(pTHX_ CV *cv, HV *stash)
{
HV *oldstash = CvSTASH(cv);
PERL_ARGS_ASSERT_CVSTASH_SET;
if (oldstash == stash)
return;
if (oldstash)
sv_del_backref(MUTABLE_SV(oldstash), MUTABLE_SV(cv));
SvANY(cv)->xcv_stash = stash;
if (stash)
Perl_sv_add_backref(aTHX_ MUTABLE_SV(stash), MUTABLE_SV(cv));
}
/*
=for apidoc gv_init
=for apidoc_item gv_init_pv
=for apidoc_item gv_init_pvn
=for apidoc_item gv_init_sv
These each convert a scalar into a typeglob. This is an incoercible typeglob;
assigning a reference to it will assign to one of its slots, instead of
overwriting it as happens with typeglobs created by C<SvSetSV>. Converting
any scalar that is C<SvOK()> may produce unpredictable results and is reserved
for perl's internal use.
C<gv> is the scalar to be converted.
C<stash> is the parent stash/package, if any.
In C<gv_init> and C<gv_init_pvn>, C<name> and C<len> give the name. The name
must be unqualified; that is, it must not include the package name. If C<gv>
is a stash element, it is the caller's responsibility to ensure that the name
passed to this function matches the name of the element. If it does not match,
perl's internal bookkeeping will get out of sync. C<name> may contain embedded
NUL characters.
C<gv_init_pv> is identical to C<gv_init_pvn>, but takes a NUL-terminated string
for the name instead of separate char * and length parameters.
In C<gv_init_sv>, the name is given by C<sv>.
All but C<gv_init> take a C<flags> parameter. Set C<flags> to include
C<SVf_UTF8> if C<name> is a UTF-8 string. In C<gv_init_sv>, if C<SvUTF8(sv)>
is non-zero, name will be also be considered to be a UTF-8 string. It's
unlikely to be a good idea to pass this particular flag to C<gv_init_sv>, as
that would potentially override the (presumaby known) state of C<sv>.
C<flags> can also take the C<GV_ADDMULTI> flag, which means to pretend that the
GV has been seen before (i.e., suppress "Used once" warnings).
C<gv_init> is the old form of C<gv_init_pvn>. It does not work with UTF-8
strings, as it has no flags parameter. Setting the C<multi> parameter to
non-zero has the same effect as setting the C<GV_ADDMULTI> flag in the other
forms.
=for apidoc Amnh||GV_ADDMULTI
=cut
*/
void
Perl_gv_init_sv(pTHX_ GV *gv, HV *stash, SV* namesv, U32 flags)
{
char *namepv;
STRLEN namelen;
PERL_ARGS_ASSERT_GV_INIT_SV;
namepv = SvPV(namesv, namelen);
if (SvUTF8(namesv))
flags |= SVf_UTF8;
gv_init_pvn(gv, stash, namepv, namelen, flags);
}
void
Perl_gv_init_pv(pTHX_ GV *gv, HV *stash, const char *name, U32 flags)
{
PERL_ARGS_ASSERT_GV_INIT_PV;
gv_init_pvn(gv, stash, name, strlen(name), flags);
}
/* Packages in the symbol table are "stashes" - hashes where the keys are symbol
names and the values are typeglobs. The value $foo::bar is actually found
by looking up the typeglob *foo::{bar} and then reading its SCALAR slot.
At least, that's what you see in Perl space if you use typeglob syntax.
Usually it's also what's actually stored in the stash, but for some cases
different values are stored (as a space optimisation) and converted to full
typeglobs "on demand" - if a typeglob syntax is used to read a value. It's
the job of this function, Perl_gv_init_pvn(), to undo any trickery and
replace the SV stored in the stash with the regular PVGV structure that it is
a shorthand for. This has to be done "in-place" by upgrading the actual SV
that is already stored in the stash to a PVGV.
As the public documentation above says:
Converting any scalar that is C<SvOK()> may produce unpredictable
results and is reserved for perl's internal use.
Values that can be stored:
* plain scalar - a subroutine declaration
The scalar's string value is the subroutine prototype; the integer -1 is
"no prototype". ie shorthand for sub foo ($$); or sub bar;
* reference to a scalar - a constant. ie shorthand for sub PI() { 4; }
* reference to a sub - a subroutine (avoids allocating a PVGV)
The earliest optimisation was subroutine declarations, implemented in 1998
by commit 8472ac73d6d80294:
"Sub declaration cost reduced from ~500 to ~100 bytes"
This space optimisation needs to be invisible to regular Perl code. For this
code:
sub foo ($$);
*foo = [];
When the first line is compiled, the optimisation is used, and $::{foo} is
assigned the scalar '$$'. No PVGV or PVCV is created.
When the second line encountered, the typeglob lookup on foo needs to
"upgrade" the symbol table entry to a PVGV, and then create a PVCV in the
{CODE} slot with the prototype $$ and no body. The typeglob is then available
so that [] can be assigned to the {ARRAY} slot. For the code above the
upgrade happens at compile time, the assignment at runtime.
Analogous code unwinds the other optimisations.
*/
void
Perl_gv_init_pvn(pTHX_ GV *gv, HV *stash, const char *name, STRLEN len, U32 flags)
{
const U32 old_type = SvTYPE(gv);
const bool doproto = old_type > SVt_NULL;
char * const proto = (doproto && SvPOK(gv))
? ((void)(SvIsCOW(gv) && (sv_force_normal((SV *)gv), 0)), SvPVX(gv))
: NULL;
const STRLEN protolen = proto ? SvCUR(gv) : 0;
const U32 proto_utf8 = proto ? SvUTF8(gv) : 0;
SV *const has_constant = doproto && SvROK(gv) ? SvRV(gv) : NULL;
const U32 exported_constant = has_constant ? SvPCS_IMPORTED(gv) : 0;
const bool really_sub =
has_constant && SvTYPE(has_constant) == SVt_PVCV;
COP * const old = PL_curcop;
PERL_ARGS_ASSERT_GV_INIT_PVN;
assert (!(proto && has_constant));
if (has_constant) {
/* The constant has to be a scalar, array or subroutine. */
switch (SvTYPE(has_constant)) {
case SVt_PVHV:
case SVt_PVFM:
case SVt_PVIO:
Perl_croak(aTHX_ "Cannot convert a reference to %s to typeglob",
sv_reftype(has_constant, 0));
NOT_REACHED; /* NOTREACHED */
break;
default: NOOP;
}
SvRV_set(gv, NULL);
SvROK_off(gv);
}
if (old_type < SVt_PVGV) {
if (old_type >= SVt_PV)
SvCUR_set(gv, 0);
sv_upgrade(MUTABLE_SV(gv), SVt_PVGV);
}
if (SvLEN(gv)) {
if (proto) {
/* For this case, we are "stealing" the buffer from the SvPV and
re-attaching to an SV below with the call to sv_usepvn_flags().
Hence we don't free it. */
SvPV_set(gv, NULL);
}
else {
/* There is no valid prototype. (SvPOK() must be true for a valid
prototype.) Hence we free the memory. */
Safefree(SvPVX_mutable(gv));
}
SvLEN_set(gv, 0);
SvPOK_off(gv);
}
SvIOK_off(gv);
isGV_with_GP_on(gv);
if (really_sub && !CvISXSUB(has_constant) && CvSTART(has_constant)
&& (OP_TYPE_IS_COP_NN(CvSTART(has_constant))))
PL_curcop = (COP *)CvSTART(has_constant);
GvGP_set(gv, Perl_newGP(aTHX_ gv));
PL_curcop = old;
GvSTASH(gv) = stash;
if (stash)
Perl_sv_add_backref(aTHX_ MUTABLE_SV(stash), MUTABLE_SV(gv));
gv_name_set(gv, name, len, GV_ADD | ( flags & SVf_UTF8 ? SVf_UTF8 : 0 ));
if (flags & GV_ADDMULTI || doproto) /* doproto means it */
GvMULTI_on(gv); /* _was_ mentioned */
if (really_sub) {
/* Not actually a constant. Just a regular sub. */
CV * const cv = (CV *)has_constant;
GvCV_set(gv,cv);
if (CvNAMED(cv) && CvSTASH(cv) == stash && (
CvNAME_HEK(cv) == GvNAME_HEK(gv)
|| ( HEK_LEN(CvNAME_HEK(cv)) == HEK_LEN(GvNAME_HEK(gv))
&& HEK_FLAGS(CvNAME_HEK(cv)) != HEK_FLAGS(GvNAME_HEK(gv))
&& HEK_UTF8(CvNAME_HEK(cv)) == HEK_UTF8(GvNAME_HEK(gv))
&& memEQ(HEK_KEY(CvNAME_HEK(cv)), GvNAME(gv), GvNAMELEN(gv))
)
))
CvGV_set(cv,gv);
}
else if (doproto) {
CV *cv;
if (has_constant) {
/* newCONSTSUB takes ownership of the reference from us. */
cv = newCONSTSUB_flags(stash, name, len, flags, has_constant);
/* In case op.c:S_process_special_blocks stole it: */
if (!GvCV(gv))
GvCV_set(gv, (CV *)SvREFCNT_inc_simple_NN(cv));
assert(GvCV(gv) == cv); /* newCONSTSUB should have set this */
/* If this reference was a copy of another, then the subroutine
must have been "imported", by a Perl space assignment to a GV
from a reference to CV. */
if (exported_constant)
GvIMPORTED_CV_on(gv);
CvSTASH_set(cv, PL_curstash); /* XXX Why is this needed? */
} else {
cv = newSTUB(gv,1);
}
if (proto) {
sv_usepvn_flags(MUTABLE_SV(cv), proto, protolen,
SV_HAS_TRAILING_NUL);
if ( proto_utf8 ) SvUTF8_on(MUTABLE_SV(cv));
}
}
}
STATIC void
S_gv_init_svtype(pTHX_ GV *gv, const svtype sv_type)
{
PERL_ARGS_ASSERT_GV_INIT_SVTYPE;
switch (sv_type) {
case SVt_PVIO:
(void)GvIOn(gv);
break;
case SVt_PVAV:
(void)GvAVn(gv);
break;
case SVt_PVHV:
(void)GvHVn(gv);
break;
#ifdef PERL_DONT_CREATE_GVSV
case SVt_NULL:
case SVt_PVCV:
case SVt_PVFM:
case SVt_PVGV:
break;
default:
if(GvSVn(gv)) {
/* Work round what appears to be a bug in Sun C++ 5.8 2005/10/13
If we just cast GvSVn(gv) to void, it ignores evaluating it for
its side effect */
}
#endif
}
}
static void core_xsub(pTHX_ CV* cv);
static GV *
S_maybe_add_coresub(pTHX_ HV * const stash, GV *gv,
const char * const name, const STRLEN len)
{
const int code = keyword(name, len, 1);
static const char file[] = __FILE__;
CV *cv, *oldcompcv = NULL;
int opnum = 0;
bool ampable = TRUE; /* &{}-able */
COP *oldcurcop = NULL;
yy_parser *oldparser = NULL;
I32 oldsavestack_ix = 0;
assert(gv || stash);
assert(name);
if (!code) return NULL; /* Not a keyword */
switch (code < 0 ? -code : code) {
/* no support for \&CORE::infix;
no support for funcs that do not parse like funcs */
case KEY___DATA__: case KEY___END__ :
case KEY_ADJUST : case KEY_AUTOLOAD: case KEY_BEGIN : case KEY_CHECK :
case KEY_DESTROY : case KEY_END : case KEY_INIT : case KEY_UNITCHECK:
case KEY_all : case KEY_and : case KEY_any :
case KEY_catch : case KEY_class :
case KEY_cmp : case KEY_default : case KEY_defer :
case KEY_do : case KEY_dump : case KEY_else : case KEY_elsif :
case KEY_eq : case KEY_eval : case KEY_field :
case KEY_finally:
case KEY_for : case KEY_foreach: case KEY_format: case KEY_ge :
case KEY_given : case KEY_goto : case KEY_grep : case KEY_gt :
case KEY_if : case KEY_isa :
case KEY_last :
case KEY_le : case KEY_local : case KEY_lt : case KEY_m :
case KEY_map : case KEY_method : case KEY_my :
case KEY_ne : case KEY_next : case KEY_no: case KEY_or: case KEY_our:
case KEY_package: case KEY_print: case KEY_printf:
case KEY_q : case KEY_qq : case KEY_qr : case KEY_qw :
case KEY_qx : case KEY_redo : case KEY_require: case KEY_return:
case KEY_s : case KEY_say : case KEY_sort :
case KEY_state: case KEY_sub :
case KEY_tr : case KEY_try :
case KEY_unless:
case KEY_until: case KEY_use : case KEY_when : case KEY_while :
case KEY_x : case KEY_xor : case KEY_y :
return NULL;
case KEY_chomp: case KEY_chop: case KEY_defined: case KEY_delete:
case KEY_eof : case KEY_exec: case KEY_exists :
case KEY_lstat:
case KEY_split:
case KEY_stat:
case KEY_system:
case KEY_truncate: case KEY_unlink:
ampable = FALSE;
}
if (!gv) {
gv = (GV *)newSV_type(SVt_NULL);
gv_init(gv, stash, name, len, TRUE);
}
GvMULTI_on(gv);
if (ampable) {
ENTER;
oldcurcop = PL_curcop;
oldparser = PL_parser;
lex_start(NULL, NULL, 0);
oldcompcv = PL_compcv;
PL_compcv = NULL; /* Prevent start_subparse from setting
CvOUTSIDE. */
oldsavestack_ix = start_subparse(FALSE,0);
cv = PL_compcv;
}
else {
/* Avoid calling newXS, as it calls us, and things start to
get hairy. */
cv = MUTABLE_CV(newSV_type(SVt_PVCV));
GvCV_set(gv,cv);
GvCVGEN(gv) = 0;
CvISXSUB_on(cv);
CvXSUB(cv) = core_xsub;
PoisonPADLIST(cv);
}
CvGV_set(cv, gv); /* This stops new ATTRSUB from setting CvFILE
from PL_curcop. */
/* XSUBs can't be perl lang/perl5db.pl debugged
if (PERLDB_LINE_OR_SAVESRC)
(void)gv_fetchfile(file); */
CvFILE(cv) = (char *)file;
/* XXX This is inefficient, as doing things this order causes
a prototype check in newATTRSUB. But we have to do
it this order as we need an op number before calling
new ATTRSUB. */
(void)core_prototype((SV *)cv, name, code, &opnum);
if (stash)
(void)hv_store(stash,name,len,(SV *)gv,0);
if (ampable) {
#ifdef DEBUGGING
CV *orig_cv = cv;
#endif
CvLVALUE_on(cv);
/* newATTRSUB will free the CV and return NULL if we're still
compiling after a syntax error */
if ((cv = newATTRSUB_x(
oldsavestack_ix, (OP *)gv,
NULL,NULL,
coresub_op(
opnum
? newSVuv((UV)opnum)
: newSVpvn(name,len),
code, opnum
),
TRUE
)) != NULL) {
assert(GvCV(gv) == orig_cv);
if (opnum != OP_VEC && opnum != OP_SUBSTR && opnum != OP_POS
&& opnum != OP_UNDEF && opnum != OP_KEYS)
CvLVALUE_off(cv); /* Now *that* was a neat trick. */
}
LEAVE;
PL_parser = oldparser;
PL_curcop = oldcurcop;
PL_compcv = oldcompcv;
}
if (cv) {
SV *opnumsv = newSViv(
(opnum == OP_ENTEREVAL && len == 9 && memEQ(name, "evalbytes", 9)) ?
(OP_ENTEREVAL | (1<<16))
: opnum ? opnum : (((I32)name[2]) << 16));
cv_set_call_checker_flags(cv, Perl_ck_entersub_args_core, opnumsv, 0);
SvREFCNT_dec_NN(opnumsv);
}
return gv;
}
/*
=for apidoc gv_fetchmeth
=for apidoc_item gv_fetchmeth_autoload
=for apidoc_item gv_fetchmeth_pv
=for apidoc_item gv_fetchmeth_pv_autoload
=for apidoc_item gv_fetchmeth_pvn
=for apidoc_item gv_fetchmeth_pvn_autoload
=for apidoc_item gv_fetchmeth_sv
=for apidoc_item gv_fetchmeth_sv_autoload
These each look for a glob with name C<name>, containing a defined subroutine,
returning the GV of that glob if found, or C<NULL> if not.
You probably want to use the C<L</gv_fetchmethod>> family of functions
instead.
Searching is always done in the following order, with some steps skipped
depending on various criteria. The first match found is used, ending the
search. C<gv_fetchmeth_pv> and C<gv_fetchmeth_pv_autoload> lack a flags
parameter, so in the following, consider C<flags> to be zero for those two
functions.
=over
=item 1
C<stash> is searched first, unless C<stash> either is NULL or C<GV_SUPER> is
set in C<flags>.
=item 2
Stashes accessible via C<@ISA> are searched next.
Searching is conducted according to L<C<MRO> order|perlmroapi>.
=item 3
C<UNIVERSAL::> is searched unless C<GV_NOUNIVERSAL> is set.
=item 4
Autoloaded subroutines are then looked for, but only for the forms whose names
end in C<_autoload>, and when C<stash> is not NULL and C<GV_SUPER> is not set.
=back
The argument C<level> should be either 0 or -1.
=over
=item If -1
No method caching is done.
=item If 0
If C<GV_SUPER> is not set in C<flags>, the method found is cached in C<stash>.
If C<GV_SUPER> is set in C<flags>, the method is cached in the super
cache for C<stash>.
If the method is not found a negative cache entry is added.
Note that subroutines found in C<UNIVERSAL::> are not cached,
though this may change.
=back
The GV returned from these may be a method cache entry, which is not visible to
Perl code. So when calling C<L</call_sv>>, you should not use the GV directly;
instead, you should use the method's CV, which can be obtained from the GV with
the C<GvCV> macro. For an autoloaded subroutine without a stub, C<GvCV()> of
the result may be zero.
The only other significant value for C<flags> is C<SVf_UTF8>, indicating that
C<name> is to be treated as being encoded in UTF-8. Since plain
C<gv_fetchmeth> and C<gv_fetchmeth_autoload> lack a C<flags> parameter, C<name>
is never UTF-8.
Otherwise, the functions behave identically, except as noted below.
In C<gv_fetchmeth_pv> and C<gv_fetchmeth_pv_autoload>, C<name> is a C language
NUL-terminated string.
In C<gv_fetchmeth>, C<gv_fetchmeth_pvn>, C<gv_fetchmeth_autoload>, and
C<gv_fetchmeth_pvn_autoload>, C<name> points to the first byte of the name, and
an additional parameter, C<len>, specifies its length in bytes. Hence, the
name may contain embedded-NUL characters.
In C<gv_fetchmeth_sv> and C<gv_fetchmeth_sv_autoload>, C<*name> is an SV, and
the name is the PV extracted from that, using C<L</SvPV>>. If the SV is marked
as being in UTF-8, the extracted PV will also be. Including C<SVf_UTF8> in
C<flags> will force the name to be considered to be UTF-8 even if the SV is
not so marked.
=for apidoc Amnh||GV_SUPER
=for apidoc Amnh||GV_NOUNIVERSAL
=cut
*/
GV *
Perl_gv_fetchmeth_sv(pTHX_ HV *stash, SV *namesv, I32 level, U32 flags)
{
char *namepv;
STRLEN namelen;
PERL_ARGS_ASSERT_GV_FETCHMETH_SV;
if (LIKELY(SvPOK_nog(namesv))) /* common case */
return gv_fetchmeth_internal(stash, namesv, NULL, 0, level,
flags | SvUTF8(namesv));
namepv = SvPV(namesv, namelen);
if (SvUTF8(namesv)) flags |= SVf_UTF8;
return gv_fetchmeth_pvn(stash, namepv, namelen, level, flags);
}
GV *
Perl_gv_fetchmeth_pv(pTHX_ HV *stash, const char *name, I32 level, U32 flags)
{
PERL_ARGS_ASSERT_GV_FETCHMETH_PV;
return gv_fetchmeth_internal(stash, NULL, name, strlen(name), level, flags);
}
/* NOTE: No support for tied ISA */
PERL_STATIC_INLINE GV*
S_gv_fetchmeth_internal(pTHX_ HV* stash, SV* meth, const char* name, STRLEN len, I32 level, U32 flags)
{
GV** gvp;
HE* he;
AV* linear_av;
SV** linear_svp;
SV* linear_sv;
HV* cstash, *cachestash;
GV* candidate = NULL;
CV* cand_cv = NULL;
GV* topgv = NULL;
const char *hvname;
STRLEN hvnamelen;
I32 create = (level >= 0) ? HV_FETCH_LVALUE : 0;
I32 items;
U32 topgen_cmp;
U32 is_utf8 = flags & SVf_UTF8;
/* UNIVERSAL methods should be callable without a stash */
if (!stash) {
create = 0; /* probably appropriate */
if(!(stash = gv_stashpvs("UNIVERSAL", 0)))
return 0;
}
assert(stash);
hvname = HvNAME_get(stash);
hvnamelen = HvNAMELEN_get(stash);
if (!hvname)
Perl_croak(aTHX_ "Can't use anonymous symbol table for method lookup");
assert(hvname);
assert(name || meth);
DEBUG_o( Perl_deb(aTHX_ "Looking for %smethod %s in package %s\n",
flags & GV_SUPER ? "SUPER " : "",
name ? name : SvPV_nolen(meth), hvname) );
topgen_cmp = HvMROMETA(stash)->cache_gen + PL_sub_generation;
if (flags & GV_SUPER) {
if (!HvAUX(stash)->xhv_mro_meta->super)
HvAUX(stash)->xhv_mro_meta->super = newHV();
cachestash = HvAUX(stash)->xhv_mro_meta->super;
}
else cachestash = stash;
/* check locally for a real method or a cache entry */
he = (HE*)hv_common(
cachestash, meth, name, len, is_utf8 ? HVhek_UTF8 : 0, create, NULL, 0
);
if (he) gvp = (GV**)&HeVAL(he);
else gvp = NULL;
if(gvp) {
topgv = *gvp;
have_gv:
assert(topgv);
if (SvTYPE(topgv) != SVt_PVGV)
{
if (!name)
name = SvPV_nomg(meth, len);
gv_init_pvn(topgv, stash, name, len, GV_ADDMULTI|is_utf8);
}
if ((cand_cv = GvCV(topgv))) {
/* If genuine method or valid cache entry, use it */
if (!GvCVGEN(topgv) || GvCVGEN(topgv) == topgen_cmp) {
return topgv;
}
else {
/* stale cache entry, junk it and move on */
SvREFCNT_dec_NN(cand_cv);
GvCV_set(topgv, NULL);
cand_cv = NULL;
GvCVGEN(topgv) = 0;
}
}
else if (GvCVGEN(topgv) == topgen_cmp) {
/* cache indicates no such method definitively */
return 0;
}
else if (stash == cachestash
&& len > 1 /* shortest is uc */
&& memEQs(hvname, HvNAMELEN_get(stash), "CORE")
&& S_maybe_add_coresub(aTHX_ NULL,topgv,name,len))
goto have_gv;
}
linear_av = mro_get_linear_isa(stash); /* has ourselves at the top of the list */
linear_svp = AvARRAY(linear_av) + 1; /* skip over self */
items = AvFILLp(linear_av); /* no +1, to skip over self */
while (items--) {
linear_sv = *linear_svp++;
assert(linear_sv);
cstash = gv_stashsv(linear_sv, 0);
if (!cstash) {
if ( ckWARN(WARN_SYNTAX)) {
if( /* these are loaded from Perl_Gv_AMupdate() one way or another */
( len && name[0] == '(' ) /* overload.pm related, in particular "()" */
|| ( memEQs( name, len, "DESTROY") )
) {
Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
"Can't locate package %" SVf " for @%" HEKf "::ISA",
SVfARG(linear_sv),
HEKfARG(HvNAME_HEK(stash)));
} else if( memEQs( name, len, "AUTOLOAD") ) {
/* gobble this warning */
} else {
Perl_ck_warner(aTHX_ packWARN(WARN_SYNTAX),
"While trying to resolve method call %.*s->%.*s()"
" can not locate package %" SVf_QUOTEDPREFIX " yet it is mentioned in @%.*s::ISA"
" (perhaps you forgot to load %" SVf_QUOTEDPREFIX "?)",
(int) hvnamelen, hvname,
(int) len, name,
SVfARG(linear_sv),
(int) hvnamelen, hvname,
SVfARG(linear_sv));
}
}
continue;
}
assert(cstash);
gvp = (GV**)hv_common(
cstash, meth, name, len, is_utf8 ? HVhek_UTF8 : 0, HV_FETCH_JUST_SV, NULL, 0
);
if (!gvp) {
if (len > 1 && HvNAMELEN_get(cstash) == 4) {
const char *hvname = HvNAME(cstash); assert(hvname);
if (strBEGINs(hvname, "CORE")
&& (candidate =
S_maybe_add_coresub(aTHX_ cstash,NULL,name,len)
))
goto have_candidate;
}
continue;
}
else candidate = *gvp;
have_candidate:
assert(candidate);
if (SvTYPE(candidate) != SVt_PVGV)
gv_init_pvn(candidate, cstash, name, len, GV_ADDMULTI|is_utf8);
if (SvTYPE(candidate) == SVt_PVGV && (cand_cv = GvCV(candidate)) && !GvCVGEN(candidate)) {
/*
* Found real method, cache method in topgv if:
* 1. topgv has no synonyms (else inheritance crosses wires)
* 2. method isn't a stub (else AUTOLOAD fails spectacularly)