#include "mouse.h"
+static MGVTBL mouse_xa_vtbl; /* identity */
-AV*
-mouse_get_xa(pTHX_ SV* const attr) {
- static MGVTBL mouse_xa_vtbl; /* identity */
-
- AV* xa;
+static AV*
+mouse_build_xa(pTHX_ SV* const attr) {
+ AV* xa;
MAGIC* mg;
- if(!IsObject(attr)){
- croak("Not a Mouse meta attribute");
- }
+ SV* slot;
+ STRLEN len;
+ const char* pv;
+ U16 flags = 0x00;
- mg = mouse_mg_find(aTHX_ SvRV(attr), &mouse_xa_vtbl, 0x00);
- if(!mg){
- SV* slot;
- STRLEN len;
- const char* pv;
- U16 flags = 0x00;
-
- ENTER;
- SAVETMPS;
+ ENTER;
+ SAVETMPS;
- xa = newAV();
+ xa = newAV();
- mg = sv_magicext(SvRV(attr), (SV*)xa, PERL_MAGIC_ext, &mouse_xa_vtbl,NULL, 0);
- SvREFCNT_dec(xa); /* refcnt++ in sv_magicext */
+ mg = sv_magicext(SvRV(attr), (SV*)xa, PERL_MAGIC_ext, &mouse_xa_vtbl, NULL, 0);
+ SvREFCNT_dec(xa); /* refcnt++ in sv_magicext */
- av_extend(xa, MOUSE_XA_last - 1);
+ av_extend(xa, MOUSE_XA_last - 1);
- slot = mcall0(attr, mouse_name);
- pv = SvPV_const(slot, len);
- av_store(xa, MOUSE_XA_SLOT, newSVpvn_share(pv, len, 0U));
+ slot = mcall0(attr, mouse_name);
+ pv = SvPV_const(slot, len);
+ av_store(xa, MOUSE_XA_SLOT, newSVpvn_share(pv, len, 0U));
- av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
+ av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
- av_store(xa, MOUSE_XA_INIT_ARG, newSVsv(mcall0s(attr, "init_arg")));
+ av_store(xa, MOUSE_XA_INIT_ARG, newSVsv(mcall0s(attr, "init_arg")));
- if(predicate_calls(attr, "has_type_constraint")){
- SV* tc;
- flags |= MOUSEf_ATTR_HAS_TC;
+ if(predicate_calls(attr, "has_type_constraint")){
+ SV* tc;
+ flags |= MOUSEf_ATTR_HAS_TC;
- tc = mcall0s(attr, "type_constraint");
- av_store(xa, MOUSE_XA_TC, newSVsv(tc));
+ tc = mcall0s(attr, "type_constraint");
+ av_store(xa, MOUSE_XA_TC, newSVsv(tc));
- if(predicate_calls(attr, "should_auto_deref")){
- SV* const is_a_type_of = sv_2mortal(newSVpvs_share("is_a_type_of"));
+ if(predicate_calls(attr, "should_auto_deref")){
+ SV* const is_a_type_of = sv_2mortal(newSVpvs_share("is_a_type_of"));
- flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
- if( SvTRUEx(mcall1(tc, is_a_type_of, newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
- flags |= MOUSEf_TC_IS_ARRAYREF;
- }
- else if( SvTRUEx(mcall1(tc, is_a_type_of, newSVpvs_flags("HashRef", SVs_TEMP))) ){
- flags |= MOUSEf_TC_IS_HASHREF;
- }
- else{
- mouse_throw_error(attr, tc,
- "Can not auto de-reference the type constraint '%"SVf"'",
- mcall0(tc, mouse_name));
- }
+ flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
+ if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
+ flags |= MOUSEf_TC_IS_ARRAYREF;
}
-
- if(predicate_calls(attr, "should_coerce")){
- flags |= MOUSEf_ATTR_SHOULD_COERCE;
+ else if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("HashRef", SVs_TEMP))) ){
+ flags |= MOUSEf_TC_IS_HASHREF;
+ }
+ else{
+ mouse_throw_error(attr, tc,
+ "Can not auto de-reference the type constraint '%"SVf"'",
+ mcall0(tc, mouse_name));
}
-
}
- if(predicate_calls(attr, "has_trigger")){
- flags |= MOUSEf_ATTR_HAS_TRIGGER;
+ if(predicate_calls(attr, "should_coerce") && predicate_calls(tc, "has_coercion")){
+ flags |= MOUSEf_ATTR_SHOULD_COERCE;
}
- if(predicate_calls(attr, "is_lazy")){
- flags |= MOUSEf_ATTR_IS_LAZY;
- }
- if(predicate_calls(attr, "has_builder")){
- flags |= MOUSEf_ATTR_HAS_BUILDER;
- }
- else if(predicate_calls(attr, "has_default")){
- flags |= MOUSEf_ATTR_HAS_DEFAULT;
- }
+ }
- if(predicate_calls(attr, "is_weak_ref")){
- flags |= MOUSEf_ATTR_IS_WEAK_REF;
- }
+ if(predicate_calls(attr, "has_trigger")){
+ flags |= MOUSEf_ATTR_HAS_TRIGGER;
+ }
- if(predicate_calls(attr, "is_required")){
- flags |= MOUSEf_ATTR_IS_REQUIRED;
- }
+ if(predicate_calls(attr, "is_lazy")){
+ flags |= MOUSEf_ATTR_IS_LAZY;
+ }
+ if(predicate_calls(attr, "has_builder")){
+ flags |= MOUSEf_ATTR_HAS_BUILDER;
+ }
+ else if(predicate_calls(attr, "has_default")){
+ flags |= MOUSEf_ATTR_HAS_DEFAULT;
+ }
+
+ if(predicate_calls(attr, "is_weak_ref")){
+ flags |= MOUSEf_ATTR_IS_WEAK_REF;
+ }
+
+ if(predicate_calls(attr, "is_required")){
+ flags |= MOUSEf_ATTR_IS_REQUIRED;
+ }
+
+ av_store(xa, MOUSE_XA_FLAGS, newSVuv(flags));
+ MOUSE_mg_flags(mg) = flags;
+
+ FREETMPS;
+ LEAVE;
+
+ return xa;
+}
- av_store(xa, MOUSE_XA_FLAGS, newSVuv(flags));
- MOUSE_mg_flags(mg) = flags;
+AV*
+mouse_get_xa(pTHX_ SV* const attr) {
+ AV* xa;
+ MAGIC* mg;
- FREETMPS;
- LEAVE;
+ if(!IsObject(attr)){
+ croak("Not a Mouse meta attribute");
+ }
+
+ mg = mouse_mg_find(aTHX_ SvRV(attr), &mouse_xa_vtbl, 0x00);
+ if(!mg){
+ xa = mouse_build_xa(aTHX_ attr);
}
else{
- xa = (AV*)MOUSE_mg_obj(mg);
+ xa = (AV*)MOUSE_mg_obj(mg);
assert(xa);
assert(SvTYPE(xa) == SVt_PVAV);
SV* tc_code;
if(flags & MOUSEf_ATTR_SHOULD_COERCE){
- value = mcall1s(tc, "coerce", value);
+ value = mcall1(tc, mouse_coerce, value);
}
if(!SvOK(MOUSE_xa_tc_code(xa))){
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
INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_auto_deref, auto_deref);
INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_coerce, coerce);
INSTALL_SIMPLE_READER(Attribute, documentation);
+ INSTALL_SIMPLE_READER(Attribute, insertion_order);
/* predicates */
INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_accessor, accessor);
INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_builder, builder);
INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_documentation, documentation);
- INSTALL_SIMPLE_READER_WITH_DEFAULTS(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");
+ INSTALL_CLASS_HOLDER(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");
+
+void
+_process_options(SV* klass, SV* name, HV* args)
+CODE:
+{
+ SV** svp;
+ SV* tc = NULL;
+
+ /* 'required' requires eigher 'init_arg', 'builder', or 'default' */
+ bool can_be_required = FALSE;
+ bool has_default = FALSE;
+ bool has_builder = FALSE;
+ /* taken from Class::MOP::Attribute::new */
+
+ must_defined(name, "an attribute name");
+
+ svp = hv_fetchs(args, "init_arg", FALSE);
+ if(!svp){
+ (void)hv_stores(args, "init_arg", newSVsv(name));
+ can_be_required = TRUE;
+ }
+ else{
+ can_be_required = SvOK(*svp) ? TRUE : FALSE;
+ }
+
+ svp = hv_fetchs(args, "builder", FALSE);
+ if(svp){
+ if(!SvOK(*svp)){
+ mouse_throw_error(klass, *svp,
+ "builder must be a defined scalar value which is a method name");
+ }
+ can_be_required = TRUE;
+ has_builder = TRUE;
+ }
+ else if((svp = hv_fetchs(args, "default", FALSE))){
+ if(SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) {
+ 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);
+ }
+ can_be_required = TRUE;
+ has_default = TRUE;
+ }
+
+ svp = hv_fetchs(args, "required", FALSE);
+ if( (svp && sv_true(*svp)) && !can_be_required){
+ mouse_throw_error(klass, NULL,
+ "You cannot have a required attribute (%"SVf") "
+ "without a default, builder, or an init_arg", name);
+ }
+
+ /* taken from Mouse::Meta::Attribute->new and ->_process_args */
+
+ svp = hv_fetchs(args, "is", FALSE);
+ if(svp){
+ const char* const is = SvOK(*svp) ? SvPV_nolen_const(*svp) : "undef";
+ if(strEQ(is, "ro")){
+ svp = hv_fetchs(args, "reader", TRUE);
+ if(!sv_true(*svp)){
+ sv_setsv(*svp, name);
+ }
+ }
+ else if(strEQ(is, "rw")){
+ if(hv_fetchs(args, "writer", FALSE)){
+ svp = hv_fetchs(args, "reader", TRUE);
+ }
+ else{
+ svp = hv_fetchs(args, "accessor", TRUE);
+ }
+ sv_setsv(*svp, name);
+ }
+ else if(strEQ(is, "bare")){
+ /* do nothing, but don't complain (later) about missing methods */
+ }
+ else{
+ mouse_throw_error(klass, NULL,
+ "I do not understand this option (is => %s) on attribute (%"SVf")",
+ is, name);
+ }
+ }
+
+ svp = hv_fetchs(args, "isa", FALSE);
+ if(svp){
+ SPAGAIN;
+ PUSHMARK(SP);
+ XPUSHs(*svp);
+ PUTBACK;
+
+ call_pv("Mouse::Util::TypeConstraints::find_or_create_isa_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);
+ }
+
+ svp = hv_fetchs(args, "coerce", FALSE);
+ if(svp){
+ if(!tc){
+ mouse_throw_error(klass, NULL,
+ "You cannot have coercion without specifying a type constraint "
+ "on attribute (%"SVf")", name);
+ }
+ svp = hv_fetchs(args, "weak_ref", FALSE);
+ if(svp && sv_true(*svp)){
+ mouse_throw_error(klass, NULL,
+ "You cannot have a weak reference to a coerced value on "
+ "attribute (%"SVf")", name);
+ }
+ }
+
+ svp = hv_fetchs(args, "lazy_build", FALSE);
+ if(svp){
+ SV* clearer;
+ SV* predicate;
+ if(has_default){
+ mouse_throw_error(klass, NULL,
+ "You can not use lazy_build and default for the same "
+ "attribute (%"SVf")", name);
+ }
+
+ svp = hv_fetchs(args, "lazy", TRUE);
+ sv_setiv(*svp, TRUE);
+
+ svp = hv_fetchs(args, "builder", TRUE);
+ if(!sv_true(*svp)){
+ sv_setpvf(*svp, "_build_%"SVf, name);
+ }
+ has_builder = TRUE;
+
+ clearer = *hv_fetchs(args, "clearer", TRUE);
+ predicate = *hv_fetchs(args, "predicate", TRUE);
+
+ if(SvPV_nolen_const(name)[0] == '_'){
+ if(!sv_true(clearer)){
+ sv_setpvf(clearer, "_clear%"SVf, name);
+ }
+ if(!sv_true(predicate)){
+ sv_setpvf(predicate, "_has%"SVf, name);
+ }
+ }
+ else{
+ if(!sv_true(clearer)){
+ sv_setpvf(clearer, "clear_%"SVf, name);
+ }
+ if(!sv_true(predicate)){
+ sv_setpvf(predicate, "has_%"SVf, name);
+ }
+ }
+ }
+
+ svp = hv_fetchs(args, "auto_deref", FALSE);
+ if(svp && sv_true(*svp)){
+ SV* const meth = sv_2mortal(newSVpvs_share("is_a_type_of"));
+ if(!tc){
+ mouse_throw_error(klass, NULL,
+ "You cannot auto-dereference without specifying a type "
+ "constraint on attribute (%"SVf")", name);
+ }
+
+ if(!(sv_true(mcall1(tc, meth, newSVpvs_flags("ArrayRef", SVs_TEMP)))
+ || sv_true(mcall1(tc, meth, newSVpvs_flags("HashRef", SVs_TEMP))) )){
+ mouse_throw_error(klass, NULL,
+ "You cannot auto-dereference anything other than a ArrayRef "
+ "or HashRef on attribute (%"SVf")", name);
+ }
+ }
+
+ svp = hv_fetchs(args, "trigger", FALSE);
+ if(svp){
+ if(!IsCodeRef(*svp)){
+ mouse_throw_error(klass, NULL,
+ "Trigger must be a CODE ref on attribute (%"SVf")",
+ name);
+ }
+ }
+
+
+ svp = hv_fetchs(args, "lazy", FALSE);
+ if(svp && sv_true(*svp)){
+ if(!(has_default || has_builder)){
+ mouse_throw_error(klass, NULL,
+ "You cannot have lazy attribute (%"SVf") without specifying "
+ "a default value for it", name);
+ }
+ }
+}