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)
17 /* simple instance slot accessor (or Mouse::Meta::Instance) */
20 mouse_instance_create(pTHX_ HV* const stash) {
23 assert(SvTYPE(stash) == SVt_PVHV);
24 instance = sv_bless( newRV_noinc((SV*)newHV()), stash );
25 return sv_2mortal(instance);
29 mouse_instance_clone(pTHX_ SV* const instance) {
31 CHECK_INSTANCE(instance);
32 assert(SvOBJECT(SvRV(instance)));
34 proto = newRV_noinc((SV*)newHVhv((HV*)SvRV(instance)));
35 sv_bless(proto, SvSTASH(SvRV(instance)));
36 return sv_2mortal(proto);
40 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
42 CHECK_INSTANCE(instance);
43 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
47 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
50 CHECK_INSTANCE(instance);
51 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
52 return he ? HeVAL(he) : NULL;
56 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
61 CHECK_INSTANCE(instance);
62 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
70 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
73 CHECK_INSTANCE(instance);
74 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
78 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
81 CHECK_INSTANCE(instance);
82 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
84 sv_rvweaken(HeVAL(he));
91 mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
93 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
95 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
96 * before calling methods, so SvGETMAGIC(self) is not required here.
103 mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
104 AV* const xa = mouse_get_xa(aTHX_ attr);
108 xsub = newXS(NULL, accessor_impl, __FILE__);
109 sv_2mortal((SV*)xsub);
111 mg = sv_magicext((SV*)xsub, MOUSE_xa_slot(xa),
112 PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
114 MOUSE_mg_flags(mg) = (U16)MOUSE_xa_flags(xa);
117 * although we use MAGIC for gc, we also store mg to
118 * CvXSUBANY for efficiency (gfx)
120 CvXSUBANY(xsub).any_ptr = (void*)mg;
126 /* pushes return values, does auto-deref if needed */
128 mouse_push_values(pTHX_ SV* const value, U16 const flags){
131 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
133 if(!(value && SvOK(value))){
137 if(flags & MOUSEf_TC_IS_ARRAYREF){
142 if(!IsArrayRef(value)){
143 croak("Mouse-panic: Not an ARRAY reference");
146 av = (AV*)SvRV(value);
147 len = av_len(av) + 1;
149 for(i = 0; i < len; i++){
150 SV** const svp = av_fetch(av, i, FALSE);
151 PUSHs(svp ? *svp : &PL_sv_undef);
158 assert(flags & MOUSEf_TC_IS_HASHREF);
160 if(!IsHashRef(value)){
161 croak("Mouse-panic: Not a HASH reference");
164 hv = (HV*)SvRV(value);
166 while((he = hv_iternext(hv))){
168 PUSHs(hv_iterkeysv(he));
169 PUSHs(hv_iterval(hv, he));
177 mouse_push_value(pTHX_ SV* const value, U16 const flags) {
178 if(flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){
179 mouse_push_values(aTHX_ value, flags);
183 XPUSHs(value ? value : &PL_sv_undef);
189 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
190 U16 const flags = MOUSE_mg_flags(mg);
193 value = get_slot(self, MOUSE_mg_slot(mg));
196 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
197 value = mouse_xa_set_default(aTHX_ MOUSE_mg_xa(mg), self);
200 mouse_push_value(aTHX_ value, flags);
204 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
205 U16 const flags = MOUSE_mg_flags(mg);
206 SV* const slot = MOUSE_mg_slot(mg);
208 if(flags & MOUSEf_ATTR_HAS_TC){
209 value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
212 value = set_slot(self, slot, value);
214 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
215 weaken_slot(self, slot);
218 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
219 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
222 /* NOTE: triggers can remove value, so
223 value must be copied here,
224 revealed by Net::Google::DataAPI (DANJOU).
226 value = sv_mortalcopy(value);
234 call_sv_safe(trigger, G_VOID | G_DISCARD);
235 /* need not SPAGAIN */
237 assert(SvTYPE(value) != SVTYPEMASK);
240 mouse_push_value(aTHX_ value, flags);
243 XS(XS_Mouse_accessor)
247 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
249 SP -= items; /* PPCODE */
252 if(items == 1){ /* reader */
253 mouse_attr_get(aTHX_ self, mg);
255 else if (items == 2){ /* writer */
256 mouse_attr_set(aTHX_ self, mg, ST(1));
259 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
260 "Expected exactly one or two argument for an accessor of %"SVf,
270 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
273 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
274 "Cannot assign a value to a read-only accessor of %"SVf,
278 SP -= items; /* PPCODE */
281 mouse_attr_get(aTHX_ self, mg);
288 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
291 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
292 "Too few arguments for a write-only accessor of %"SVf,
296 SP -= items; /* PPCODE */
299 mouse_attr_set(aTHX_ self, mg, ST(1));
302 /* simple accessors */
306 mouse_accessor_get_mg(pTHX_ CV* const xsub){
307 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
312 mouse_simple_accessor_generate(pTHX_
313 const char* const fq_name, const char* const key, I32 const keylen,
314 XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen) {
315 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
316 SV* const slot = newSVpvn_share(key, keylen, 0U);
320 /* anonymous xsubs need sv_2mortal() */
321 sv_2mortal((SV*)xsub);
324 mg = sv_magicext((SV*)xsub, slot,
325 PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)dptr, dlen);
327 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
328 if(dlen == HEf_SVKEY){
333 * although we use MAGIC for gc, we also store mg to CvXSUBANY
334 * for efficiency (gfx)
336 CvXSUBANY(xsub).any_ptr = (void*)mg;
341 XS(XS_Mouse_simple_reader)
345 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
349 croak("Expected exactly one argument for a reader of %"SVf,
353 value = get_slot(self, MOUSE_mg_slot(mg));
355 if(MOUSE_mg_ptr(mg)){
356 /* the default value must be a SV */
357 assert(MOUSE_mg_len(mg) == HEf_SVKEY);
358 value = (SV*)MOUSE_mg_ptr(mg);
361 value = &PL_sv_undef;
370 XS(XS_Mouse_simple_writer)
374 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
377 croak("Expected exactly two argument for a writer of %"SVf,
381 ST(0) = set_slot(self, slot, ST(1));
385 XS(XS_Mouse_simple_clearer)
389 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
393 croak("Expected exactly one argument for a clearer of %"SVf,
397 value = delete_slot(self, slot);
398 ST(0) = value ? value : &PL_sv_undef;
402 XS(XS_Mouse_simple_predicate)
406 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
409 croak("Expected exactly one argument for a predicate of %"SVf, slot);
412 ST(0) = boolSV( has_slot(self, slot) );
416 /* Class::Data::Inheritable-like class accessor */
417 XS(XS_Mouse_inheritable_class_accessor) {
420 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
424 if(items == 1){ /* reader */
427 else if (items == 2){ /* writer */
431 croak("Expected exactly one or two argument for a class data accessor"
433 value = NULL; /* -Wuninitialized */
436 stash = mouse_get_namespace(aTHX_ self);
438 if(!value) { /* reader */
439 value = get_slot(self, slot);
441 AV* const isa = mro_get_linear_isa(stash);
442 I32 const len = av_len(isa) + 1;
444 for(i = 1; i < len; i++) {
445 SV* const klass = MOUSE_av_at(isa, i);
446 SV* const meta = get_metaclass(klass);
448 continue; /* skip non-Mouse classes */
450 value = get_slot(meta, slot);
456 value = &PL_sv_undef;
461 set_slot(self, slot, value);
462 mro_method_changed_in(stash);
470 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
473 VERSIONCHECK: DISABLE
476 _generate_accessor(klass, SV* attr, metaclass)
479 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_accessor);
485 _generate_reader(klass, SV* attr, metaclass)
488 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_reader);
494 _generate_writer(klass, SV* attr, metaclass)
497 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_writer);
503 _generate_clearer(klass, SV* attr, metaclass)
506 SV* const slot = mcall0(attr, mouse_name);
508 const char* const pv = SvPV_const(slot, len);
509 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0);
515 _generate_predicate(klass, SV* attr, metaclass)
518 SV* const slot = mcall0(attr, mouse_name);
520 const char* const pv = SvPV_const(slot, len);
521 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0);