This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Configure fix for handling DynaLoader
[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
55497cff 123 ST(0) = (sv_derived_from(sv, name) ? &sv_yes : &sv_no) ;
760ac839 124
6d4a7be2
PP
125 XSRETURN(1);
126}
127
128static
129XS(XS_UNIVERSAL_can)
130{
131 dXSARGS;
132 SV *sv;
133 char *name;
134 SV *rv;
135 GV *gv;
136 CV *cvp;
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
145 if(SvROK(sv) && (sv = (SV*)SvRV(sv)) && SvOBJECT(sv)) {
146 gv = gv_fetchmethod(SvSTASH(sv), name);
147
148 if(gv && GvCV(gv)) {
149 /* If the sub is only a stub then we may have a gv to AUTOLOAD */
150 GV **gvp = (GV**)hv_fetch(GvSTASH(gv), name, strlen(name), TRUE);
151 if(gvp && (cvp = GvCV(*gvp))) {
152 rv = sv_newmortal();
153 sv_setsv(rv, newRV((SV*)cvp));
154 }
155 }
156 }
157
158 ST(0) = rv;
159 XSRETURN(1);
160}
161
162static
163XS(XS_UNIVERSAL_is_instance)
164{
165 dXSARGS;
166 ST(0) = SvROK(ST(0)) ? &sv_yes : &sv_no;
167 XSRETURN(1);
168}
169
170static
171XS(XS_UNIVERSAL_class)
172{
173 dXSARGS;
bf3f28f2 174 if(SvROK(ST(0)) && SvOBJECT(SvRV(ST(0)))) {
6d4a7be2 175 SV *sv = sv_newmortal();
bf3f28f2 176 sv_setpv(sv, HvNAME(SvSTASH(SvRV(ST(0)))));
6d4a7be2
PP
177 ST(0) = sv;
178 }
179 XSRETURN(1);
180}
181
182static
183XS(XS_UNIVERSAL_VERSION)
184{
185 dXSARGS;
186 HV *pkg;
187 GV **gvp;
188 GV *gv;
189 SV *sv;
190 char *undef;
191
192 if(SvROK(ST(0))) {
193 sv = (SV*)SvRV(ST(0));
194 if(!SvOBJECT(sv))
195 croak("Cannot find version of an unblessed reference");
196 pkg = SvSTASH(sv);
197 }
198 else {
199 pkg = gv_stashsv(ST(0), FALSE);
200 }
201
202 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
203
204 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (sv = GvSV(gv))) {
205 SV *nsv = sv_newmortal();
206 sv_setsv(nsv, sv);
207 sv = nsv;
208 undef = Nullch;
209 }
210 else {
211 sv = (SV*)&sv_undef;
212 undef = "(undef)";
213 }
214
215 if(items > 1 && (undef || SvNV(ST(1)) > SvNV(sv)))
216 croak("%s version %s required--this is only version %s",
217 HvNAME(pkg),SvPV(ST(1),na),undef ? undef : SvPV(sv,na));
218
219 ST(0) = sv;
220
221 XSRETURN(1);
222}
223
224void
225boot_core_UNIVERSAL()
226{
227 char *file = __FILE__;
228
229 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
230 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
231 newXS("UNIVERSAL::class", XS_UNIVERSAL_class, file);
232 newXS("UNIVERSAL::is_instance", XS_UNIVERSAL_is_instance, file);
233 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
234}