UTF-8 documentation.
[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) {
599cee73 77 if (ckWARN(WARN_MISC))
cea2e8a9 78 Perl_warner(aTHX_ WARN_SYNTAX,
599cee73 79 "Can't locate package %s for @%s::ISA",
6d4a7be2 80 SvPVX(sv), HvNAME(stash));
81 continue;
82 }
46e4b22b 83 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
3280af22 84 (void)hv_store(hv,name,len,&PL_sv_yes,0);
85 return &PL_sv_yes;
6d4a7be2 86 }
87 }
3280af22 88 (void)hv_store(hv,name,len,&PL_sv_no,0);
6d4a7be2 89 }
90 }
91
e09f3e01 92 return boolSV(strEQ(name, "UNIVERSAL"));
6d4a7be2 93}
94
954c1994 95/*
96=for apidoc sv_derived_from
97
98Returns a boolean indicating whether the SV is derived from the specified
99class. This is the function that implements C<UNIVERSAL::isa>. It works
100for class names as well as for objects.
101
102=cut
103*/
104
55497cff 105bool
864dbfa3 106Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
55497cff 107{
55497cff 108 char *type;
109 HV *stash;
46e4b22b 110
55497cff 111 stash = Nullhv;
112 type = Nullch;
46e4b22b 113
55497cff 114 if (SvGMAGICAL(sv))
115 mg_get(sv) ;
116
117 if (SvROK(sv)) {
118 sv = SvRV(sv);
119 type = sv_reftype(sv,0);
46e4b22b 120 if (SvOBJECT(sv))
55497cff 121 stash = SvSTASH(sv);
122 }
123 else {
124 stash = gv_stashsv(sv, FALSE);
125 }
46e4b22b 126
55497cff 127 return (type && strEQ(type,name)) ||
3280af22 128 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
55497cff 129 ? TRUE
130 : FALSE ;
55497cff 131}
132
0cb96387 133void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
134void XS_UNIVERSAL_can(pTHXo_ CV *cv);
135void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
136
137void
138Perl_boot_core_UNIVERSAL(pTHX)
139{
140 char *file = __FILE__;
141
142 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
143 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
144 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
145}
146
76e3520e 147#include "XSUB.h"
55497cff 148
6d4a7be2 149XS(XS_UNIVERSAL_isa)
150{
151 dXSARGS;
55497cff 152 SV *sv;
153 char *name;
2d8e6c8d 154 STRLEN n_a;
6d4a7be2 155
156 if (items != 2)
cea2e8a9 157 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
6d4a7be2 158
159 sv = ST(0);
f8f70380 160
d3f7f2b2 161 if (SvGMAGICAL(sv))
162 mg_get(sv);
163
aca069ec 164 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380 165 XSRETURN_UNDEF;
166
2d8e6c8d 167 name = (char *)SvPV(ST(1),n_a);
6d4a7be2 168
54310121 169 ST(0) = boolSV(sv_derived_from(sv, name));
6d4a7be2 170 XSRETURN(1);
171}
172
6d4a7be2 173XS(XS_UNIVERSAL_can)
174{
175 dXSARGS;
176 SV *sv;
177 char *name;
178 SV *rv;
6f08146e 179 HV *pkg = NULL;
2d8e6c8d 180 STRLEN n_a;
6d4a7be2 181
182 if (items != 2)
cea2e8a9 183 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
6d4a7be2 184
185 sv = ST(0);
f8f70380 186
d3f7f2b2 187 if (SvGMAGICAL(sv))
188 mg_get(sv);
189
aca069ec 190 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
f8f70380 191 XSRETURN_UNDEF;
192
2d8e6c8d 193 name = (char *)SvPV(ST(1),n_a);
3280af22 194 rv = &PL_sv_undef;
6d4a7be2 195
46e4b22b 196 if (SvROK(sv)) {
6f08146e 197 sv = (SV*)SvRV(sv);
46e4b22b 198 if (SvOBJECT(sv))
6f08146e 199 pkg = SvSTASH(sv);
200 }
201 else {
202 pkg = gv_stashsv(sv, FALSE);
203 }
204
205 if (pkg) {
dc848c6f 206 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
207 if (gv && isGV(gv))
208 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
6d4a7be2 209 }
210
211 ST(0) = rv;
212 XSRETURN(1);
213}
214
6d4a7be2 215XS(XS_UNIVERSAL_VERSION)
216{
217 dXSARGS;
218 HV *pkg;
219 GV **gvp;
220 GV *gv;
221 SV *sv;
222 char *undef;
223
1571675a 224 if (SvROK(ST(0))) {
6d4a7be2 225 sv = (SV*)SvRV(ST(0));
1571675a 226 if (!SvOBJECT(sv))
cea2e8a9 227 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
6d4a7be2 228 pkg = SvSTASH(sv);
229 }
230 else {
231 pkg = gv_stashsv(ST(0), FALSE);
232 }
233
234 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
235
d4bea2fb 236 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
6d4a7be2 237 SV *nsv = sv_newmortal();
238 sv_setsv(nsv, sv);
239 sv = nsv;
240 undef = Nullch;
241 }
242 else {
3280af22 243 sv = (SV*)&PL_sv_undef;
6d4a7be2 244 undef = "(undef)";
245 }
246
1571675a 247 if (items > 1) {
248 STRLEN len;
249 SV *req = ST(1);
250
251 if (undef)
252 Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
253 HvNAME(pkg), HvNAME(pkg));
254
255 if (!SvNIOK(sv) && SvPOK(sv)) {
256 char *str = SvPVx(sv,len);
257 while (len) {
258 --len;
259 /* XXX could DWIM "1.2.3" here */
260 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
261 break;
262 }
263 if (len) {
4305d8ab 264 if (SvNOK(req) && SvPOK(req)) {
1571675a 265 /* they said C<use Foo v1.2.3> and $Foo::VERSION
266 * doesn't look like a float: do string compare */
267 if (sv_cmp(req,sv) == 1) {
d2560b70 268 Perl_croak(aTHX_ "%s v%"VDf" required--"
269 "this is only v%"VDf,
1571675a 270 HvNAME(pkg), req, sv);
271 }
272 goto finish;
273 }
274 /* they said C<use Foo 1.002_003> and $Foo::VERSION
275 * doesn't look like a float: force numeric compare */
155aba94 276 (void)SvUPGRADE(sv, SVt_PVNV);
1571675a 277 SvNVX(sv) = str_to_version(sv);
278 SvPOK_off(sv);
279 SvNOK_on(sv);
280 }
281 }
282 /* if we get here, we're looking for a numeric comparison,
283 * so force the required version into a float, even if they
284 * said C<use Foo v1.2.3> */
4305d8ab 285 if (SvNOK(req) && SvPOK(req)) {
1571675a 286 NV n = SvNV(req);
287 req = sv_newmortal();
288 sv_setnv(req, n);
289 }
290
291 if (SvNV(req) > SvNV(sv))
292 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
293 HvNAME(pkg), SvPV(req,len), SvPV(sv,len));
2d8e6c8d 294 }
6d4a7be2 295
1571675a 296finish:
6d4a7be2 297 ST(0) = sv;
298
299 XSRETURN(1);
300}
301