X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=xs-src%2FMouseAttribute.xs;h=8ea3a060faa36d385ae6ef0ecaea6219c43ca504;hp=2b3e6212862ce65dd661964d930a00e06695e53d;hb=077f2efda66008ab1a1fb959851a7c9062404588;hpb=c6b8ac471ee3137b7ad72ad46efca0099acde204 diff --git a/xs-src/MouseAttribute.xs b/xs-src/MouseAttribute.xs index 2b3e621..8ea3a06 100644 --- a/xs-src/MouseAttribute.xs +++ b/xs-src/MouseAttribute.xs @@ -54,7 +54,7 @@ mouse_build_xa(pTHX_ SV* const attr) { } } - if(predicate_calls(attr, "should_coerce")){ + if(predicate_calls(attr, "should_coerce") && predicate_calls(tc, "has_coercion")){ flags |= MOUSEf_ATTR_SHOULD_COERCE; } @@ -184,6 +184,43 @@ mouse_xa_set_default(pTHX_ AV* const xa, SV* const object) { return value; } +static void +mouse_check_isa_does_does(pTHX_ SV* const klass, SV* const name, SV* const isa, SV* const does){ + STRLEN len; + const char* const pv = SvPV_const(isa, len); /* need strigify */ + bool does_ok; + dSP; + + ENTER; + SAVETMPS; + + SAVESPTR(ERRSV); + ERRSV = sv_newmortal(); + + PUSHMARK(SP); + EXTEND(SP, 2); + mPUSHp(pv, len); + PUSHs(does); + PUTBACK; + + call_method("does", G_EVAL | G_SCALAR); + + SPAGAIN; + does_ok = sv_true(POPs); + PUTBACK; + + FREETMPS; + LEAVE; + + if(!does_ok){ + mouse_throw_error(klass, NULL, + "Cannot have both an isa option and a does option" + "because '%"SVf"' does not do '%"SVf"' on attribute (%"SVf")", + isa, does, name + ); + } +} + MODULE = Mouse::Meta::Attribute PACKAGE = Mouse::Meta::Attribute PROTOTYPES: DISABLE @@ -260,7 +297,7 @@ CODE: svp = hv_fetchs(args, "builder", FALSE); if(svp){ if(!SvOK(*svp)){ - mouse_throw_error(klass, NULL, + mouse_throw_error(klass, *svp, "builder must be a defined scalar value which is a method name"); } can_be_required = TRUE; @@ -268,7 +305,7 @@ CODE: } else if((svp = hv_fetchs(args, "default", FALSE))){ if(SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) { - mouse_throw_error(klass, NULL, + mouse_throw_error(klass, *svp, "References are not allowed as default values, you must " "wrap the default of '%"SVf"' in a CODE reference " "(ex: sub { [] } and not [])", name); @@ -327,17 +364,25 @@ CODE: tc = newSVsv(POPs); PUTBACK; } - else if((svp = hv_fetchs(args, "does", FALSE))){ - SPAGAIN; - PUSHMARK(SP); - XPUSHs(*svp); - PUTBACK; - call_pv("Mouse::Util::TypeConstraints::find_or_create_does_type_constraint", - G_SCALAR); - SPAGAIN; - tc = newSVsv(POPs); - PUTBACK; + if((svp = hv_fetchs(args, "does", FALSE))){ + /* check 'isa' does 'does' */ + if(tc){ + mouse_check_isa_does_does(aTHX_ klass, name, tc, *svp); + /* nothing to do */ + } + else{ + SPAGAIN; + PUSHMARK(SP); + XPUSHs(*svp); + PUTBACK; + + call_pv("Mouse::Util::TypeConstraints::find_or_create_does_type_constraint", + G_SCALAR); + SPAGAIN; + tc = newSVsv(POPs); + PUTBACK; + } } if(tc){ (void)hv_stores(args, "type_constraint", tc);