3 #define CHECK_INSTANCE(instance) STMT_START{ \
5 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
6 croak("Invalid object instance: '%"SVf"'", instance); \
11 #define MOUSE_mg_attribute(mg) MOUSE_xa_attribute(MOUSE_mg_xa(mg))
13 static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */
15 #define dMOUSE_self SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv)
18 mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
20 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
23 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
24 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
32 mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
33 AV* const xa = mouse_get_xa(aTHX_ attr);
37 xsub = newXS(NULL, accessor_impl, __FILE__);
38 sv_2mortal((SV*)xsub);
40 mg = sv_magicext((SV*)xsub, MOUSE_xa_slot(xa),
41 PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
43 MOUSE_mg_flags(mg) = (U16)MOUSE_xa_flags(xa);
46 * although we use MAGIC for gc, we also store mg to
47 * CvXSUBANY for efficiency (gfx)
49 CvXSUBANY(xsub).any_ptr = (void*)mg;
55 #define PUSH_VALUE(value, flags) STMT_START { \
56 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
57 mouse_push_values(aTHX_ value, (flags)); \
61 XPUSHs(value ? value : &PL_sv_undef); \
66 /* pushes return values, does auto-deref if needed */
68 mouse_push_values(pTHX_ SV* const value, U16 const flags){
71 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
73 if(!(value && SvOK(value))){
77 if(flags & MOUSEf_TC_IS_ARRAYREF){
82 if(!IsArrayRef(value)){
83 croak("Mouse-panic: Not an ARRAY reference");
86 av = (AV*)SvRV(value);
89 for(i = 0; i < len; i++){
90 SV** const svp = av_fetch(av, i, FALSE);
91 PUSHs(svp ? *svp : &PL_sv_undef);
98 assert(flags & MOUSEf_TC_IS_HASHREF);
100 if(!IsHashRef(value)){
101 croak("Mouse-panic: Not a HASH reference");
104 hv = (HV*)SvRV(value);
106 while((he = hv_iternext(hv))){
108 PUSHs(hv_iterkeysv(he));
109 PUSHs(hv_iterval(hv, he));
117 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
118 U16 const flags = MOUSE_mg_flags(mg);
121 value = get_slot(self, MOUSE_mg_slot(mg));
124 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
125 value = mouse_xa_set_default(aTHX_ MOUSE_mg_xa(mg), self);
128 PUSH_VALUE(value, flags);
132 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
133 U16 const flags = MOUSE_mg_flags(mg);
134 SV* const slot = MOUSE_mg_slot(mg);
136 if(flags & MOUSEf_ATTR_HAS_TC){
137 value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
140 value = set_slot(self, slot, value);
142 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
143 weaken_slot(self, slot);
146 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
147 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
150 /* NOTE: triggers can remove value, so
151 value must be copied here,
152 revealed by Net::Google::DataAPI (DANJOU).
154 value = sv_mortalcopy(value);
162 call_sv_safe(trigger, G_VOID | G_DISCARD);
163 /* need not SPAGAIN */
165 assert(SvTYPE(value) != SVTYPEMASK);
168 PUSH_VALUE(value, flags);
171 XS(XS_Mouse_accessor)
175 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
177 SP -= items; /* PPCODE */
180 if(items == 1){ /* reader */
181 mouse_attr_get(aTHX_ self, mg);
183 else if (items == 2){ /* writer */
184 mouse_attr_set(aTHX_ self, mg, ST(1));
187 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
188 "Expected exactly one or two argument for an accessor of %"SVf,
198 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
201 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
202 "Cannot assign a value to a read-only accessor of %"SVf,
206 SP -= items; /* PPCODE */
209 mouse_attr_get(aTHX_ self, mg);
216 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
219 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
220 "Too few arguments for a write-only accessor of %"SVf,
224 SP -= items; /* PPCODE */
227 mouse_attr_set(aTHX_ self, mg, ST(1));
230 /* simple accessors */
234 mouse_accessor_get_mg(pTHX_ CV* const xsub){
235 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
240 mouse_simple_accessor_generate(pTHX_
241 const char* const fq_name, const char* const key, I32 const keylen,
242 XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen) {
243 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
244 SV* const slot = newSVpvn_share(key, keylen, 0U);
248 /* anonymous xsubs need sv_2mortal() */
249 sv_2mortal((SV*)xsub);
252 mg = sv_magicext((SV*)xsub, slot,
253 PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)dptr, dlen);
255 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
256 if(dlen == HEf_SVKEY){
261 * although we use MAGIC for gc, we also store mg to CvXSUBANY
262 * for efficiency (gfx)
264 CvXSUBANY(xsub).any_ptr = (void*)mg;
269 XS(XS_Mouse_simple_reader)
273 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
277 croak("Expected exactly one argument for a reader of %"SVf,
281 value = get_slot(self, MOUSE_mg_slot(mg));
283 if(MOUSE_mg_ptr(mg)){
284 /* the default value must be a SV */
285 assert(MOUSE_mg_len(mg) == HEf_SVKEY);
286 value = (SV*)MOUSE_mg_ptr(mg);
289 value = &PL_sv_undef;
298 XS(XS_Mouse_simple_writer)
302 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
305 croak("Expected exactly two argument for a writer of %"SVf,
309 ST(0) = set_slot(self, slot, ST(1));
313 XS(XS_Mouse_simple_clearer)
317 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
321 croak("Expected exactly one argument for a clearer of %"SVf,
325 value = delete_slot(self, slot);
326 ST(0) = value ? value : &PL_sv_undef;
330 XS(XS_Mouse_simple_predicate)
334 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
337 croak("Expected exactly one argument for a predicate of %"SVf, slot);
340 ST(0) = boolSV( has_slot(self, slot) );
344 /* Class::Data::Inheritable-like class accessor */
345 XS(XS_Mouse_inheritable_class_accessor) {
348 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
352 if(items == 1){ /* reader */
355 else if (items == 2){ /* writer */
359 croak("Expected exactly one or two argument for a class data accessor"
361 value = NULL; /* -Wuninitialized */
364 stash = mouse_get_namespace(aTHX_ self);
366 if(!value) { /* reader */
367 value = get_slot(self, slot);
369 AV* const isa = mro_get_linear_isa(stash);
370 I32 const len = av_len(isa) + 1;
372 for(i = 1; i < len; i++) {
373 SV* const klass = MOUSE_av_at(isa, i);
374 SV* const meta = get_metaclass(klass);
376 continue; /* skip non-Mouse classes */
378 value = get_slot(meta, slot);
384 value = &PL_sv_undef;
389 set_slot(self, slot, value);
390 mro_method_changed_in(stash);
397 /* simple instance slot accessor (or Mouse::Meta::Instance) */
400 mouse_instance_create(pTHX_ HV* const stash) {
403 assert(SvTYPE(stash) == SVt_PVHV);
404 instance = sv_bless( newRV_noinc((SV*)newHV()), stash );
405 return sv_2mortal(instance);
409 mouse_instance_clone(pTHX_ SV* const instance) {
411 CHECK_INSTANCE(instance);
412 assert(SvOBJECT(SvRV(instance)));
414 proto = newRV_noinc((SV*)newHVhv((HV*)SvRV(instance)));
415 sv_bless(proto, SvSTASH(SvRV(instance)));
416 return sv_2mortal(proto);
420 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
422 CHECK_INSTANCE(instance);
423 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
427 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
430 CHECK_INSTANCE(instance);
431 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
432 return he ? HeVAL(he) : NULL;
436 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
441 CHECK_INSTANCE(instance);
442 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
450 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
453 CHECK_INSTANCE(instance);
454 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
458 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
461 CHECK_INSTANCE(instance);
462 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
464 sv_rvweaken(HeVAL(he));
468 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
471 VERSIONCHECK: DISABLE
474 _generate_accessor(klass, SV* attr, metaclass)
477 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_accessor);
483 _generate_reader(klass, SV* attr, metaclass)
486 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_reader);
492 _generate_writer(klass, SV* attr, metaclass)
495 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_writer);
501 _generate_clearer(klass, SV* attr, metaclass)
504 SV* const slot = mcall0(attr, mouse_name);
506 const char* const pv = SvPV_const(slot, len);
507 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0);
513 _generate_predicate(klass, SV* attr, metaclass)
516 SV* const slot = mcall0(attr, mouse_name);
518 const char* const pv = SvPV_const(slot, len);
519 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0);