3 #define CHECK_INSTANCE(instance) STMT_START{ \
4 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
5 croak("Invalid object instance: '%"SVf"'", instance); \
10 #define MOUSE_mg_attribute(mg) MOUSE_xa_attribute(MOUSE_mg_xa(mg))
12 static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */
14 #define dMOUSE_self SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv)
17 mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
19 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
22 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
23 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
31 mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
32 AV* const xa = mouse_get_xa(aTHX_ attr);
36 xsub = newXS(NULL, accessor_impl, __FILE__);
37 sv_2mortal((SV*)xsub);
39 mg = sv_magicext((SV*)xsub, MOUSE_xa_slot(xa), PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
41 MOUSE_mg_flags(mg) = (U16)MOUSE_xa_flags(xa);
44 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
46 CvXSUBANY(xsub).any_ptr = (void*)mg;
52 #define PUSH_VALUE(value, flags) STMT_START { \
53 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
54 mouse_push_values(aTHX_ value, (flags)); \
58 XPUSHs(value ? value : &PL_sv_undef); \
63 /* pushes return values, does auto-deref if needed */
65 mouse_push_values(pTHX_ SV* const value, U16 const flags){
68 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
70 if(!(value && SvOK(value))){
74 if(flags & MOUSEf_TC_IS_ARRAYREF){
79 if(!IsArrayRef(value)){
80 croak("Mouse-panic: Not an ARRAY reference");
83 av = (AV*)SvRV(value);
86 for(i = 0; i < len; i++){
87 SV** const svp = av_fetch(av, i, FALSE);
88 PUSHs(svp ? *svp : &PL_sv_undef);
95 assert(flags & MOUSEf_TC_IS_HASHREF);
97 if(!IsHashRef(value)){
98 croak("Mouse-panic: Not a HASH reference");
101 hv = (HV*)SvRV(value);
103 while((he = hv_iternext(hv))){
105 PUSHs(hv_iterkeysv(he));
106 PUSHs(hv_iterval(hv, he));
114 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
115 U16 const flags = MOUSE_mg_flags(mg);
118 value = get_slot(self, MOUSE_mg_slot(mg));
121 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
122 value = mouse_xa_set_default(aTHX_ MOUSE_mg_xa(mg), self);
125 PUSH_VALUE(value, flags);
129 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
130 U16 const flags = MOUSE_mg_flags(mg);
131 SV* const slot = MOUSE_mg_slot(mg);
133 if(flags & MOUSEf_ATTR_HAS_TC){
134 value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
137 value = set_slot(self, slot, value);
139 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
140 weaken_slot(self, slot);
143 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
144 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
147 /* NOTE: triggers can remove value, so
148 value must be copied here,
149 revealed by Net::Google::DataAPI (DANJOU).
151 value = sv_mortalcopy(value);
159 call_sv_safe(trigger, G_VOID | G_DISCARD);
160 /* need not SPAGAIN */
162 assert(SvTYPE(value) != SVTYPEMASK);
165 PUSH_VALUE(value, flags);
168 XS(XS_Mouse_accessor)
172 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
174 SP -= items; /* PPCODE */
177 if(items == 1){ /* reader */
178 mouse_attr_get(aTHX_ self, mg);
180 else if (items == 2){ /* writer */
181 mouse_attr_set(aTHX_ self, mg, ST(1));
184 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
185 "Expected exactly one or two argument for an accessor");
194 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
197 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
198 "Cannot assign a value to a read-only accessor");
201 SP -= items; /* PPCODE */
204 mouse_attr_get(aTHX_ self, mg);
211 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
214 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
215 "Too few arguments for a write-only accessor");
218 SP -= items; /* PPCODE */
221 mouse_attr_set(aTHX_ self, mg, ST(1));
224 /* simple accessors */
228 mouse_accessor_get_mg(pTHX_ CV* const xsub){
229 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
234 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){
235 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
236 SV* const slot = newSVpvn_share(key, keylen, 0U);
240 /* anonymous xsubs need sv_2mortal */
241 sv_2mortal((SV*)xsub);
244 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)dptr, dlen);
245 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
246 if(dlen == HEf_SVKEY){
251 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
253 CvXSUBANY(xsub).any_ptr = (void*)mg;
258 XS(XS_Mouse_simple_reader)
262 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
266 croak("Expected exactly one argument for a reader for '%"SVf"'", MOUSE_mg_slot(mg));
269 value = get_slot(self, MOUSE_mg_slot(mg));
271 if(MOUSE_mg_ptr(mg)){
272 /* the default value must be a SV */
273 assert(MOUSE_mg_len(mg) == HEf_SVKEY);
274 value = (SV*)MOUSE_mg_ptr(mg);
277 value = &PL_sv_undef;
286 XS(XS_Mouse_simple_writer)
290 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
293 croak("Expected exactly two argument for a writer for '%"SVf"'", slot);
296 ST(0) = set_slot(self, slot, ST(1));
300 XS(XS_Mouse_simple_clearer)
304 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
308 croak("Expected exactly one argument for a clearer for '%"SVf"'", slot);
311 value = delete_slot(self, slot);
312 ST(0) = value ? value : &PL_sv_undef;
316 XS(XS_Mouse_simple_predicate)
320 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
323 croak("Expected exactly one argument for a predicate for '%"SVf"'", slot);
326 ST(0) = boolSV( has_slot(self, slot) );
330 /* simple instance slot accessor (or Mouse::Meta::Instance) */
333 mouse_instance_create(pTHX_ HV* const stash) {
335 assert(SvTYPE(stash) == SVt_PVHV);
336 return sv_bless( newRV_noinc((SV*)newHV()), stash );
340 mouse_instance_clone(pTHX_ SV* const instance) {
344 CHECK_INSTANCE(instance);
345 proto = newHVhv((HV*)SvRV(instance));
346 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
350 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
353 CHECK_INSTANCE(instance);
354 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
358 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
362 CHECK_INSTANCE(instance);
363 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
364 return he ? HeVAL(he) : NULL;
368 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
374 CHECK_INSTANCE(instance);
375 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
383 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
386 CHECK_INSTANCE(instance);
387 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
391 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
395 CHECK_INSTANCE(instance);
396 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
398 sv_rvweaken(HeVAL(he));
402 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
405 VERSIONCHECK: DISABLE
408 _generate_accessor(klass, SV* attr, metaclass)
411 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_accessor);
417 _generate_reader(klass, SV* attr, metaclass)
420 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_reader);
426 _generate_writer(klass, SV* attr, metaclass)
429 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_writer);
435 _generate_clearer(klass, SV* attr, metaclass)
438 SV* const slot = mcall0(attr, mouse_name);
440 const char* const pv = SvPV_const(slot, len);
441 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0);
447 _generate_predicate(klass, SV* attr, metaclass)
450 SV* const slot = mcall0(attr, mouse_name);
452 const char* const pv = SvPV_const(slot, len);
453 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0);