This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[dummy merge]
[perl5.git] / universal.c
CommitLineData
6d4a7be2
PP
1#include "EXTERN.h"
2#include "perl.h"
3#include "XSUB.h"
4
5/*
6 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
7 * The main guts of traverse_isa was actually copied from gv_fetchmeth
8 */
9
10static SV *
11isa_lookup(stash, name, len, level)
12HV *stash;
13char *name;
14int len;
15int level;
16{
17 AV* av;
18 GV* gv;
19 GV** gvp;
20 HV* hv = Nullhv;
21
22 if (!stash)
23 return &sv_undef;
24
25 if(strEQ(HvNAME(stash), name))
26 return &sv_yes;
27
28 if (level > 100)
29 croak("Recursive inheritance detected");
30
31 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
32
33 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv))) {
34 SV* sv;
35 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
36 if (svp && (sv = *svp) != (SV*)&sv_undef)
37 return sv;
38 }
39
40 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
41
42 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) {
43 if(!hv) {
44 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
45
46 gv = *gvp;
47
48 if (SvTYPE(gv) != SVt_PVGV)
49 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
50
51 hv = GvHVn(gv);
52 }
53 if(hv) {
54 SV** svp = AvARRAY(av);
55 I32 items = AvFILL(av) + 1;
56 while (items--) {
57 SV* sv = *svp++;
58 HV* basestash = gv_stashsv(sv, FALSE);
59 if (!basestash) {
60 if (dowarn)
61 warn("Can't locate package %s for @%s::ISA",
62 SvPVX(sv), HvNAME(stash));
63 continue;
64 }
65 if(&sv_yes == isa_lookup(basestash, name, len, level + 1)) {
66 (void)hv_store(hv,name,len,&sv_yes,0);
67 return &sv_yes;
68 }
69 }
70 (void)hv_store(hv,name,len,&sv_no,0);
71 }
72 }
73
74 return &sv_no;
75}
76
55497cff
PP
77bool
78sv_derived_from(sv, name)
79SV * sv ;
80char * name ;
81{
82 SV *rv;
83 char *type;
84 HV *stash;
85
86 stash = Nullhv;
87 type = Nullch;
88
89 if (SvGMAGICAL(sv))
90 mg_get(sv) ;
91
92 if (SvROK(sv)) {
93 sv = SvRV(sv);
94 type = sv_reftype(sv,0);
95 if(SvOBJECT(sv))
96 stash = SvSTASH(sv);
97 }
98 else {
99 stash = gv_stashsv(sv, FALSE);
100 }
101
102 return (type && strEQ(type,name)) ||
103 (stash && isa_lookup(stash, name, strlen(name), 0) == &sv_yes)
104 ? TRUE
105 : FALSE ;
106
107}
108
109
6d4a7be2
PP
110static
111XS(XS_UNIVERSAL_isa)
112{
113 dXSARGS;
55497cff
PP
114 SV *sv;
115 char *name;
6d4a7be2
PP
116
117 if (items != 2)
118 croak("Usage: UNIVERSAL::isa(reference, kind)");
119
120 sv = ST(0);
121 name = (char *)SvPV(ST(1),na);
122
54310121 123 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2
PP
124 XSRETURN(1);
125}
126
127static
128XS(XS_UNIVERSAL_can)
129{
130 dXSARGS;
131 SV *sv;
132 char *name;
133 SV *rv;
134 GV *gv;
135 CV *cvp;
6f08146e 136 HV *pkg = NULL;
6d4a7be2
PP
137
138 if (items != 2)
139 croak("Usage: UNIVERSAL::can(object-ref, method)");
140
141 sv = ST(0);
142 name = (char *)SvPV(ST(1),na);
143 rv = &sv_undef;
144
6f08146e
NIS
145 if(SvROK(sv)) {
146 sv = (SV*)SvRV(sv);
147 if(SvOBJECT(sv))
148 pkg = SvSTASH(sv);
149 }
150 else {
151 pkg = gv_stashsv(sv, FALSE);
152 }
153
154 if (pkg) {
155 gv = gv_fetchmethod(pkg, name);
6d4a7be2
PP
156
157 if(gv && GvCV(gv)) {
158 /* If the sub is only a stub then we may have a gv to AUTOLOAD */
159 GV **gvp = (GV**)hv_fetch(GvSTASH(gv), name, strlen(name), TRUE);
160 if(gvp && (cvp = GvCV(*gvp))) {
161 rv = sv_newmortal();
162 sv_setsv(rv, newRV((SV*)cvp));
163 }
164 }
165 }
166
167 ST(0) = rv;
168 XSRETURN(1);
169}
170
171static
6d4a7be2
PP
172XS(XS_UNIVERSAL_VERSION)
173{
174 dXSARGS;
175 HV *pkg;
176 GV **gvp;
177 GV *gv;
178 SV *sv;
179 char *undef;
13826f2c 180 double req;
6d4a7be2
PP
181
182 if(SvROK(ST(0))) {
183 sv = (SV*)SvRV(ST(0));
184 if(!SvOBJECT(sv))
185 croak("Cannot find version of an unblessed reference");
186 pkg = SvSTASH(sv);
187 }
188 else {
189 pkg = gv_stashsv(ST(0), FALSE);
190 }
191
192 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
193
194 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (sv = GvSV(gv))) {
195 SV *nsv = sv_newmortal();
196 sv_setsv(nsv, sv);
197 sv = nsv;
198 undef = Nullch;
199 }
200 else {
201 sv = (SV*)&sv_undef;
202 undef = "(undef)";
203 }
204
13826f2c 205 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv))))
6d4a7be2 206 croak("%s version %s required--this is only version %s",
13826f2c 207 HvNAME(pkg), SvPV(ST(1),na), undef ? undef : SvPV(sv,na));
6d4a7be2
PP
208
209 ST(0) = sv;
210
211 XSRETURN(1);
212}
213
214void
215boot_core_UNIVERSAL()
216{
217 char *file = __FILE__;
218
219 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
220 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
6d4a7be2
PP
221 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
222}