Typo fix
[gitmo/Class-MOP.git] / MOP.xs
diff --git a/MOP.xs b/MOP.xs
index bf5b6a4..44ebb0a 100644 (file)
--- a/MOP.xs
+++ b/MOP.xs
@@ -9,45 +9,83 @@ This shuts up warnings from gcc -Wall
 #include "perl.h"
 #include "XSUB.h"
 
+#define NEED_newRV_noinc
 #define NEED_sv_2pv_flags
 #define NEED_sv_2pv_nolen
 #include "ppport.h"
 
-SV *key_name;
-U32 hash_name;
+#define DECLARE_KEY(name) SV *key_##name; U32 hash_##name;
 
-SV *key_package;
-U32 hash_package;
+#define PREHASH_KEY_WITH_VALUE(name, value) do { \
+    key_##name = newSVpvs(value); \
+    PERL_HASH(hash_##name, value, sizeof(value) - 1); \
+} while (0)
 
-SV *key_package_name;
-U32 hash_package_name;
+/* this is basically the same as the above macro, except that the value will be
+ * the stringified name. However, we can't just implement this in terms of
+ * PREHASH_KEY_WITH_VALUE as that'd cause macro expansion on the value of
+ * 'name' when it's being passed to the other macro. suggestions on how to make
+ * this more elegant would be much appreciated */
 
-SV *key_body;
-U32 hash_body;
+#define PREHASH_KEY(name) do { \
+    key_##name = newSVpvs(#name); \
+    PERL_HASH(hash_##name, #name, sizeof(#name) - 1); \
+} while (0)
 
-SV* method_metaclass;
-SV* associated_metaclass;
-SV* wrap;
+DECLARE_KEY(name);
+DECLARE_KEY(package);
+DECLARE_KEY(package_name);
+DECLARE_KEY(body);
+DECLARE_KEY(package_cache_flag);
+DECLARE_KEY(methods);
+DECLARE_KEY(VERSION);
+DECLARE_KEY(ISA);
 
+SV *method_metaclass;
+SV *associated_metaclass;
+SV *wrap;
 
-#define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
-#ifdef HvMROMETA /* 5.10.0 */
 
-#ifndef mro_meta_init
-#define mro_meta_init(stash) Perl_mro_meta_init(aTHX_ stash) /* used in HvMROMETA macro */
-#endif /* !mro_meta_init */
+#define check_package_cache_flag(stash) mop_check_package_cache_flag(aTHX_ stash)
+#if PERL_VERSION >= 10
 
 static UV
 mop_check_package_cache_flag(pTHX_ HV* stash) {
     assert(SvTYPE(stash) == SVt_PVHV);
 
-    return HvMROMETA(stash)->pkg_gen; /* mro::get_pkg_gen($pkg) */
+    /* here we're trying to implement a c version of mro::get_pkg_gen($stash),
+     * however the perl core doesn't make it easy for us. It doesn't provide an
+     * api that just does what we want.
+     *
+     * However, we know that the information we want is, inside the core,
+     * available using HvMROMETA(stash)->pkg_gen. Unfortunately, although the
+     * HvMROMETA macro is public, it is implemented using Perl_mro_meta_init,
+     * which is not public and only available inside the core, as the mro
+     * interface as well as the structure returned by mro_meta_init isn't
+     * considered to be stable yet.
+     *
+     * Perl_mro_meta_init isn't declared static, so we could just define it
+     * ourselfs if perls headers don't do that for us, except that won't work
+     * on platforms where symbols need to be explicitly exported when linking
+     * shared libraries.
+     *
+     * So our, hopefully temporary, solution is to be even more evil and
+     * basically reimplement HvMROMETA in a very fragile way that'll blow up
+     * when the relevant parts of the mro implementation in core change.
+     *
+     * :-(
+     *
+     */
+
+    return HvAUX(stash)->xhv_mro_meta
+         ? HvAUX(stash)->xhv_mro_meta->pkg_gen
+         : 0;
 }
 
 #else /* pre 5.10.0 */
 
 static UV
-mop_check_package_cache_flag(pTHX_ HV* stash) {
+mop_check_package_cache_flag(pTHX_ HV *stash) {
     PERL_UNUSED_ARG(stash);
     assert(SvTYPE(stash) == SVt_PVHV);
 
@@ -56,10 +94,10 @@ mop_check_package_cache_flag(pTHX_ HV* stash) {
 #endif
 
 #define call0(s, m)  mop_call0(aTHX_ s, m)
-static SV*
-mop_call0(pTHX_ SV* const self, SV* const method) {
+static SV *
+mop_call0(pTHX_ SV *const self, SV *const method) {
     dSP;
-    SV* ret;
+    SV *ret;
 
     PUSHMARK(SP);
     XPUSHs(self);
@@ -74,97 +112,31 @@ mop_call0(pTHX_ SV* const self, SV* const method) {
     return ret;
 }
 
-static void
-mop_update_method_map(pTHX_ SV* const self, SV* const class_name, HV* const stash, HV* const map) {
-    const char* const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
-    SV*   method_metaclass_name;
-    char* method_name;
-    I32   method_name_len;
-    GV* gv;
-    dSP;
-
-    /* this function massivly overlaps with the xs version of
-     * get_all_package_symbols. a common c function to walk the symbol table
-     * should be factored out and used by both.  --rafl */
+static int
+get_code_info (SV *coderef, char **pkg, char **name)
+{
+    if (!SvOK(coderef) || !SvROK(coderef) || SvTYPE(SvRV(coderef)) != SVt_PVCV) {
+        return 0;
+    }
 
-    hv_iterinit(stash);
-    while ( (gv = (GV*)hv_iternextsv(stash, &method_name, &method_name_len)) ) {
-        CV* cv;
-        switch (SvTYPE (gv)) {
-#ifndef SVt_RV
-            case SVt_RV:
+    coderef = SvRV(coderef);
+    /* I think this only gets triggered with a mangled coderef, but if
+       we hit it without the guard, we segfault. The slightly odd return
+       value strikes me as an improvement (mst)
+    */
+#ifdef isGV_with_GP
+    if ( isGV_with_GP(CvGV(coderef)) ) {
 #endif
-            case SVt_IV:
-            case SVt_PV:
-                /* rafl says that this wastes memory savings that GvSVs have
-                   in 5.8.9 and 5.10.x. But without it some tests fail. rafl
-                   says the right thing to do is to handle GvSVs differently
-                   here. */
-                gv_init((GV*)gv, stash, method_name, method_name_len, GV_ADDMULTI);
-                /* fall through */
-            default:
-                break;
-        }
-
-        if ( SvTYPE(gv) == SVt_PVGV && (cv = GvCVu(gv)) ) {
-            GV* const cvgv = CvGV(cv);
-            /* ($cvpkg_name, $cv_name) = get_code_info($cv) */
-            const char* const cvpkg_name = HvNAME(GvSTASH(cvgv));
-            const char* const cv_name    = GvNAME(cvgv);
-            SV* method_slot;
-            SV* method_object;
-
-            /* this checks to see that the subroutine is actually from our package  */
-            if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
-                if ( strNE(cvpkg_name, class_name_pv) ) {
-                    continue;
-                }
-            }
-
-            method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
-            if ( SvOK(method_slot) ) {
-                SV* const body = call0(method_slot, key_body); /* $method_object->body() */
-                if ( SvROK(body) && ((CV*) SvRV(body)) == cv ) {
-                    continue;
-                }
-            }
-
-            method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
-
-            /*
-                $method_object = $method_metaclass->wrap(
-                    $cv,
-                    associated_metaclass => $self,
-                    package_name         => $class_name,
-                    name                 => $method_name
-                );
-            */
-            ENTER;
-            SAVETMPS;
-
-            PUSHMARK(SP);
-            EXTEND(SP, 8);
-            PUSHs(method_metaclass_name); /* invocant */
-            mPUSHs(newRV_inc((SV*)cv));
-            PUSHs(associated_metaclass);
-            PUSHs(self);
-            PUSHs(key_package_name);
-            PUSHs(class_name);
-            PUSHs(key_name);
-            mPUSHs(newSVpv(method_name, method_name_len));
-            PUTBACK;
-
-            call_sv(wrap, G_SCALAR | G_METHOD);
-            SPAGAIN;
-            method_object = POPs;
-            PUTBACK;
-            /* $map->{$method_name} = $method_object */
-            sv_setsv(method_slot, method_object);
-
-            FREETMPS;
-            LEAVE;
-        }
+        *pkg     = HvNAME( GvSTASH(CvGV(coderef)) );
+        *name    = GvNAME( CvGV(coderef) );
+#ifdef isGV_with_GP
+    } else {
+        *pkg     = "__UNKNOWN__";
+        *name    = "__ANON__";
     }
+#endif
+
+    return 1;
 }
 
 typedef enum {
@@ -188,7 +160,9 @@ get_all_package_symbols(HV *stash, type_filter_t filter)
         while ( (he = hv_iternext(stash)) ) {
             STRLEN keylen;
             char *key = HePV(he, keylen);
-            hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0);
+            if (!hv_store(ret, key, keylen, SvREFCNT_inc(HeVAL(he)), 0)) {
+                croak("failed to store glob ref");
+            }
         }
 
         return ret;
@@ -243,13 +217,92 @@ get_all_package_symbols(HV *stash, type_filter_t filter)
 
         if (sv) {
             char *key = HePV(he, keylen);
-            hv_store(ret, key, keylen, newRV_inc(sv), 0);
+            if (!hv_store(ret, key, keylen, newRV_inc(sv), 0)) {
+                croak("failed to store symbol ref");
+            }
         }
     }
 
     return ret;
 }
 
+
+static void
+mop_update_method_map(pTHX_ SV *const self, SV *const class_name, HV *const stash, HV *const map) {
+    const char *const class_name_pv = HvNAME(stash); /* must be HvNAME(stash), not SvPV_nolen_const(class_name) */
+    SV   *method_metaclass_name;
+    char *method_name;
+    I32   method_name_len;
+    SV   *coderef;
+    HV   *symbols;
+    dSP;
+
+    symbols = get_all_package_symbols(stash, TYPE_FILTER_CODE);
+
+    (void)hv_iterinit(symbols);
+    while ( (coderef = hv_iternextsv(symbols, &method_name, &method_name_len)) ) {
+        CV *cv = (CV *)SvRV(coderef);
+        char *cvpkg_name;
+        char *cv_name;
+        SV *method_slot;
+        SV *method_object;
+
+        if (!get_code_info(coderef, &cvpkg_name, &cv_name)) {
+            continue;
+        }
+
+        /* this checks to see that the subroutine is actually from our package  */
+        if ( !(strEQ(cvpkg_name, "constant") && strEQ(cv_name, "__ANON__")) ) {
+            if ( strNE(cvpkg_name, class_name_pv) ) {
+                continue;
+            }
+        }
+
+        method_slot = *hv_fetch(map, method_name, method_name_len, TRUE);
+        if ( SvOK(method_slot) ) {
+            SV *const body = call0(method_slot, key_body); /* $method_object->body() */
+            if ( SvROK(body) && ((CV *) SvRV(body)) == cv ) {
+                continue;
+            }
+        }
+
+        method_metaclass_name = call0(self, method_metaclass); /* $self->method_metaclass() */
+
+        /*
+            $method_object = $method_metaclass->wrap(
+                $cv,
+                associated_metaclass => $self,
+                package_name         => $class_name,
+                name                 => $method_name
+            );
+        */
+        ENTER;
+        SAVETMPS;
+
+        PUSHMARK(SP);
+        EXTEND(SP, 8);
+        PUSHs(method_metaclass_name); /* invocant */
+        mPUSHs(newRV_inc((SV *)cv));
+        PUSHs(associated_metaclass);
+        PUSHs(self);
+        PUSHs(key_package_name);
+        PUSHs(class_name);
+        PUSHs(key_name);
+        mPUSHs(newSVpv(method_name, method_name_len));
+        PUTBACK;
+
+        call_sv(wrap, G_SCALAR | G_METHOD);
+        SPAGAIN;
+        method_object = POPs;
+        PUTBACK;
+        /* $map->{$method_name} = $method_object */
+        sv_setsv(method_slot, method_object);
+
+        FREETMPS;
+        LEAVE;
+    }
+}
+
 /*
 get_code_info:
   Pass in a coderef, returns:
@@ -260,54 +313,85 @@ get_code_info:
 MODULE = Class::MOP   PACKAGE = Class::MOP
 
 BOOT:
-    key_name = newSVpvs("name");
-    key_body = newSVpvs("body");
-    key_package = newSVpvs("package");
-    key_package_name = newSVpvs("package_name");
-
-    PERL_HASH(hash_name, "name", 4);
-    PERL_HASH(hash_body, "body", 4);
-    PERL_HASH(hash_package, "package", 7);
-    PERL_HASH(hash_package_name, "package_name", 12);
+    PREHASH_KEY(name);
+    PREHASH_KEY(body);
+    PREHASH_KEY(package);
+    PREHASH_KEY(package_name);
+    PREHASH_KEY(methods);
+    PREHASH_KEY(ISA);
+    PREHASH_KEY(VERSION);
+    PREHASH_KEY_WITH_VALUE(package_cache_flag, "_package_cache_flag");
 
     method_metaclass     = newSVpvs("method_metaclass");
     wrap                 = newSVpvs("wrap");
     associated_metaclass = newSVpvs("associated_metaclass");
 
 
-PROTOTYPES: ENABLE
-
+PROTOTYPES: DISABLE
 
+# use prototype here to be compatible with get_code_info from Sub::Identify
 void
 get_code_info(coderef)
-  SV* coderef
-  PREINIT:
-    char* name;
-    char* pkg;
-  PPCODE:
-    if ( SvOK(coderef) && SvROK(coderef) && SvTYPE(SvRV(coderef)) == SVt_PVCV ) {
-      coderef = SvRV(coderef);
-      /* I think this only gets triggered with a mangled coderef, but if
-         we hit it without the guard, we segfault. The slightly odd return
-         value strikes me as an improvement (mst)
-      */
-#ifdef isGV_with_GP
-      if ( isGV_with_GP(CvGV(coderef)) ) {
-#endif
-        pkg     = HvNAME( GvSTASH(CvGV(coderef)) );
-        name    = GvNAME( CvGV(coderef) );
-#ifdef isGV_with_GP
-      } else {
-        pkg     = "__UNKNOWN__";
-        name    = "__ANON__";
-      }
-#endif
+    SV *coderef
+    PROTOTYPE: $
+    PREINIT:
+        char *pkg  = NULL;
+        char *name = NULL;
+    PPCODE:
+        if (get_code_info(coderef, &pkg, &name)) {
+            EXTEND(SP, 2);
+            PUSHs(newSVpv(pkg, 0));
+            PUSHs(newSVpv(name, 0));
+        }
 
-      EXTEND(SP, 2);
-      PUSHs(newSVpvn(pkg, strlen(pkg)));
-      PUSHs(newSVpvn(name, strlen(name)));
-    }
+void
+is_class_loaded(klass=&PL_sv_undef)
+    SV *klass
+    PREINIT:
+        HV *stash;
+        char *key;
+        I32 keylen;
+        GV *gv;
+    PPCODE:
+        if (!SvPOK(klass) || !SvCUR(klass)) {
+            XSRETURN_NO;
+        }
 
+        stash = gv_stashsv(klass, 0);
+        if (!stash) {
+            XSRETURN_NO;
+        }
+
+        if (hv_exists_ent (stash, key_VERSION, hash_VERSION)) {
+            HE *version = hv_fetch_ent(stash, key_VERSION, 0, hash_VERSION);
+            if (version && HeVAL(version) && GvSV(HeVAL(version))) {
+                XSRETURN_YES;
+            }
+        }
+
+        if (hv_exists_ent (stash, key_ISA, hash_ISA)) {
+            HE *isa = hv_fetch_ent(stash, key_ISA, 0, hash_ISA);
+            if (isa && HeVAL(isa) && GvAV(HeVAL(isa))) {
+                XSRETURN_YES;
+            }
+        }
+
+        (void)hv_iterinit(stash);
+        while ((gv = (GV *)hv_iternextsv(stash, &key, &keylen))) {
+            if (keylen <= 0) {
+                continue;
+            }
+
+            if (key[keylen - 1] == ':' && key[keylen - 2] == ':') {
+                continue;
+            }
+
+            if (!isGV(gv) || GvCV(gv) || GvSV(gv) || GvAV(gv) || GvHV(gv) || GvIO(gv) || GvFORM(gv)) {
+                XSRETURN_YES;
+            }
+        }
+
+        XSRETURN_NO;
 
 MODULE = Class::MOP   PACKAGE = Class::MOP::Package
 
@@ -315,7 +399,6 @@ void
 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
     SV *self
     type_filter_t filter
-    PROTOTYPE: $;$
     PREINIT:
         HV *stash = NULL;
         HV *symbols = NULL;
@@ -451,35 +534,25 @@ MODULE = Class::MOP    PACKAGE = Class::MOP::Class
 
 void
 get_method_map(self)
-    SV* self
+    SV *self
     PREINIT:
-        SV* const class_name = HeVAL( hv_fetch_ent((HV*)SvRV(self), key_package, TRUE, hash_package) );
-        HV* const stash      = gv_stashsv(class_name, TRUE);
+        HV *const obj        = (HV *)SvRV(self);
+        SV *const class_name = HeVAL( hv_fetch_ent(obj, key_package, 0, hash_package) );
+        HV *const stash      = gv_stashsv(class_name, 0);
         UV  const current    = check_package_cache_flag(stash);
-        SV* const cache_flag = *hv_fetchs((HV*)SvRV(self), "_package_cache_flag", TRUE);
-        SV* const map_ref    = *hv_fetchs((HV*)SvRV(self), "methods", TRUE);
+        SV *const cache_flag = HeVAL( hv_fetch_ent(obj, key_package_cache_flag, TRUE, hash_package_cache_flag));
+        SV *const map_ref    = HeVAL( hv_fetch_ent(obj, key_methods, TRUE, hash_methods));
     PPCODE:
-        if ( ! SvRV(self) ) {
-            die("Cannot call get_method_map as a class method");
-        }
-
         /* in  $self->{methods} does not yet exist (or got deleted) */
-        if ( ! (SvROK(map_ref) && SvTYPE(SvRV(map_ref)) == SVt_PVHV) ) {
-            SV* new_map_ref = newRV_noinc((SV*)newHV());
+        if ( !SvROK(map_ref) || SvTYPE(SvRV(map_ref)) != SVt_PVHV ) {
+            SV *new_map_ref = newRV_noinc((SV *)newHV());
             sv_2mortal(new_map_ref);
             sv_setsv(map_ref, new_map_ref);
         }
 
-        if ( ! (SvOK(cache_flag) && SvUV(cache_flag) == current) ) {
-            ENTER;
-            SAVETMPS;
-
-            mop_update_method_map(aTHX_ self, class_name, stash, (HV*)SvRV(map_ref));
+        if ( !SvOK(cache_flag) || SvUV(cache_flag) != current ) {
+            mop_update_method_map(aTHX_ self, class_name, stash, (HV *)SvRV(map_ref));
             sv_setuv(cache_flag, check_package_cache_flag(stash)); /* update_cache_flag() */
-
-            FREETMPS;
-            LEAVE;
         }
 
         XPUSHs(map_ref);
-