Refactor type constraint check function to make class_type faster
[gitmo/Mouse.git] / xs-src / MouseAccessor.xs
index e398c58..ee7bf8d 100644 (file)
@@ -2,7 +2,7 @@
 
 #define CHECK_INSTANCE(instance) STMT_START{                          \
         if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
-            croak("Invalid object for instance managers");            \
+            croak("Invalid object instance");                         \
         }                                                             \
     } STMT_END
 
@@ -52,8 +52,6 @@ static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */
 
 SV*
 mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
-    SV* self;
-
     if(items < 1){
         croak("Too few arguments for %s", GvNAME(CvGV(cv)));
     }
@@ -62,11 +60,7 @@ mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
      *       before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
      */
 
-    self = ST(0);
-    if(!IsObject(self)){
-        croak("Cant call %s as a class method", GvNAME(CvGV(cv)));
-    }
-    return self;
+    return ST(0);
 }
 
 
@@ -168,18 +162,10 @@ mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
     }
 
     if(!SvOK(MOUSE_xa_tc_code(xa))){
-        XS(XS_Mouse__Util__TypeConstraints_Item); /* prototype defined in Mouse.xs */
-
         tc_code = mcall0s(tc, "_compiled_type_constraint");
-
-        if(SvROK(tc_code) && SvTYPE(SvRV(tc_code))
-            && CvXSUB((CV*)SvRV(tc_code)) == XS_Mouse__Util__TypeConstraints_Item){
-            /* built-in type constraints */
-            mouse_tc const id = CvXSUBANY((CV*)SvRV(tc_code)).any_i32;
-            av_store(xa, MOUSE_XA_TC_CODE, newSViv(id));
-        }
-        else{
-            av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
+        av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
+        if(!(SvROK(tc_code) && SvTYPE(SvRV(tc_code)) == SVt_PVCV)){
+            mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference");
         }
     }
     else{
@@ -475,7 +461,7 @@ XS(mouse_xs_simple_predicate)
     XSRETURN(1);
 }
 
-/* simple instance slot accessor */
+/* simple instance slot accessor (or Mouse::Meta::Instance) */
 
 SV*
 mouse_instance_create(pTHX_ HV* const stash) {