2 #define PERL_IN_UNIVERSAL_C
6 * Contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
7 * The main guts of traverse_isa was actually copied from gv_fetchmeth
11 S_isa_lookup(pTHX_ HV *stash, const char *name, int len, int level)
22 if (strEQ(HvNAME(stash), name))
26 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'",
29 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
31 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (subgen = GvSV(gv))
34 if (SvIV(subgen) == PL_sub_generation) {
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)) );
44 DEBUG_o( Perl_deb(aTHX_ "ISA Cache in package %s is stale\n",
47 sv_setiv(subgen, PL_sub_generation);
51 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
53 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
55 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
59 if (SvTYPE(gv) != SVt_PVGV)
60 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
65 subgen = newSViv(PL_sub_generation);
70 SV** svp = AvARRAY(av);
71 /* NOTE: No support for tied ISA */
72 I32 items = AvFILLp(av) + 1;
75 HV* basestash = gv_stashsv(sv, FALSE);
77 if (ckWARN(WARN_MISC))
78 Perl_warner(aTHX_ WARN_SYNTAX,
79 "Can't locate package %s for @%s::ISA",
80 SvPVX(sv), HvNAME(stash));
83 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
84 (void)hv_store(hv,name,len,&PL_sv_yes,0);
88 (void)hv_store(hv,name,len,&PL_sv_no,0);
92 return boolSV(strEQ(name, "UNIVERSAL"));
96 =for apidoc sv_derived_from
98 Returns a boolean indicating whether the SV is derived from the specified
99 class. This is the function that implements C<UNIVERSAL::isa>. It works
100 for class names as well as for objects.
106 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
119 type = sv_reftype(sv,0);
124 stash = gv_stashsv(sv, FALSE);
127 return (type && strEQ(type,name)) ||
128 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
133 void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
134 void XS_UNIVERSAL_can(pTHXo_ CV *cv);
135 void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
138 Perl_boot_core_UNIVERSAL(pTHX)
140 char *file = __FILE__;
142 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
143 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
144 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
157 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
164 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
167 name = (char *)SvPV(ST(1),n_a);
169 ST(0) = boolSV(sv_derived_from(sv, name));
183 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
190 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
193 name = (char *)SvPV(ST(1),n_a);
202 pkg = gv_stashsv(sv, FALSE);
206 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
208 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
215 XS(XS_UNIVERSAL_VERSION)
225 sv = (SV*)SvRV(ST(0));
227 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
231 pkg = gv_stashsv(ST(0), FALSE);
234 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
236 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
237 SV *nsv = sv_newmortal();
243 sv = (SV*)&PL_sv_undef;
252 Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
253 HvNAME(pkg), HvNAME(pkg));
255 if (!SvNIOK(sv) && SvPOK(sv)) {
256 char *str = SvPVx(sv,len);
259 /* XXX could DWIM "1.2.3" here */
260 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
264 if (SvNOK(req) && SvPOK(req)) {
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) {
268 Perl_croak(aTHX_ "%s v%"VDf" required--"
269 "this is only v%"VDf,
270 HvNAME(pkg), req, sv);
274 /* they said C<use Foo 1.002_003> and $Foo::VERSION
275 * doesn't look like a float: force numeric compare */
276 (void)SvUPGRADE(sv, SVt_PVNV);
277 SvNVX(sv) = str_to_version(sv);
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> */
285 if (SvNOK(req) && SvPOK(req)) {
287 req = sv_newmortal();
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));