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);
78 if (ckWARN(WARN_MISC))
79 Perl_warner(aTHX_ WARN_SYNTAX,
80 "Can't locate package %s for @%s::ISA",
81 SvPVX(sv), HvNAME(stash));
84 if (&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
85 (void)hv_store(hv,name,len,&PL_sv_yes,0);
89 (void)hv_store(hv,name,len,&PL_sv_no,0);
93 return boolSV(strEQ(name, "UNIVERSAL"));
97 =for apidoc sv_derived_from
99 Returns a boolean indicating whether the SV is derived from the specified
100 class. This is the function that implements C<UNIVERSAL::isa>. It works
101 for class names as well as for objects.
107 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
120 type = sv_reftype(sv,0);
125 stash = gv_stashsv(sv, FALSE);
128 return (type && strEQ(type,name)) ||
129 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
134 void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
135 void XS_UNIVERSAL_can(pTHXo_ CV *cv);
136 void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
139 Perl_boot_core_UNIVERSAL(pTHX)
141 char *file = __FILE__;
143 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
144 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
145 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
158 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
165 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
168 name = (char *)SvPV(ST(1),n_a);
170 ST(0) = boolSV(sv_derived_from(sv, name));
184 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
191 if (!SvOK(sv) || !(SvROK(sv) || (SvPOK(sv) && SvCUR(sv))))
194 name = (char *)SvPV(ST(1),n_a);
203 pkg = gv_stashsv(sv, FALSE);
207 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
209 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
216 XS(XS_UNIVERSAL_VERSION)
226 sv = (SV*)SvRV(ST(0));
228 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
232 pkg = gv_stashsv(ST(0), FALSE);
235 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
237 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
238 SV *nsv = sv_newmortal();
244 sv = (SV*)&PL_sv_undef;
253 Perl_croak(aTHX_ "%s does not define $%s::VERSION--version check failed",
254 HvNAME(pkg), HvNAME(pkg));
256 if (!SvNIOK(sv) && SvPOK(sv)) {
257 char *str = SvPVx(sv,len);
260 /* XXX could DWIM "1.2.3" here */
261 if (!isDIGIT(str[len]) && str[len] != '.' && str[len] != '_')
265 if (SvNOK(req) && SvPOK(req)) {
266 /* they said C<use Foo v1.2.3> and $Foo::VERSION
267 * doesn't look like a float: do string compare */
268 if (sv_cmp(req,sv) == 1) {
269 Perl_croak(aTHX_ "%s v%vd required--"
271 HvNAME(pkg), req, sv);
275 /* they said C<use Foo 1.002_003> and $Foo::VERSION
276 * doesn't look like a float: force numeric compare */
277 (void)SvUPGRADE(sv, SVt_PVNV);
278 SvNVX(sv) = str_to_version(sv);
283 /* if we get here, we're looking for a numeric comparison,
284 * so force the required version into a float, even if they
285 * said C<use Foo v1.2.3> */
286 if (SvNOK(req) && SvPOK(req)) {
288 req = sv_newmortal();
292 if (SvNV(req) > SvNV(sv))
293 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
294 HvNAME(pkg), SvPV(req,len), SvPV(sv,len));