[win32] integrate mainline
[p5sagit/p5-mst-13.2.git] / universal.c
CommitLineData
6d4a7be2 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 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);
93965878 51 /* NOTE: No support for tied ISA */
52 I32 items = AvFILLp(av) + 1;
6d4a7be2 53 while (items--) {
54 SV* sv = *svp++;
55 HV* basestash = gv_stashsv(sv, FALSE);
56 if (!basestash) {
57 if (dowarn)
58 warn("Can't locate package %s for @%s::ISA",
59 SvPVX(sv), HvNAME(stash));
60 continue;
61 }
62 if(&sv_yes == isa_lookup(basestash, name, len, level + 1)) {
63 (void)hv_store(hv,name,len,&sv_yes,0);
64 return &sv_yes;
65 }
66 }
67 (void)hv_store(hv,name,len,&sv_no,0);
68 }
69 }
70
e09f3e01 71 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2 72}
73
55497cff 74bool
8ac85365 75sv_derived_from(SV *sv, char *name)
55497cff 76{
77 SV *rv;
78 char *type;
79 HV *stash;
80
81 stash = Nullhv;
82 type = Nullch;
83
84 if (SvGMAGICAL(sv))
85 mg_get(sv) ;
86
87 if (SvROK(sv)) {
88 sv = SvRV(sv);
89 type = sv_reftype(sv,0);
90 if(SvOBJECT(sv))
91 stash = SvSTASH(sv);
92 }
93 else {
94 stash = gv_stashsv(sv, FALSE);
95 }
96
97 return (type && strEQ(type,name)) ||
98 (stash && isa_lookup(stash, name, strlen(name), 0) == &sv_yes)
99 ? TRUE
100 : FALSE ;
101
102}
103
104
6d4a7be2 105static
106XS(XS_UNIVERSAL_isa)
107{
108 dXSARGS;
55497cff 109 SV *sv;
110 char *name;
6d4a7be2 111
112 if (items != 2)
113 croak("Usage: UNIVERSAL::isa(reference, kind)");
114
115 sv = ST(0);
116 name = (char *)SvPV(ST(1),na);
117
54310121 118 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2 119 XSRETURN(1);
120}
121
122static
123XS(XS_UNIVERSAL_can)
124{
125 dXSARGS;
126 SV *sv;
127 char *name;
128 SV *rv;
6f08146e 129 HV *pkg = NULL;
6d4a7be2 130
131 if (items != 2)
132 croak("Usage: UNIVERSAL::can(object-ref, method)");
133
134 sv = ST(0);
135 name = (char *)SvPV(ST(1),na);
136 rv = &sv_undef;
137
6f08146e 138 if(SvROK(sv)) {
139 sv = (SV*)SvRV(sv);
140 if(SvOBJECT(sv))
141 pkg = SvSTASH(sv);
142 }
143 else {
144 pkg = gv_stashsv(sv, FALSE);
145 }
146
147 if (pkg) {
dc848c6f 148 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
149 if (gv && isGV(gv))
150 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2 151 }
152
153 ST(0) = rv;
154 XSRETURN(1);
155}
156
157static
6d4a7be2 158XS(XS_UNIVERSAL_VERSION)
159{
160 dXSARGS;
161 HV *pkg;
162 GV **gvp;
163 GV *gv;
164 SV *sv;
165 char *undef;
13826f2c 166 double req;
6d4a7be2 167
168 if(SvROK(ST(0))) {
169 sv = (SV*)SvRV(ST(0));
170 if(!SvOBJECT(sv))
171 croak("Cannot find version of an unblessed reference");
172 pkg = SvSTASH(sv);
173 }
174 else {
175 pkg = gv_stashsv(ST(0), FALSE);
176 }
177
178 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
179
180 if (gvp && (gv = *gvp) != (GV*)&sv_undef && (sv = GvSV(gv))) {
181 SV *nsv = sv_newmortal();
182 sv_setsv(nsv, sv);
183 sv = nsv;
184 undef = Nullch;
185 }
186 else {
187 sv = (SV*)&sv_undef;
188 undef = "(undef)";
189 }
190
13826f2c 191 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv))))
6d4a7be2 192 croak("%s version %s required--this is only version %s",
13826f2c 193 HvNAME(pkg), SvPV(ST(1),na), undef ? undef : SvPV(sv,na));
6d4a7be2 194
195 ST(0) = sv;
196
197 XSRETURN(1);
198}
199
200void
8ac85365 201boot_core_UNIVERSAL(void)
6d4a7be2 202{
203 char *file = __FILE__;
204
205 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
206 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
6d4a7be2 207 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
208}