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