Use GvCV_set() for bleadperl
[gitmo/Mouse.git] / xs-src / MouseUtil.xs
index aba91be..3ec7d8c 100644 (file)
@@ -112,14 +112,65 @@ mouse_throw_error(SV* const metaobject, SV* const data /* not used */, const cha
             mPUSHs(newSVpvs("depth"));
             mPUSHi(-1);
         }
-
         PUTBACK;
-
-        call_method("throw_error", G_VOID);
+        if(SvOK(metaobject)) {
+            call_method("throw_error", G_VOID);
+        }
+        else {
+            call_pv("Mouse::Util::throw_error", G_VOID);
+        }
         croak("throw_error() did not throw the error (%"SVf")", message);
     }
 }
 
+/* workaround Perl-RT #69939 */
+I32
+mouse_call_sv_safe(pTHX_ SV* const sv, I32 const flags) {
+    I32 count;
+    ENTER;
+    /* Don't do SAVETMPS */
+
+    SAVESPTR(ERRSV);
+    ERRSV = sv_newmortal();
+
+    count = Perl_call_sv(aTHX_ sv, flags | G_EVAL);
+
+    if(sv_true(ERRSV)){
+        SV* const err = sv_mortalcopy(ERRSV);
+        LEAVE;
+        sv_setsv(ERRSV, err);
+        croak(NULL); /* rethrow */
+    }
+
+    LEAVE;
+
+    return count;
+}
+
+void
+mouse_must_defined(pTHX_ SV* const value, const char* const name) {
+    assert(value);
+    assert(name);
+
+    SvGETMAGIC(value);
+    if(!SvOK(value)){
+        croak("You must define %s", name);
+    }
+}
+
+void
+mouse_must_ref(pTHX_ SV* const value, const char* const name, svtype const t) {
+    assert(value);
+    assert(name);
+
+    SvGETMAGIC(value);
+    if(!(SvROK(value) && (t == SVt_NULL || SvTYPE(SvRV(value)) == t))) {
+        croak("You must pass %s, not %s",
+            name, SvOK(value) ? SvPV_nolen(value) : "undef");
+    }
+}
+
+
 bool
 mouse_is_class_loaded(pTHX_ SV * const klass){
     HV *stash;
@@ -152,11 +203,13 @@ mouse_is_class_loaded(pTHX_ SV * const klass){
         GV* const gv = (GV*)HeVAL(he);
 
         if(isGV(gv)){
-            if(GvCVu(gv)){
+            if(GvCVu(gv)){ /* is GV and has CV */
+                hv_iterinit(stash); /* reset */
                 return TRUE;
             }
         }
-        else if(SvOK(gv)){
+        else if(SvOK(gv)){ /* is a stub or constant */
+            hv_iterinit(stash); /* reset */
             return TRUE;
         }
     }
@@ -173,7 +226,7 @@ mouse_call0 (pTHX_ SV* const self, SV* const method) {
     XPUSHs(self);
     PUTBACK;
 
-    call_sv(method, G_SCALAR | G_METHOD);
+    call_sv_safe(method, G_SCALAR | G_METHOD);
 
     SPAGAIN;
     ret = POPs;
@@ -193,7 +246,7 @@ mouse_call1 (pTHX_ SV* const self, SV* const method, SV* const arg1) {
     PUSHs(arg1);
     PUTBACK;
 
-    call_sv(method, G_SCALAR | G_METHOD);
+    call_sv_safe(method, G_SCALAR | G_METHOD);
 
     SPAGAIN;
     ret = POPs;
@@ -270,8 +323,9 @@ mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) {
 
     if(GvCVu(gv)){ /* delete *slot{gv} to work around "redefine" warning */
         SvREFCNT_dec(GvCV(gv));
-        GvCV(gv) = NULL;
+        GvCV_set(gv, NULL);
     }
+
     sv_setsv_mg((SV*)gv, code_ref); /* *gv = $code_ref */
 
     /* name the CODE ref if it's anonymous */
@@ -297,7 +351,7 @@ mouse_install_sub(pTHX_ GV* const gv, SV* const code_ref) {
             }
         }
 
-        CvGV(cv) = gv;
+        CvGV_set(cv, gv);
         CvANON_off(cv);
     }
 }
@@ -323,7 +377,9 @@ CODE:
     }
     {
         dMY_CXT;
-        if(MY_CXT.metas) croak("Cannot set metaclass storage more than once");
+        if(MY_CXT.metas && ckWARN(WARN_REDEFINE)){
+            Perl_warner(aTHX_ packWARN(WARN_REDEFINE), "Metaclass storage more than once");
+        }
         MY_CXT.metas = metas;
         SvREFCNT_inc_simple_void_NN(metas);
     }
@@ -390,12 +446,8 @@ CODE:
     const char* name_pv;
     GV* gv;
 
-    if(!SvOK(package)){
-        croak("You must define %s", "a package name");
-    }
-    if(!SvOK(name)){
-        croak("You must define %s", "a subroutine name");
-    }
+    must_defined(package, "a package name");
+    must_defined(name,    "a subroutine name");
 
     stash = gv_stashsv(package, FALSE);
     if(!stash){
@@ -423,17 +475,10 @@ PPCODE:
     const char* name_pv = NULL;
     CV* xsub;
 
-    SvGETMAGIC(arg);
-
-    if(!SvOK(arg)){
-        croak("You must define %s", ix == 0 ? "a class name" : "method names");
-    }
+    must_defined(arg, ix == 0 ? "a class_name" : "method names");
 
     if(predicate_name){
-        SvGETMAGIC(predicate_name);
-        if(!SvOK(predicate_name)){
-            croak("You must define %s", "a predicate name");
-        }
+        must_defined(predicate_name, "a predicate name");
         name_pv = SvPV_nolen_const(predicate_name);
     }
 
@@ -457,10 +502,7 @@ CODE:
     HV* stash;
     I32 i;
 
-    SvGETMAGIC(into);
-    if(!SvOK(into)){
-        croak("You must define %s", "a package name");
-    }
+    must_defined(into, "a package name");
     stash = gv_stashsv(into, TRUE);
 
     if( ((items-1) % 2) != 0 ){
@@ -474,14 +516,8 @@ CODE:
         const char* pv;
         GV* gv;
 
-        SvGETMAGIC(name);
-        if(!SvOK(name)){
-            croak("You must define %s", "a subroutine name");
-        }
-        SvGETMAGIC(code);
-        if(!IsCodeRef(code)){
-            croak("You must define %s", "a CODE reference");
-        }
+        must_defined(name, "a subroutine name");
+        must_ref(code, "a CODE reference", SVt_PVCV);
 
         pv = SvPV_const(name, len);
         gv = stash_fetch(stash, pv, len, TRUE);