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