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