$tc->check into XS
[gitmo/Mouse.git] / xs-src / MouseTypeConstraints.xs
index a6e81ee..af6b592 100644 (file)
@@ -16,6 +16,9 @@
 
 typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv);
 
+/*
+    NOTE: mouse_tc_check() handles GETMAGIC
+*/
 int
 mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
     CV* const cv = (CV*)SvRV(tc_code);
@@ -27,6 +30,7 @@ mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
         assert(CvXSUBANY(cv).any_ptr != NULL);
         assert(mg->mg_ptr            != NULL);
 
+        SvGETMAGIC(sv);
         /* call the check function directly, skipping call_sv() */
         return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
     }
@@ -44,7 +48,7 @@ mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
         call_sv(tc_code, G_SCALAR);
 
         SPAGAIN;
-        ok = SvTRUEx(POPs);
+        ok = sv_true(POPs);
         PUTBACK;
 
         FREETMPS;
@@ -69,7 +73,7 @@ int
 mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
     assert(sv);
 
-    if(SvTRUE(sv)){
+    if(sv_true(sv)){
         if(SvIOKp(sv)){
             return SvIVX(sv) == 1;
         }
@@ -114,14 +118,37 @@ mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
 }
 
 int
+S_nv_is_integer(pTHX_ NV const nv) {
+    if(nv == (NV)(IV)nv){
+        return TRUE;
+    }
+    else {
+        char buf[64];  /* Must fit sprintf/Gconvert of longest NV */
+        char* p;
+        Gconvert(nv, NV_DIG, 0, buf);
+        p = &buf[0];
+
+        /* -?[0-9]+ */
+        if(*p == '-') p++;
+
+        while(*p){
+            if(!isDIGIT(*p)){
+                return FALSE;
+            }
+            p++;
+        }
+        return TRUE;
+    }
+}
+
+int
 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
     assert(sv);
     if(SvIOKp(sv)){
         return TRUE;
     }
-    else if(SvNOKp(sv)){
-        NV const nv = SvNVX(sv);
-        return nv > 0 ? (nv == (NV)(UV)nv) : (nv == (NV)(IV)nv);
+    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);
@@ -170,9 +197,13 @@ mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
 }
 
 int
-mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
+mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* sv) {
     assert(sv);
-    return SvROK(sv) && !SvOBJECT(SvRV(sv)) && (SvTYPE(SvRV(sv)) <= SVt_PVLV && !isGV(SvRV(sv)));
+    if(SvROK(sv)){
+         sv = SvRV(sv);
+         return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
+    }
+    return FALSE;
 }
 
 int
@@ -240,7 +271,6 @@ mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
         I32 i;
         for(i = 0; i < len; i++){
             SV* const value = *av_fetch(av, i, TRUE);
-            SvGETMAGIC(value);
             if(!mouse_tc_check(aTHX_ param, value)){
                 return FALSE;
             }
@@ -259,7 +289,6 @@ mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
         hv_iterinit(hv);
         while((he = hv_iternext(hv))){
             SV* const value = hv_iterval(hv, he);
-            SvGETMAGIC(value);
             if(!mouse_tc_check(aTHX_ param, value)){
                 hv_iterinit(hv); /* reset */
                 return FALSE;
@@ -381,39 +410,28 @@ mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
     if(IsObject(instance)){
         dMY_CXT;
         HV* const instance_stash = SvSTASH(SvRV(instance));
-        GV* const instance_isa   = find_method_pvs(instance_stash, "isa");
+        GV* const myisa          = find_method_pvs(instance_stash, "isa");
 
         /* the instance has no own isa method */
-        if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
+        if(myisa == NULL || GvCV(myisa) == GvCV(MY_CXT.universal_isa)){
             return stash == instance_stash
                 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
         }
         /* the instance has its own isa method */
         else {
-            int retval;
-            dSP;
+            SV* package;
+            int ok;
 
             ENTER;
             SAVETMPS;
 
-            PUSHMARK(SP);
-            EXTEND(SP, 2);
-            PUSHs(instance);
-            mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
-            PUTBACK;
-
-            call_sv((SV*)instance_isa, G_SCALAR);
-
-            SPAGAIN;
-
-            retval = SvTRUEx(POPs);
-
-            PUTBACK;
+            package = newSVpvn_share(HvNAME_get(stash), HvNAMELEN_get(stash), 0U);
+            ok = sv_true(mcall1s(instance, "isa", sv_2mortal(package)));
 
             FREETMPS;
             LEAVE;
 
-            return retval;
+            return ok;
         }
     }
     return FALSE;
@@ -444,23 +462,11 @@ mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
             }
             else{
                 bool ok;
-                dSP;
 
                 ENTER;
                 SAVETMPS;
 
-                PUSHMARK(SP);
-                EXTEND(SP, 2);
-                PUSHs(instance);
-                PUSHs(sv_mortalcopy(name));
-                PUTBACK;
-
-                call_method("can", G_SCALAR);
-
-                SPAGAIN;
-                ok = SvTRUE(TOPs);
-                (void)POPs;
-                PUTBACK;
+                ok = sv_true(mcall1s(instance, "can", sv_mortalcopy(name)));
 
                 FREETMPS;
                 LEAVE;
@@ -487,7 +493,7 @@ mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* con
         param,       /* mg_obj: refcnt will be increased */
         PERL_MAGIC_ext,
         &mouse_util_type_constraints_vtbl,
-        (void*)fptr, /* mg_ptr */
+        (char*)fptr, /* mg_ptr */
         0            /* mg_len: 0 for static data */
     );
 
@@ -503,18 +509,18 @@ mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predic
     STRLEN klass_len;
     const char* klass_pv = SvPV_const(klass, klass_len);
     SV*   param;
-    void* fptr;
+    check_fptr_t fptr;
 
     klass_pv = mouse_canonicalize_package_name(klass_pv);
 
     if(strNE(klass_pv, "UNIVERSAL")){
         param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
-        fptr = (void*)mouse_is_an_instance_of;
+        fptr = (check_fptr_t)mouse_is_an_instance_of;
 
     }
     else{
         param = NULL;
-        fptr = (void*)mouse_is_an_instance_of_universal;
+        fptr = (check_fptr_t)mouse_is_an_instance_of_universal;
     }
 
     return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
@@ -527,10 +533,7 @@ mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const pred
     I32 len;
     I32 i;
 
-    SvGETMAGIC(methods);
-    if(!IsArrayRef(methods)){
-        croak("You must pass an ARRAY ref method names");
-    }
+    must_ref(methods, "an ARRAY ref for method names", SVt_PVAV);
     av = (AV*)SvRV(methods);
 
     len = av_len(av) + 1;
@@ -656,10 +659,13 @@ BOOT:
     INSTALL_SIMPLE_READER(TypeConstraint, parent);
     INSTALL_SIMPLE_READER(TypeConstraint, message);
 
+    INSTALL_SIMPLE_READER(TypeConstraint, type_parameter);
+
     INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
     INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
 
     INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
+    INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, __is_parameterized, type_parameter); /* Mouse specific */
 
 void
 compile_type_constraint(SV* self)
@@ -719,8 +725,7 @@ CODE:
             SV* const tc = *av_fetch(types, i, TRUE);
             SV* const c  = get_slots(tc, "compiled_type_constraint");
             if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
-                sv_dump(self);
-                croak("'%"SVf"' has no compiled type constraint", self);
+                mouse_throw_error(self, c, "'%"SVf"' has no compiled type constraint", self);
             }
             av_push(union_checks, newSVsv(c));
         }
@@ -735,6 +740,19 @@ CODE:
     else{
         check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
     }
-    set_slots(self, "compiled_type_constraint", check);
+    (void)set_slots(self, "compiled_type_constraint", check);
+}
+
+bool
+check(SV* self, SV* sv)
+CODE:
+{
+    SV* const check = get_slots(self, "compiled_type_constraint");
+    if(!(check && mouse_tc_CodeRef(aTHX_ NULL, check))){
+        mouse_throw_error(self, check, "'%"SVf"' has no compiled type constraint", self);
+    }
+    RETVAL = mouse_tc_check(aTHX_ check, sv) ? TRUE : FALSE;
 }
+OUTPUT:
+    RETVAL