Fix 'Int' type constraint for dualvars (like $!)
[gitmo/Mouse.git] / xs-src / MouseTypeConstraints.xs
index 096ce4a..f7e99a8 100644 (file)
@@ -1,15 +1,11 @@
 /*
- *   full definition of built-in type constraints (ware in Moose::Util::TypeConstraints::OptimizedConstraints)
+ * TypeConstraint stuff
+ *  - Mouse::Util::TypeConstraints (including OptimizedConstraionts)
+ *  - Mouse::Meta::TypeConstraint
  */
 
 #include "mouse.h"
 
-#if PERL_BCDVERSION >= 0x5008005
-#define LooksLikeNumber(sv) looks_like_number(sv)
-#else
-#define LooksLikeNumber(sv) ( SvPOKp(sv) ? looks_like_number(sv) : (I32)SvNIOKp(sv) )
-#endif
-
 #ifndef SvRXOK
 #define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr))
 #endif
@@ -74,21 +70,21 @@ mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
     assert(sv);
 
     if(sv_true(sv)){
-        if(SvIOKp(sv)){
+        if(SvPOKp(sv)){ /* "1" */
+            return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
+        }
+        else if(SvIOKp(sv)){
             return SvIVX(sv) == 1;
         }
         else if(SvNOKp(sv)){
             return SvNVX(sv) == 1.0;
         }
-        else if(SvPOKp(sv)){ /* "1" */
-            return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
-        }
         else{
             return FALSE;
         }
     }
     else{
-        /* any false value must be boolean */
+        /* any false value is a boolean */
         return TRUE;
     }
 }
@@ -144,18 +140,16 @@ S_nv_is_integer(pTHX_ NV const nv) {
 int
 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
     assert(sv);
-    if(SvIOKp(sv)){
+    if(SvPOKp(sv)){
+        int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
+        return num_type && !(num_type & IS_NUMBER_NOT_INT);
+    }
+    else if(SvIOKp(sv)){
         return TRUE;
     }
     else if(SvNOKp(sv)) {
         return S_nv_is_integer(aTHX_ SvNVX(sv));
     }
-    else if(SvPOKp(sv)){
-        int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
-        if(num_type){
-            return !(num_type & IS_NUMBER_NOT_INT);
-        }
-    }
     return FALSE;
 }
 
@@ -166,7 +160,7 @@ mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
 }
 
 int
-mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){ 
+mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){
     assert(sv);
     return is_class_loaded(sv);
 }
@@ -553,16 +547,26 @@ XS(XS_Mouse_constraint_check) {
     dVAR;
     dXSARGS;
     MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
+    SV* sv;
 
     if(items < 1){
         croak("Too few arguments for type constraint check functions");
     }
 
-    SvGETMAGIC( ST(0) );
-    ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
+    sv = ST(0);
+    SvGETMAGIC(sv);
+    ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv) );
     XSRETURN(1);
 }
 
+XS(XS_Mouse_TypeConstraint_fallback); /* -Wmissing-prototypes */
+XS(XS_Mouse_TypeConstraint_fallback) {
+    dXSARGS;
+    PERL_UNUSED_VAR(cv);
+    PERL_UNUSED_VAR(items);
+    XSRETURN_EMPTY;
+}
+
 static void
 setup_my_cxt(pTHX_ pMY_CXT){
     MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
@@ -574,6 +578,8 @@ setup_my_cxt(pTHX_ pMY_CXT){
 
 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
 
+#define MTC_CLASS "Mouse::Meta::TypeConstraint"
+
 MODULE = Mouse::Util::TypeConstraints    PACKAGE = Mouse::Util::TypeConstraints
 
 PROTOTYPES:   DISABLE
@@ -667,6 +673,56 @@ BOOT:
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */
 
+    /* overload stuff */
+    PL_amagic_generation++;
+    (void)newXS( MTC_CLASS "::()",
+        XS_Mouse_TypeConstraint_fallback, file);
+
+    /* fallback => 1 */
+    sv_setsv(
+        get_sv( MTC_CLASS "::()", GV_ADD ),
+        &PL_sv_yes
+    );
+
+    /* '""' => '_as_string' */
+    {
+        SV* const code_ref = sv_2mortal(newRV_inc(
+            (SV*)get_cv( MTC_CLASS "::_as_string", GV_ADD )));
+        sv_setsv_mg(
+            (SV*)gv_fetchpvs( MTC_CLASS "::(\"\"", GV_ADDMULTI, SVt_PVCV ),
+            code_ref );
+    }
+
+    /* '0+' => '_identity' */
+    {
+        SV* const code_ref = sv_2mortal(newRV_inc(
+            (SV*)get_cv( MTC_CLASS "::_identity", GV_ADD )));
+        sv_setsv_mg(
+            (SV*)gv_fetchpvs( MTC_CLASS "::(0+", GV_ADDMULTI, SVt_PVCV ),
+            code_ref );
+    }
+
+    /* '|' => '_unite' */
+    {
+        SV* const code_ref = sv_2mortal(newRV_inc(
+            (SV*)get_cv( MTC_CLASS "::_unite", GV_ADD )));
+        sv_setsv_mg(
+            (SV*)gv_fetchpvs( MTC_CLASS "::(|", GV_ADDMULTI, SVt_PVCV ),
+            code_ref );
+    }
+
+UV
+_identity(SV* self, ...)
+CODE:
+{
+    if(!SvROK(self)) {
+        croak("Invalid object instance: '%"SVf"'", self);
+    }
+    RETVAL = PTR2UV(SvRV(self));
+}
+OUTPUT:
+    RETVAL
+
 void
 compile_type_constraint(SV* self)
 CODE: