X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=xs-src%2FMouseTypeConstraints.xs;h=8188467f1fad9119501ea1e7e7c57f79b4a28e35;hp=96cb6eb8dbf9f9a41fed4b7322ac90b746a0321a;hb=ca8e67d622c1a14bee4933ad64b1e465c5d63766;hpb=ebe91068002fbe34a924a0a9e2cd79553867938c diff --git a/xs-src/MouseTypeConstraints.xs b/xs-src/MouseTypeConstraints.xs index 96cb6eb..8188467 100644 --- a/xs-src/MouseTypeConstraints.xs +++ b/xs-src/MouseTypeConstraints.xs @@ -16,11 +16,15 @@ 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); assert(SvTYPE(cv) == SVt_PVCV); + SvGETMAGIC(sv); if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */ MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr; @@ -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; } @@ -170,9 +174,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 +248,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 +266,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; @@ -360,6 +366,19 @@ mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){ return FALSE; } +#define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c) +#define find_method_pvs(a, b) mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b)) + +static inline GV* +mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){ + GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE); + if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */ + return *gvp; + } + + return gv_fetchmeth_autoload(stash, name, namelen, 0); +} + int mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){ assert(stash); @@ -368,7 +387,7 @@ 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 = gv_fetchmeth_autoload(instance_stash, "isa", sizeof("isa")-1, 0); + GV* const instance_isa = 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)){ @@ -392,9 +411,7 @@ mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){ call_sv((SV*)instance_isa, G_SCALAR); SPAGAIN; - - retval = SvTRUEx(POPs); - + retval = sv_true(POPs); PUTBACK; FREETMPS; @@ -417,15 +434,15 @@ mouse_can_methods(pTHX_ AV* const methods, SV* const instance){ if(IsObject(instance)){ dMY_CXT; HV* const mystash = SvSTASH(SvRV(instance)); - GV* const mycan = gv_fetchmeth_autoload(mystash, "can", sizeof("can")-1, 0); - bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_isa)) ? TRUE : FALSE; + GV* const mycan = find_method_pvs(mystash, "can"); + bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE; I32 const len = AvFILLp(methods) + 1; I32 i; for(i = 0; i < len; i++){ SV* const name = MOUSE_av_at(methods, i); if(use_builtin){ - if(!gv_fetchmeth_autoload(mystash, SvPVX(name), SvCUR(name), 0)){ + if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){ return FALSE; } } @@ -445,8 +462,7 @@ mouse_can_methods(pTHX_ AV* const methods, SV* const instance){ call_method("can", G_SCALAR); SPAGAIN; - ok = SvTRUE(TOPs); - (void)POPs; + ok = sv_true(POPs); PUTBACK; FREETMPS; @@ -474,7 +490,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 */ ); @@ -490,18 +506,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); @@ -722,6 +738,6 @@ 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); }