X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMouse.git;a=blobdiff_plain;f=xs-src%2FMouseAccessor.xs;h=909e59507cab1463f067193a82b16959222e0106;hp=d724f937fab55dd9a25d6116067bf6dbaa981860;hb=ca8e67d622c1a14bee4933ad64b1e465c5d63766;hpb=cc7cd81f082a9aaa3dd17ca07dbb91da28bd0b69 diff --git a/xs-src/MouseAccessor.xs b/xs-src/MouseAccessor.xs index d724f93..909e595 100644 --- a/xs-src/MouseAccessor.xs +++ b/xs-src/MouseAccessor.xs @@ -28,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; @@ -132,7 +132,7 @@ mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){ value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags); } - set_slot(self, slot, value); + value = set_slot(self, slot, value); if(flags & MOUSEf_ATTR_IS_WEAK_REF){ weaken_slot(self, slot); @@ -221,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; @@ -231,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) @@ -246,15 +249,26 @@ 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); - ST(0) = value ? value : &PL_sv_undef; + 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; + } + } + + ST(0) = value; XSRETURN(1); } @@ -350,7 +364,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; } @@ -373,7 +388,7 @@ mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) { sv_rvweaken(HeVAL(he)); } } - + MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS PROTOTYPES: DISABLE @@ -383,7 +398,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 @@ -392,7 +407,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 @@ -401,7 +416,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 @@ -413,7 +428,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 @@ -425,7 +440,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