From: gfx Date: Tue, 3 Nov 2009 02:36:58 +0000 (+0900) Subject: Make type check macros X-Git-Tag: 0.40_06~41 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=80aa5731d6763d8f38ba0fa057729af7026e6580;p=gitmo%2FMouse.git Make type check macros --- diff --git a/mouse.h b/mouse.h index ba1c3e0..8f543c5 100644 --- a/mouse.h +++ b/mouse.h @@ -59,7 +59,10 @@ bool mouse_is_class_loaded(pTHX_ SV*); #define is_an_instance_of(klass, sv) mouse_is_an_instance_of(aTHX_ gv_stashpvs(klass, GV_ADD), (sv)) -#define IsObject(sv) (SvROK(sv) && SvOBJECT(SvRV(sv))) +#define IsObject(sv) (SvROK(sv) && SvOBJECT(SvRV(sv))) +#define IsArrayRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV) +#define IsHashRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV) +#define IsCodeRef(sv) (SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVCV) #define mcall0(invocant, m) mouse_call0(aTHX_ (invocant), (m)) #define mcall1(invocant, m, arg1) mouse_call1(aTHX_ (invocant), (m), (arg1)) diff --git a/xs-src/MouseAccessor.xs b/xs-src/MouseAccessor.xs index 0e53ca8..b6c11cc 100644 --- a/xs-src/MouseAccessor.xs +++ b/xs-src/MouseAccessor.xs @@ -165,7 +165,7 @@ mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){ tc_code = mcall0s(tc, "_compiled_type_constraint"); av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code)); - if(!(SvROK(tc_code) && SvTYPE(SvRV(tc_code)) == SVt_PVCV)){ + if(!IsCodeRef(tc_code)){ mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference"); } } @@ -206,14 +206,15 @@ mouse_push_values(pTHX_ SV* const value, U16 const flags){ } if(flags & MOUSEf_TC_IS_ARRAYREF){ - AV* const av = (AV*)SvRV(value); + AV* av; I32 len; I32 i; - if(SvTYPE(av) != SVt_PVAV){ + if(!IsArrayRef(value)){ croak("Mouse-panic: Not an ARRAY reference"); } + av = (AV*)SvRV(value); len = av_len(av) + 1; EXTEND(SP, len); for(i = 0; i < len; i++){ @@ -222,13 +223,14 @@ mouse_push_values(pTHX_ SV* const value, U16 const flags){ } } else if(flags & MOUSEf_TC_IS_HASHREF){ - HV* const hv = (HV*)SvRV(value); + HV* hv; HE* he; - if(SvTYPE(hv) != SVt_PVHV){ + if(!IsHashRef(value)){ croak("Mouse-panic: Not a HASH reference"); } + hv = (HV*)SvRV(value); hv_iterinit(hv); while((he = hv_iternext(hv))){ EXTEND(SP, 2); @@ -261,7 +263,7 @@ mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){ else { value = mcall0s(attr, "default"); - if(SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVCV){ + if(IsCodeRef(value)){ value = mcall0(self, value); } } diff --git a/xs-src/MouseTypeConstraints.xs b/xs-src/MouseTypeConstraints.xs index b88ba95..6f69f2f 100644 --- a/xs-src/MouseTypeConstraints.xs +++ b/xs-src/MouseTypeConstraints.xs @@ -188,19 +188,19 @@ mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { int mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { assert(sv); - return SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVAV; + return IsArrayRef(sv); } int mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { assert(sv); - return SvROK(sv) && !SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) == SVt_PVHV; + return IsHashRef(sv); } int mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { assert(sv); - return SvROK(sv) && !SvOBJECT(SvRV(sv))&& SvTYPE(SvRV(sv)) == SVt_PVCV; + return IsCodeRef(sv); } int @@ -244,7 +244,7 @@ mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) { static int mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) { - if(mouse_tc_ArrayRef(aTHX_ NULL, sv)){ + if(IsArrayRef(sv)){ AV* const av = (AV*)SvRV(sv); I32 const len = av_len(av) + 1; I32 i; @@ -546,7 +546,7 @@ CODE: { check_fptr_t fptr; SV* const tc_code = mcall0s(param, "_compiled_type_constraint"); - if(!(SvROK(tc_code) && SvTYPE(SvRV(tc_code)) == SVt_PVCV)){ + if(!IsCodeRef(tc_code)){ croak("_compiled_type_constraint didn't return a CODE reference"); } @@ -625,7 +625,7 @@ CODE: I32 len; I32 i; - if(!mouse_tc_ArrayRef(aTHX_ NULL, types_ref)){ + if(!IsArrayRef(types_ref)){ croak("Not an ARRAY reference"); } types = (AV*)SvRV(types_ref); diff --git a/xs-src/MouseUtil.xs b/xs-src/MouseUtil.xs index 6af7e03..8289828 100644 --- a/xs-src/MouseUtil.xs +++ b/xs-src/MouseUtil.xs @@ -44,7 +44,7 @@ mouse_mro_get_linear_isa(pTHX_ HV* const stash){ avref = POPs; PUTBACK; - if(SvROK(avref) && SvTYPE(SvRV(avref)) == SVt_PVAV){ + if(IsArrayRef(avref)){ AV* const av = (AV*)SvRV(avref); I32 const len = AvFILLp(av) + 1; I32 i;