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)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 #define PUSH_VALUE(value, flags) STMT_START { \
52 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
53 mouse_push_values(aTHX_ value, (flags)); \
57 XPUSHs(value ? value : &PL_sv_undef); \
62 /* pushes return values, does auto-deref if needed */
64 mouse_push_values(pTHX_ SV* const value, U16 const flags){
67 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
69 if(!(value && SvOK(value))){
73 if(flags & MOUSEf_TC_IS_ARRAYREF){
78 if(!IsArrayRef(value)){
79 croak("Mouse-panic: Not an ARRAY reference");
82 av = (AV*)SvRV(value);
85 for(i = 0; i < len; i++){
86 SV** const svp = av_fetch(av, i, FALSE);
87 PUSHs(svp ? *svp : &PL_sv_undef);
90 else if(flags & MOUSEf_TC_IS_HASHREF){
94 if(!IsHashRef(value)){
95 croak("Mouse-panic: Not a HASH reference");
98 hv = (HV*)SvRV(value);
100 while((he = hv_iternext(hv))){
102 PUSHs(hv_iterkeysv(he));
103 PUSHs(hv_iterval(hv, he));
111 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
112 U16 const flags = MOUSE_mg_flags(mg);
115 value = get_slot(self, MOUSE_mg_slot(mg));
118 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
119 value = mouse_xa_set_default(aTHX_ MOUSE_mg_xa(mg), self);
122 PUSH_VALUE(value, flags);
126 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
127 U16 const flags = MOUSE_mg_flags(mg);
128 SV* const slot = MOUSE_mg_slot(mg);
130 if(flags & MOUSEf_ATTR_HAS_TC){
131 value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
134 set_slot(self, slot, value);
136 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
137 weaken_slot(self, slot);
140 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
141 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
150 call_sv(trigger, G_VOID | G_DISCARD);
151 /* need not SPAGAIN */
154 PUSH_VALUE(value, flags);
157 XS(XS_Mouse_accessor)
161 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
163 SP -= items; /* PPCODE */
166 if(items == 1){ /* reader */
167 mouse_attr_get(aTHX_ self, mg);
169 else if (items == 2){ /* writer */
170 mouse_attr_set(aTHX_ self, mg, ST(1));
173 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
174 "Expected exactly one or two argument for an accessor");
183 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
186 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
187 "Cannot assign a value to a read-only accessor");
190 SP -= items; /* PPCODE */
193 mouse_attr_get(aTHX_ self, mg);
200 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
203 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
204 "Too few arguments for a write-only accessor");
207 SP -= items; /* PPCODE */
210 mouse_attr_set(aTHX_ self, mg, ST(1));
213 /* simple accessors */
217 mouse_accessor_get_mg(pTHX_ CV* const xsub){
218 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
223 mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl){
224 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
225 SV* const slot = newSVpvn_share(key, keylen, 0U);
229 /* anonymous xsubs need sv_2mortal */
230 sv_2mortal((SV*)xsub);
233 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, NULL, 0);
234 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
237 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
239 CvXSUBANY(xsub).any_ptr = (void*)mg;
244 XS(XS_Mouse_simple_reader)
248 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
252 croak("Expected exactly one argument for a reader for '%"SVf"'", slot);
255 value = get_slot(self, slot);
256 ST(0) = value ? value : &PL_sv_undef;
261 XS(XS_Mouse_simple_writer)
265 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
268 croak("Expected exactly two argument for a writer for '%"SVf"'", slot);
271 ST(0) = set_slot(self, slot, ST(1));
275 XS(XS_Mouse_simple_clearer)
279 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
283 croak("Expected exactly one argument for a clearer for '%"SVf"'", slot);
286 value = delete_slot(self, slot);
287 ST(0) = value ? value : &PL_sv_undef;
291 XS(XS_Mouse_simple_predicate)
295 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
298 croak("Expected exactly one argument for a predicate for '%"SVf"'", slot);
301 ST(0) = boolSV( has_slot(self, slot) );
305 /* simple instance slot accessor (or Mouse::Meta::Instance) */
308 mouse_instance_create(pTHX_ HV* const stash) {
310 assert(SvTYPE(stash) == SVt_PVHV);
311 return sv_bless( newRV_noinc((SV*)newHV()), stash );
315 mouse_instance_clone(pTHX_ SV* const instance) {
319 CHECK_INSTANCE(instance);
320 proto = newHVhv((HV*)SvRV(instance));
321 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
325 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
328 CHECK_INSTANCE(instance);
329 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
333 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
337 CHECK_INSTANCE(instance);
338 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
339 return he ? HeVAL(he) : NULL;
343 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
349 CHECK_INSTANCE(instance);
350 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
352 sv_setsv_mg(sv, value);
357 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
360 CHECK_INSTANCE(instance);
361 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
365 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
369 CHECK_INSTANCE(instance);
370 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
372 sv_rvweaken(HeVAL(he));
376 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
379 VERSIONCHECK: DISABLE
382 _generate_accessor(klass, SV* attr, metaclass)
385 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_accessor);
391 _generate_reader(klass, SV* attr, metaclass)
394 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_reader);
400 _generate_writer(klass, SV* attr, metaclass)
403 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, XS_Mouse_writer);
409 _generate_clearer(klass, SV* attr, metaclass)
412 SV* const slot = mcall0s(attr, "name");
414 const char* const pv = SvPV_const(slot, len);
415 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer);
421 _generate_predicate(klass, SV* attr, metaclass)
424 SV* const slot = mcall0s(attr, "name");
426 const char* const pv = SvPV_const(slot, len);
427 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate);