This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate with Sarathy.
[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     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)) ||
109             (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
110         ? TRUE
111         : FALSE ;
112 }
113
114 void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
115 void XS_UNIVERSAL_can(pTHXo_ CV *cv);
116 void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
117
118 void
119 Perl_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
128 #include "XSUB.h"
129
130 XS(XS_UNIVERSAL_isa)
131 {
132     dXSARGS;
133     SV *sv;
134     char *name;
135     STRLEN n_a;
136
137     if (items != 2)
138         Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
139
140     sv = ST(0);
141
142     if (!SvOK(sv) || !(SvROK(sv) || SvCUR(sv)))
143         XSRETURN_UNDEF;
144
145     name = (char *)SvPV(ST(1),n_a);
146
147     ST(0) = boolSV(sv_derived_from(sv, name));
148     XSRETURN(1);
149 }
150
151 XS(XS_UNIVERSAL_can)
152 {
153     dXSARGS;
154     SV   *sv;
155     char *name;
156     SV   *rv;
157     HV   *pkg = NULL;
158     STRLEN n_a;
159
160     if (items != 2)
161         Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
162
163     sv = ST(0);
164
165     if (!SvOK(sv) || !(SvROK(sv) || SvCUR(sv)))
166         XSRETURN_UNDEF;
167
168     name = (char *)SvPV(ST(1),n_a);
169     rv = &PL_sv_undef;
170
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) {
181         GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
182         if (gv && isGV(gv))
183             rv = sv_2mortal(newRV((SV*)GvCV(gv)));
184     }
185
186     ST(0) = rv;
187     XSRETURN(1);
188 }
189
190 XS(XS_UNIVERSAL_VERSION)
191 {
192     dXSARGS;
193     HV *pkg;
194     GV **gvp;
195     GV *gv;
196     SV *sv;
197     char *undef;
198
199     if (SvROK(ST(0))) {
200         sv = (SV*)SvRV(ST(0));
201         if (!SvOBJECT(sv))
202             Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
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
211     if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
212         SV *nsv = sv_newmortal();
213         sv_setsv(nsv, sv);
214         sv = nsv;
215         undef = Nullch;
216     }
217     else {
218         sv = (SV*)&PL_sv_undef;
219         undef = "(undef)";
220     }
221
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 */
251                 (void)SvUPGRADE(sv, SVt_PVNV);
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));
269     }
270
271 finish:
272     ST(0) = sv;
273
274     XSRETURN(1);
275 }
276