X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=xs-src%2FMouseTypeConstraints.xs;h=605c9237378da215bfdb6d953ea66a794b864306;hp=88181869de1f12b56c98d7f388cfdce1d2c2b5a5;hb=fc83f4cf2ef7708120a216bddc285b93082d7958;hpb=902329757f26fb562c33f6335ff66d6c81394900 diff --git a/xs-src/MouseTypeConstraints.xs b/xs-src/MouseTypeConstraints.xs index 8818186..605c923 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,8 +266,8 @@ 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; } } @@ -319,6 +326,7 @@ mouse_types_check(pTHX_ AV* const types, SV* const sv) { #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION typedef struct sui_cxt{ GV* universal_isa; + GV* universal_can; } my_cxt_t; START_MY_CXT @@ -358,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); @@ -366,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)){ @@ -390,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; @@ -410,6 +429,55 @@ mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){ return SvROK(sv) && SvOBJECT(SvRV(sv)); } +static int +mouse_can_methods(pTHX_ AV* const methods, SV* const instance){ + if(IsObject(instance)){ + dMY_CXT; + HV* const mystash = SvSTASH(SvRV(instance)); + 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(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){ + return FALSE; + } + } + 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 = sv_true(POPs); + PUTBACK; + + FREETMPS; + LEAVE; + + if(!ok){ + return FALSE; + } + } + } + return TRUE; + } + return FALSE; +} + static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */ static CV* @@ -422,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 */ ); @@ -438,23 +506,49 @@ 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); } +CV* +mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){ + AV* av; + AV* const param = newAV_mortal(); + I32 len; + I32 i; + + SvGETMAGIC(methods); + if(!IsArrayRef(methods)){ + croak("You must pass an ARRAY ref method names"); + } + av = (AV*)SvRV(methods); + + len = av_len(av) + 1; + for(i = 0; i < len; i++){ + SV* const name = *av_fetch(av, i, TRUE); + STRLEN pvlen; + const char* const pv = SvPV_const(name, pvlen); + + av_push(param, newSVpvn_share(pv, pvlen, 0U)); + } + + return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param); +} + + XS(XS_Mouse_constraint_check) { dVAR; dXSARGS; @@ -473,6 +567,9 @@ static void setup_my_cxt(pTHX_ pMY_CXT){ MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV); SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa); + + MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV); + SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can); } #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL) @@ -562,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) @@ -641,6 +741,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); }