| 1 | #include "EXTERN.h" |
| 2 | #include "perl.h" |
| 3 | #include "XSUB.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 | isa_lookup(stash, name, len, level) |
| 12 | HV *stash; |
| 13 | char *name; |
| 14 | int len; |
| 15 | int level; |
| 16 | { |
| 17 | AV* av; |
| 18 | GV* gv; |
| 19 | GV** gvp; |
| 20 | HV* hv = Nullhv; |
| 21 | |
| 22 | if (!stash) |
| 23 | return &sv_undef; |
| 24 | |
| 25 | if(strEQ(HvNAME(stash), name)) |
| 26 | return &sv_yes; |
| 27 | |
| 28 | if (level > 100) |
| 29 | croak("Recursive inheritance detected"); |
| 30 | |
| 31 | gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, FALSE); |
| 32 | |
| 33 | if (gvp && (gv = *gvp) != (GV*)&sv_undef && (hv = GvHV(gv))) { |
| 34 | SV* sv; |
| 35 | SV** svp = (SV**)hv_fetch(hv, name, len, FALSE); |
| 36 | if (svp && (sv = *svp) != (SV*)&sv_undef) |
| 37 | return sv; |
| 38 | } |
| 39 | |
| 40 | gvp = (GV**)hv_fetch(stash,"ISA",3,FALSE); |
| 41 | |
| 42 | if (gvp && (gv = *gvp) != (GV*)&sv_undef && (av = GvAV(gv))) { |
| 43 | if(!hv) { |
| 44 | gvp = (GV**)hv_fetch(stash, "::ISA::CACHE::", 14, TRUE); |
| 45 | |
| 46 | gv = *gvp; |
| 47 | |
| 48 | if (SvTYPE(gv) != SVt_PVGV) |
| 49 | gv_init(gv, stash, "::ISA::CACHE::", 14, TRUE); |
| 50 | |
| 51 | hv = GvHVn(gv); |
| 52 | } |
| 53 | if(hv) { |
| 54 | SV** svp = AvARRAY(av); |
| 55 | I32 items = AvFILL(av) + 1; |
| 56 | while (items--) { |
| 57 | SV* sv = *svp++; |
| 58 | HV* basestash = gv_stashsv(sv, FALSE); |
| 59 | if (!basestash) { |
| 60 | if (dowarn) |
| 61 | warn("Can't locate package %s for @%s::ISA", |
| 62 | SvPVX(sv), HvNAME(stash)); |
| 63 | continue; |
| 64 | } |
| 65 | if(&sv_yes == isa_lookup(basestash, name, len, level + 1)) { |
| 66 | (void)hv_store(hv,name,len,&sv_yes,0); |
| 67 | return &sv_yes; |
| 68 | } |
| 69 | } |
| 70 | (void)hv_store(hv,name,len,&sv_no,0); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | return &sv_no; |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | sv_derived_from(sv, name) |
| 79 | SV * sv ; |
| 80 | char * name ; |
| 81 | { |
| 82 | SV *rv; |
| 83 | char *type; |
| 84 | HV *stash; |
| 85 | |
| 86 | stash = Nullhv; |
| 87 | type = Nullch; |
| 88 | |
| 89 | if (SvGMAGICAL(sv)) |
| 90 | mg_get(sv) ; |
| 91 | |
| 92 | if (SvROK(sv)) { |
| 93 | sv = SvRV(sv); |
| 94 | type = sv_reftype(sv,0); |
| 95 | if(SvOBJECT(sv)) |
| 96 | stash = SvSTASH(sv); |
| 97 | } |
| 98 | else { |
| 99 | stash = gv_stashsv(sv, FALSE); |
| 100 | } |
| 101 | |
| 102 | return (type && strEQ(type,name)) || |
| 103 | (stash && isa_lookup(stash, name, strlen(name), 0) == &sv_yes) |
| 104 | ? TRUE |
| 105 | : FALSE ; |
| 106 | |
| 107 | } |
| 108 | |
| 109 | |
| 110 | static |
| 111 | XS(XS_UNIVERSAL_isa) |
| 112 | { |
| 113 | dXSARGS; |
| 114 | SV *sv; |
| 115 | char *name; |
| 116 | |
| 117 | if (items != 2) |
| 118 | croak("Usage: UNIVERSAL::isa(reference, kind)"); |
| 119 | |
| 120 | sv = ST(0); |
| 121 | name = (char *)SvPV(ST(1),na); |
| 122 | |
| 123 | ST(0) = (sv_derived_from(sv, name) ? &sv_yes : &sv_no) ; |
| 124 | |
| 125 | XSRETURN(1); |
| 126 | } |
| 127 | |
| 128 | static |
| 129 | XS(XS_UNIVERSAL_can) |
| 130 | { |
| 131 | dXSARGS; |
| 132 | SV *sv; |
| 133 | char *name; |
| 134 | SV *rv; |
| 135 | GV *gv; |
| 136 | CV *cvp; |
| 137 | HV *pkg = NULL; |
| 138 | |
| 139 | if (items != 2) |
| 140 | croak("Usage: UNIVERSAL::can(object-ref, method)"); |
| 141 | |
| 142 | sv = ST(0); |
| 143 | name = (char *)SvPV(ST(1),na); |
| 144 | rv = &sv_undef; |
| 145 | |
| 146 | if(SvROK(sv)) { |
| 147 | sv = (SV*)SvRV(sv); |
| 148 | if(SvOBJECT(sv)) |
| 149 | pkg = SvSTASH(sv); |
| 150 | } |
| 151 | else { |
| 152 | pkg = gv_stashsv(sv, FALSE); |
| 153 | } |
| 154 | |
| 155 | if (pkg) { |
| 156 | gv = gv_fetchmethod(pkg, name); |
| 157 | |
| 158 | if(gv && GvCV(gv)) { |
| 159 | /* If the sub is only a stub then we may have a gv to AUTOLOAD */ |
| 160 | GV **gvp = (GV**)hv_fetch(GvSTASH(gv), name, strlen(name), TRUE); |
| 161 | if(gvp && (cvp = GvCV(*gvp))) { |
| 162 | rv = sv_newmortal(); |
| 163 | sv_setsv(rv, newRV((SV*)cvp)); |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | ST(0) = rv; |
| 169 | XSRETURN(1); |
| 170 | } |
| 171 | |
| 172 | static |
| 173 | XS(XS_UNIVERSAL_is_instance) |
| 174 | { |
| 175 | dXSARGS; |
| 176 | ST(0) = SvROK(ST(0)) ? &sv_yes : &sv_no; |
| 177 | XSRETURN(1); |
| 178 | } |
| 179 | |
| 180 | static |
| 181 | XS(XS_UNIVERSAL_class) |
| 182 | { |
| 183 | dXSARGS; |
| 184 | if(SvROK(ST(0)) && SvOBJECT(SvRV(ST(0)))) { |
| 185 | SV *sv = sv_newmortal(); |
| 186 | sv_setpv(sv, HvNAME(SvSTASH(SvRV(ST(0))))); |
| 187 | ST(0) = sv; |
| 188 | } |
| 189 | XSRETURN(1); |
| 190 | } |
| 191 | |
| 192 | static |
| 193 | XS(XS_UNIVERSAL_VERSION) |
| 194 | { |
| 195 | dXSARGS; |
| 196 | HV *pkg; |
| 197 | GV **gvp; |
| 198 | GV *gv; |
| 199 | SV *sv; |
| 200 | char *undef; |
| 201 | double req; |
| 202 | |
| 203 | if(SvROK(ST(0))) { |
| 204 | sv = (SV*)SvRV(ST(0)); |
| 205 | if(!SvOBJECT(sv)) |
| 206 | croak("Cannot find version of an unblessed reference"); |
| 207 | pkg = SvSTASH(sv); |
| 208 | } |
| 209 | else { |
| 210 | pkg = gv_stashsv(ST(0), FALSE); |
| 211 | } |
| 212 | |
| 213 | gvp = pkg ? (GV**)hv_fetch(pkg,"VERSION",7,FALSE) : Null(GV**); |
| 214 | |
| 215 | if (gvp && (gv = *gvp) != (GV*)&sv_undef && (sv = GvSV(gv))) { |
| 216 | SV *nsv = sv_newmortal(); |
| 217 | sv_setsv(nsv, sv); |
| 218 | sv = nsv; |
| 219 | undef = Nullch; |
| 220 | } |
| 221 | else { |
| 222 | sv = (SV*)&sv_undef; |
| 223 | undef = "(undef)"; |
| 224 | } |
| 225 | |
| 226 | if (items > 1 && (undef || (req = SvNV(ST(1)), req > SvNV(sv)))) |
| 227 | croak("%s version %s required--this is only version %s", |
| 228 | HvNAME(pkg), SvPV(ST(1),na), undef ? undef : SvPV(sv,na)); |
| 229 | |
| 230 | ST(0) = sv; |
| 231 | |
| 232 | XSRETURN(1); |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | boot_core_UNIVERSAL() |
| 237 | { |
| 238 | char *file = __FILE__; |
| 239 | |
| 240 | newXS("UNIVERSAL::isa", XS_UNIVERSAL_isa, file); |
| 241 | newXS("UNIVERSAL::can", XS_UNIVERSAL_can, file); |
| 242 | newXS("UNIVERSAL::class", XS_UNIVERSAL_class, file); |
| 243 | newXS("UNIVERSAL::is_instance", XS_UNIVERSAL_is_instance, file); |
| 244 | newXS("UNIVERSAL::VERSION", XS_UNIVERSAL_VERSION, file); |
| 245 | } |