3 #define CHECK_INSTANCE(instance) STMT_START{ \
4 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
5 croak("Invalid object instance"); \
10 #define MOUSE_mg_attribute(mg) MOUSE_xa_attribute(MOUSE_mg_xa(mg))
12 static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */
16 mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
18 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
21 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
22 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
30 mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
31 AV* const xa = mouse_get_xa(aTHX_ attr);
35 xsub = newXS(NULL, accessor_impl, __FILE__);
36 sv_2mortal((SV*)xsub);
38 mg = sv_magicext((SV*)xsub, MOUSE_xa_slot(xa), PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
40 MOUSE_mg_flags(mg) = (U16)SvUV(MOUSE_xa_flags(xa));
43 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
45 CvXSUBANY(xsub).any_ptr = (void*)mg;
51 mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
52 SV* const tc = MOUSE_xa_tc(xa);
55 if(flags & MOUSEf_ATTR_SHOULD_COERCE){
56 value = mcall1s(tc, "coerce", value);
59 if(!SvOK(MOUSE_xa_tc_code(xa))){
60 tc_code = mcall0s(tc, "_compiled_type_constraint");
61 av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
63 if(!IsCodeRef(tc_code)){
64 mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference");
68 tc_code = MOUSE_xa_tc_code(xa);
71 if(!mouse_tc_check(aTHX_ tc_code, value)){
72 mouse_throw_error(MOUSE_xa_attribute(xa), value,
73 "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
74 mcall0(MOUSE_xa_attribute(xa), mouse_name),
75 mcall1s(tc, "get_message", value));
81 #define PUSH_VALUE(value, flags) STMT_START { \
82 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
83 mouse_push_values(aTHX_ value, (flags)); \
87 XPUSHs(value ? value : &PL_sv_undef); \
92 /* pushes return values, does auto-deref if needed */
94 mouse_push_values(pTHX_ SV* const value, U16 const flags){
97 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
99 if(!(value && SvOK(value))){
103 if(flags & MOUSEf_TC_IS_ARRAYREF){
108 if(!IsArrayRef(value)){
109 croak("Mouse-panic: Not an ARRAY reference");
112 av = (AV*)SvRV(value);
113 len = av_len(av) + 1;
115 for(i = 0; i < len; i++){
116 SV** const svp = av_fetch(av, i, FALSE);
117 PUSHs(svp ? *svp : &PL_sv_undef);
120 else if(flags & MOUSEf_TC_IS_HASHREF){
124 if(!IsHashRef(value)){
125 croak("Mouse-panic: Not a HASH reference");
128 hv = (HV*)SvRV(value);
130 while((he = hv_iternext(hv))){
132 PUSHs(hv_iterkeysv(he));
133 PUSHs(hv_iterval(hv, he));
141 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
142 U16 const flags = MOUSE_mg_flags(mg);
143 SV* const slot = MOUSE_mg_slot(mg);
146 value = get_slot(self, slot);
149 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
150 AV* const xa = MOUSE_mg_xa(mg);
151 SV* const attr = MOUSE_xa_attribute(xa);
153 /* get default value by $attr->builder or $attr->default */
154 if(flags & MOUSEf_ATTR_HAS_BUILDER){
155 SV* const builder = mcall0s(attr, "builder");
156 value = mcall0(self, builder);
159 value = mcall0s(attr, "default");
161 if(IsCodeRef(value)){
162 value = mcall0(self, value);
166 /* apply coerce and type constraint */
167 if(flags & MOUSEf_ATTR_HAS_TC){
168 value = mouse_apply_type_constraint(aTHX_ xa, value, flags);
171 /* store value to slot */
172 value = set_slot(self, slot, value);
175 PUSH_VALUE(value, flags);
179 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
180 U16 const flags = MOUSE_mg_flags(mg);
181 SV* const slot = MOUSE_mg_slot(mg);
183 if(flags & MOUSEf_ATTR_HAS_TC){
184 value = mouse_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
187 set_slot(self, slot, value);
189 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
190 weaken_slot(self, slot);
193 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
194 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
203 call_sv(trigger, G_VOID | G_DISCARD);
204 /* need not SPAGAIN */
207 PUSH_VALUE(value, flags);
210 XS(XS_Mouse_accessor)
214 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
216 SP -= items; /* PPCODE */
219 if(items == 1){ /* reader */
220 mouse_attr_get(aTHX_ self, mg);
222 else if (items == 2){ /* writer */
223 mouse_attr_set(aTHX_ self, mg, ST(1));
226 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
227 "Expected exactly one or two argument for an accessor");
236 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
239 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
240 "Cannot assign a value to a read-only accessor");
243 SP -= items; /* PPCODE */
246 mouse_attr_get(aTHX_ self, mg);
253 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
256 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
257 "Too few arguments for a write-only accessor");
260 SP -= items; /* PPCODE */
263 mouse_attr_set(aTHX_ self, mg, ST(1));
266 /* simple accessors */
270 mouse_accessor_get_mg(pTHX_ CV* const xsub){
271 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
276 mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl){
277 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
278 SV* const slot = newSVpvn_share(key, keylen, 0U);
282 /* anonymous xsubs need sv_2mortal */
283 sv_2mortal((SV*)xsub);
286 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, NULL, 0);
287 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
290 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
292 CvXSUBANY(xsub).any_ptr = (void*)mg;
297 XS(XS_Mouse_simple_reader)
301 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
305 croak("Expected exactly one argument for a reader for '%"SVf"'", slot);
308 value = get_slot(self, slot);
309 ST(0) = value ? value : &PL_sv_undef;
314 XS(XS_Mouse_simple_writer)
318 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
321 croak("Expected exactly two argument for a writer for '%"SVf"'", slot);
324 ST(0) = set_slot(self, slot, ST(1));
328 XS(XS_Mouse_simple_clearer)
332 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
336 croak("Expected exactly one argument for a clearer for '%"SVf"'", slot);
339 value = delete_slot(self, slot);
340 ST(0) = value ? value : &PL_sv_undef;
344 XS(XS_Mouse_simple_predicate)
348 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
351 croak("Expected exactly one argument for a predicate for '%"SVf"'", slot);
354 ST(0) = boolSV( has_slot(self, slot) );
358 /* simple instance slot accessor (or Mouse::Meta::Instance) */
361 mouse_instance_create(pTHX_ HV* const stash) {
363 assert(SvTYPE(stash) == SVt_PVHV);
364 return sv_bless( newRV_noinc((SV*)newHV()), stash );
368 mouse_instance_clone(pTHX_ SV* const instance) {
372 CHECK_INSTANCE(instance);
373 proto = newHVhv((HV*)SvRV(instance));
374 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
378 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
381 CHECK_INSTANCE(instance);
382 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
386 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
390 CHECK_INSTANCE(instance);
391 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
392 return he ? HeVAL(he) : NULL;
396 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
402 CHECK_INSTANCE(instance);
403 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
405 sv_setsv_mg(sv, value);
410 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
413 CHECK_INSTANCE(instance);
414 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
418 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
422 CHECK_INSTANCE(instance);
423 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
425 sv_rvweaken(HeVAL(he));
429 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
432 VERSIONCHECK: DISABLE
435 _generate_accessor(klass, SV* attr, metaclass)
438 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_accessor);
444 _generate_reader(klass, SV* attr, metaclass)
447 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_reader);
453 _generate_writer(klass, SV* attr, metaclass)
456 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_writer);
462 _generate_clearer(klass, SV* attr, metaclass)
465 SV* const slot = mcall0s(attr, "name");
467 const char* const pv = SvPV_const(slot, len);
468 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer);
474 _generate_predicate(klass, SV* attr, metaclass)
477 SV* const slot = mcall0s(attr, "name");
479 const char* const pv = SvPV_const(slot, len);
480 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate);