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)
21 if(strEQ(HvNAME(stash), name))
25 Perl_croak(aTHX_ "Recursive inheritance detected in package '%s'", HvNAME(stash));
27 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE);
29 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (hv = GvHV(gv))) {
31 SV** svp = (SV**)hv_fetch(hv, name, len, FALSE);
32 if (svp && (sv = *svp) != (SV*)&PL_sv_undef)
36 gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE);
38 if (gvp && (gv = *gvp) != (GV*)&PL_sv_undef && (av = GvAV(gv))) {
40 gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE);
44 if (SvTYPE(gv) != SVt_PVGV)
45 gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE);
50 SV** svp = AvARRAY(av);
51 /* NOTE: No support for tied ISA */
52 I32 items = AvFILLp(av) + 1;
55 HV* basestash = gv_stashsv(sv, FALSE);
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));
64 if(&PL_sv_yes == isa_lookup(basestash, name, len, level + 1)) {
65 (void)hv_store(hv,name,len,&PL_sv_yes,0);
69 (void)hv_store(hv,name,len,&PL_sv_no,0);
73 return boolSV(strEQ(name, "UNIVERSAL"));
77 Perl_sv_derived_from(pTHX_ SV *sv, const char *name)
91 type = sv_reftype(sv,0);
96 stash = gv_stashsv(sv, FALSE);
99 return (type && strEQ(type,name)) ||
100 (stash && isa_lookup(stash, name, strlen(name), 0) == &PL_sv_yes)
106 void XS_UNIVERSAL_isa(pTHXo_ CV *cv);
107 void XS_UNIVERSAL_can(pTHXo_ CV *cv);
108 void XS_UNIVERSAL_VERSION(pTHXo_ CV *cv);
111 Perl_boot_core_UNIVERSAL(pTHX)
113 char *file = __FILE__;
115 newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file);
116 newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file);
117 newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file);
130 Perl_croak(aTHX_ "Usage: UNIVERSAL::isa(reference, kind)");
133 name = (char *)SvPV(ST(1),n_a);
135 ST(0) = boolSV(sv_derived_from(sv, name));
149 Perl_croak(aTHX_ "Usage: UNIVERSAL::can(object-ref, method)");
152 name = (char *)SvPV(ST(1),n_a);
161 pkg = gv_stashsv(sv, FALSE);
165 GV *gv = gv_fetchmethod_autoload(pkg, name, FALSE);
167 rv = sv_2mortal(newRV((SV*)GvCV(gv)));
174 XS(XS_UNIVERSAL_VERSION)
185 sv = (SV*)SvRV(ST(0));
187 Perl_croak(aTHX_ "Cannot find version of an unblessed reference");
191 pkg = gv_stashsv(ST(0), FALSE);
194 gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**);
196 if (gvp && isGV(gv = *gvp) && SvOK(sv = GvSV(gv))) {
197 SV *nsv = sv_newmortal();
203 sv = (SV*)&PL_sv_undef;
207 if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv)))) {
209 Perl_croak(aTHX_ "%s version %s required--this is only version %s",
210 HvNAME(pkg), SvPV(ST(1),n_a), undef ? undef : SvPV(sv,n_a));