X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=xs-src%2FMouseTypeConstraints.xs;h=5ad33898d3a51ae4a07c1daaf3134ad8d15883e3;hb=f3e4c7ebdc35a752e69f6031121b5cab1630b09a;hp=9f1e6779a62e09bd046e40f40a4050887ba20290;hpb=28e3abdf23123bfb0bd056e9e21f386c9269559f;p=gitmo%2FMouse.git diff --git a/xs-src/MouseTypeConstraints.xs b/xs-src/MouseTypeConstraints.xs index 9f1e677..5ad3389 100644 --- a/xs-src/MouseTypeConstraints.xs +++ b/xs-src/MouseTypeConstraints.xs @@ -7,7 +7,7 @@ #if PERL_BCDVERSION >= 0x5008005 #define LooksLikeNumber(sv) looks_like_number(sv) #else -#define LooksLikeNumber(sv) ( SvPOKp(sv) ? looks_like_number(sv) : SvNIOKp(sv) ) +#define LooksLikeNumber(sv) ( SvPOKp(sv) ? looks_like_number(sv) : (I32)SvNIOKp(sv) ) #endif #ifndef SvRXOK @@ -117,15 +117,38 @@ mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { return LooksLikeNumber(sv); } +static 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; + (void)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); @@ -702,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)); } @@ -721,3 +743,16 @@ CODE: (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 +