Update Changes.
[p5sagit/p5-mst-13.2.git] / universal.c
CommitLineData
6d4a7be2 1#include "EXTERN.h"
864dbfa3 2#define PERL_IN_UNIVERSAL_C
6d4a7be2 3#include "perl.h"
6d4a7be2 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
76e3520e 10STATIC SV *
cea2e8a9 11S_isa_lookup(pTHX_ HV *stash, const char *name, int len, int level)
6d4a7be2 12{
13 AV* av;
14 GV* gv;
15 GV** gvp;
16 HV* hv = Nullhv;
46e4b22b 17 SV* subgen = Nullsv;
6d4a7be2 18
19 if (!stash)
3280af22 20 return &PL_sv_undef;
6d4a7be2 21
46e4b22b 22 if (strEQ(HvNAME(stash), name))
3280af22 23 return &PL_sv_yes;
6d4a7be2 24
25 if (level > 100)
46e4b22b 26 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
27 HvNAME(stash));
6d4a7be2 28
29 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
30
46e4b22b 31 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
32 && (hv = GvHV(gv)))
33 {
34 if (SvIV(subgen) == PL_sub_generation) {
35 SV* sv;
36 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
37 if (svp && (sv = *svp) != (SV*)&PL_sv_undef) {
38 DEBUG_o( Perl_deb(aTHX_ "Using cached ISA %s for package %s\n",
39 name, HvNAME(stash)) );
40 return sv;
41 }
42 }
43 else {
44 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
45 HvNAME(stash)) );
46 hv_clear(hv);
47 sv_setiv(subgen, PL_sub_generation);
48 }
6d4a7be2 49 }
50
51 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
46e4b22b 52
3280af22 53 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
46e4b22b 54 if (!hv || !subgen) {
6d4a7be2 55 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
56
57 gv = *gvp;
58
59 if (SvTYPE(gv) != SVt_PVGV)
60 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
61
46e4b22b 62 if (!hv)
63 hv = GvHVn(gv);
64 if (!subgen) {
65 subgen = newSViv(PL_sub_generation);
66 GvSV(gv) = subgen;
67 }
6d4a7be2 68 }
46e4b22b 69 if (hv) {
6d4a7be2 70 SV** svp = AvARRAY(av);
93965878 71 /* NOTE: No support for tied ISA */
72 I32 items = AvFILLp(av) + 1;
6d4a7be2 73 while (items--) {
74 SV* sv = *svp++;
75 HV* basestash = gv_stashsv(sv, FALSE);
76 if (!basestash) {
d008e5eb 77 dTHR;
599cee73 78 if (ckWARN(WARN_MISC))
cea2e8a9 79 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73 80 "Can't locate package %s for @%s::ISA",
6d4a7be2 81 SvPVX(sv), HvNAME(stash));
82 continue;
83 }
46e4b22b 84 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
3280af22 85 (void)hv_store(hv,name,len,&PL_sv_yes,0);
86 return &PL_sv_yes;
6d4a7be2 87 }
88 }
3280af22 89 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2 90 }
91 }
92
e09f3e01 93 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2 94}
95
954c1994 96/*
97=for apidoc sv_derived_from
98
99Returns a boolean indicating whether the SV is derived from the specified
100class. This is the function that implements C<UNIVERSAL::isa>. It works
101for class names as well as for objects.
102
103=cut
104*/
105
55497cff 106bool
864dbfa3 107Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 108{
55497cff 109 char *type;
110 HV *stash;
46e4b22b 111
55497cff 112 stash = Nullhv;
113 type = Nullch;
46e4b22b 114
55497cff 115 if (SvGMAGICAL(sv))
116 mg_get(sv) ;
117
118 if (SvROK(sv)) {
119 sv = SvRV(sv);
120 type = sv_reftype(sv,0);
46e4b22b 121 if (SvOBJECT(sv))
55497cff 122 stash = SvSTASH(sv);
123 }
124 else {
125 stash = gv_stashsv(sv, FALSE);
126 }
46e4b22b 127
55497cff 128 return (type && strEQ(type,name)) ||
3280af22 129 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
55497cff 130 ? TRUE
131 : FALSE ;
55497cff 132}
133
0cb96387 134void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
135void XS_UNIVERSAL_can(pTHXo_ CV *cv);
136void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
137
138void
139Perl_boot_core_UNIVERSAL(pTHX)
140{
141 char *file = __FILE__;
142
143 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
144 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
145 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
146}
147
76e3520e 148#include "XSUB.h"
55497cff 149
6d4a7be2 150XS(XS_UNIVERSAL_isa)
151{
152 dXSARGS;
55497cff 153 SV *sv;
154 char *name;
2d8e6c8d 155 STRLEN n_a;
6d4a7be2 156
157 if (items != 2)
cea2e8a9 158 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2 159
160 sv = ST(0);
f8f70380 161
d3f7f2b2 162 if (SvGMAGICAL(sv))
163 mg_get(sv);
164
aca069ec 165 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380 166 XSRETURN_UNDEF;
167
2d8e6c8d 168 name = (char *)SvPV(ST(1),n_a);
6d4a7be2 169
54310121 170 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2 171 XSRETURN(1);
172}
173
6d4a7be2 174XS(XS_UNIVERSAL_can)
175{
176 dXSARGS;
177 SV *sv;
178 char *name;
179 SV *rv;
6f08146e 180 HV *pkg = NULL;
2d8e6c8d 181 STRLEN n_a;
6d4a7be2 182
183 if (items != 2)
cea2e8a9 184 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2 185
186 sv = ST(0);
f8f70380 187
d3f7f2b2 188 if (SvGMAGICAL(sv))
189 mg_get(sv);
190
aca069ec 191 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380 192 XSRETURN_UNDEF;
193
2d8e6c8d 194 name = (char *)SvPV(ST(1),n_a);
3280af22 195 rv = &PL_sv_undef;
6d4a7be2 196
46e4b22b 197 if (SvROK(sv)) {
6f08146e 198 sv = (SV*)SvRV(sv);
46e4b22b 199 if (SvOBJECT(sv))
6f08146e 200 pkg = SvSTASH(sv);
201 }
202 else {
203 pkg = gv_stashsv(sv, FALSE);
204 }
205
206 if (pkg) {
dc848c6f 207 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
208 if (gv && isGV(gv))
209 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2 210 }
211
212 ST(0) = rv;
213 XSRETURN(1);
214}
215
6d4a7be2 216XS(XS_UNIVERSAL_VERSION)
217{
218 dXSARGS;
219 HV *pkg;
220 GV **gvp;
221 GV *gv;
222 SV *sv;
223 char *undef;
224
1571675a 225 if (SvROK(ST(0))) {
6d4a7be2 226 sv = (SV*)SvRV(ST(0));
1571675a 227 if (!SvOBJECT(sv))
cea2e8a9 228 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2 229 pkg = SvSTASH(sv);
230 }
231 else {
232 pkg = gv_stashsv(ST(0), FALSE);
233 }
234
235 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
236
d4bea2fb 237 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
6d4a7be2 238 SV *nsv = sv_newmortal();
239 sv_setsv(nsv, sv);
240 sv = nsv;
241 undef = Nullch;
242 }
243 else {
3280af22 244 sv = (SV*)&PL_sv_undef;
6d4a7be2 245 undef = "(undef)";
246 }
247
1571675a 248 if (items > 1) {
249 STRLEN len;
250 SV *req = ST(1);
251
252 if (undef)
253 Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
254 HvNAME(pkg), HvNAME(pkg));
255
256 if (!SvNIOK(sv) && SvPOK(sv)) {
257 char *str = SvPVx(sv,len);
258 while (len) {
259 --len;
260 /* XXX could DWIM "1.2.3" here */
261 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
262 break;
263 }
264 if (len) {
4305d8ab 265 if (SvNOK(req) && SvPOK(req)) {
1571675a 266 /* they said C<use Foo v1.2.3> and $Foo::VERSION
267 * doesn't look like a float: do string compare */
268 if (sv_cmp(req,sv) == 1) {
cc507455 269 Perl_croak(aTHX_ "%s v%vd required--"
270 "this is only v%vd",
1571675a 271 HvNAME(pkg), req, sv);
272 }
273 goto finish;
274 }
275 /* they said C<use Foo 1.002_003> and $Foo::VERSION
276 * doesn't look like a float: force numeric compare */
155aba94 277 (void)SvUPGRADE(sv, SVt_PVNV);
1571675a 278 SvNVX(sv) = str_to_version(sv);
279 SvPOK_off(sv);
280 SvNOK_on(sv);
281 }
282 }
283 /* if we get here, we're looking for a numeric comparison,
284 * so force the required version into a float, even if they
285 * said C<use Foo v1.2.3> */
4305d8ab 286 if (SvNOK(req) && SvPOK(req)) {
1571675a 287 NV n = SvNV(req);
288 req = sv_newmortal();
289 sv_setnv(req, n);
290 }
291
292 if (SvNV(req) > SvNV(sv))
293 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
294 HvNAME(pkg), SvPV(req,len), SvPV(sv,len));
2d8e6c8d 295 }
6d4a7be2 296
1571675a 297finish:
6d4a7be2 298 ST(0) = sv;
299
300 XSRETURN(1);
301}
302