X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=xs-src%2FMouseAccessor.xs;h=4dd16dd84aec8911988dcf3f61d27b1fd2517eda;hp=86b0b9f40fbd82bb75ea2dc862d56c2757ff5387;hb=957474ad02b108cb465de4d314bb22b6f1ea5a9f;hpb=0aad026606f2d1d5d27cfb8824dd182f790fa9e2 diff --git a/xs-src/MouseAccessor.xs b/xs-src/MouseAccessor.xs index 86b0b9f..4dd16dd 100644 --- a/xs-src/MouseAccessor.xs +++ b/xs-src/MouseAccessor.xs @@ -11,8 +11,9 @@ static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */ +#define dMOUSE_self SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv) -SV* +static inline SV* mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) { if(items < 1){ croak("Too few arguments for %s", GvNAME(CvGV(cv))); @@ -27,7 +28,7 @@ mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) { CV* -mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){ +mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){ AV* const xa = mouse_get_xa(aTHX_ attr); CV* xsub; MAGIC* mg; @@ -37,7 +38,7 @@ mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_im mg = sv_magicext((SV*)xsub, MOUSE_xa_slot(xa), PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY); - MOUSE_mg_flags(mg) = (U16)SvUV(MOUSE_xa_flags(xa)); + MOUSE_mg_flags(mg) = (U16)MOUSE_xa_flags(xa); /* NOTE: * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx) @@ -47,36 +48,6 @@ mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_im return xsub; } -static SV* -mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){ - SV* const tc = MOUSE_xa_tc(xa); - SV* tc_code; - - if(flags & MOUSEf_ATTR_SHOULD_COERCE){ - value = mcall1s(tc, "coerce", value); - } - - if(!SvOK(MOUSE_xa_tc_code(xa))){ - tc_code = mcall0s(tc, "_compiled_type_constraint"); - av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code)); - - if(!IsCodeRef(tc_code)){ - mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference"); - } - } - else{ - tc_code = MOUSE_xa_tc_code(xa); - } - - if(!mouse_tc_check(aTHX_ tc_code, value)){ - mouse_throw_error(MOUSE_xa_attribute(xa), value, - "Attribute (%"SVf") does not pass the type constraint because: %"SVf, - mcall0(MOUSE_xa_attribute(xa), mouse_name), - mcall1s(tc, "get_message", value)); - } - - return value; -} #define PUSH_VALUE(value, flags) STMT_START { \ if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \ @@ -140,36 +111,13 @@ mouse_push_values(pTHX_ SV* const value, U16 const flags){ static void mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){ U16 const flags = MOUSE_mg_flags(mg); - SV* const slot = MOUSE_mg_slot(mg); SV* value; - value = get_slot(self, slot); + value = get_slot(self, MOUSE_mg_slot(mg)); /* check_lazy */ if( !value && flags & MOUSEf_ATTR_IS_LAZY ){ - AV* const xa = MOUSE_mg_xa(mg); - SV* const attr = MOUSE_xa_attribute(xa); - - /* get default value by $attr->builder or $attr->default */ - if(flags & MOUSEf_ATTR_HAS_BUILDER){ - SV* const builder = mcall0s(attr, "builder"); - value = mcall0(self, builder); - } - else { - value = mcall0s(attr, "default"); - - if(IsCodeRef(value)){ - value = mcall0(self, value); - } - } - - /* apply coerce and type constraint */ - if(flags & MOUSEf_ATTR_HAS_TC){ - value = mouse_apply_type_constraint(aTHX_ xa, value, flags); - } - - /* store value to slot */ - value = set_slot(self, slot, value); + value = mouse_xa_set_default(aTHX_ MOUSE_mg_xa(mg), self); } PUSH_VALUE(value, flags); @@ -181,7 +129,7 @@ mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){ SV* const slot = MOUSE_mg_slot(mg); if(flags & MOUSEf_ATTR_HAS_TC){ - value = mouse_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags); + value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags); } set_slot(self, slot, value); @@ -273,7 +221,7 @@ mouse_accessor_get_mg(pTHX_ CV* const xsub){ */ CV* -mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl){ +mouse_simple_accessor_generate(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen){ CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__); SV* const slot = newSVpvn_share(key, keylen, 0U); MAGIC* mg; @@ -283,8 +231,11 @@ mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const sv_2mortal((SV*)xsub); } - mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, NULL, 0); + mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)dptr, dlen); SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */ + if(dlen == HEf_SVKEY){ + SvREFCNT_dec(dptr); + } /* NOTE: * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx) @@ -294,18 +245,49 @@ mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const return xsub; } +XS(XS_Mouse_simple_accessor) +{ + dVAR; dXSARGS; + dMOUSE_self; + MAGIC* const mg = (MAGIC*)XSANY.any_ptr; + SV* value; + + if(items == 1){ /* reader */ + value = get_slot(self, MOUSE_mg_slot(mg)); + if(!value) { + if(MOUSE_mg_ptr(mg)){ + /* the default value must be a SV */ + assert(MOUSE_mg_len(mg) == HEf_SVKEY); + value = (SV*)MOUSE_mg_ptr(mg); + } + else{ + value = &PL_sv_undef; + } + } + } + else if(items == 2){ /* writer */ + value = set_slot(self, MOUSE_mg_slot(mg), ST(1)); + } + else { + croak("Expected exactly one or two argument for an accessor for '%"SVf"'", MOUSE_mg_slot(mg)); + } + + ST(0) = value; + XSRETURN(1); +} + XS(XS_Mouse_simple_reader) { dVAR; dXSARGS; dMOUSE_self; - SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr); + MAGIC* const mg = (MAGIC*)XSANY.any_ptr; SV* value; if (items != 1) { - croak("Expected exactly one argument for a reader for '%"SVf"'", slot); + croak("Expected exactly one argument for a reader for '%"SVf"'", MOUSE_mg_slot(mg)); } - value = get_slot(self, slot); + value = get_slot(self, MOUSE_mg_slot(mg)); ST(0) = value ? value : &PL_sv_undef; XSRETURN(1); } @@ -402,7 +384,8 @@ mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const valu CHECK_INSTANCE(instance); he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U); sv = HeVAL(he); - sv_setsv_mg(sv, value); + sv_setsv(sv, value); + SvSETMAGIC(sv); return sv; } @@ -435,7 +418,7 @@ CV* _generate_accessor(klass, SV* attr, metaclass) CODE: { - RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_accessor); + RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_accessor); } OUTPUT: RETVAL @@ -444,7 +427,7 @@ CV* _generate_reader(klass, SV* attr, metaclass) CODE: { - RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_reader); + RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_reader); } OUTPUT: RETVAL @@ -453,7 +436,7 @@ CV* _generate_writer(klass, SV* attr, metaclass) CODE: { - RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_writer); + RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_writer); } OUTPUT: RETVAL @@ -465,7 +448,7 @@ CODE: SV* const slot = mcall0s(attr, "name"); STRLEN len; const char* const pv = SvPV_const(slot, len); - RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer); + RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0); } OUTPUT: RETVAL @@ -477,7 +460,7 @@ CODE: SV* const slot = mcall0s(attr, "name"); STRLEN len; const char* const pv = SvPV_const(slot, len); - RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate); + RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0); } OUTPUT: RETVAL