3 #define CHECK_INSTANCE(instance) STMT_START{ \
4 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
5 croak("Invalid object for instance managers"); \
9 /* Moose XS Attribute object */
18 #define MOUSE_xa_attribute(m) MOUSE_av_at(m, MOUSE_XA_ATTRIBUTE)
19 #define MOUSE_xa_tc(m) MOUSE_av_at(m, MOUSE_XA_TC)
20 #define MOUSE_xa_tc_code(m) MOUSE_av_at(m, MOUSE_XA_TC_CODE)
22 #define MOUSE_mg_attribute(mg) MOUSE_xa_attribute(MOUSE_mg_xa(mg))
24 enum mouse_xa_flags_t{
25 MOUSEf_ATTR_HAS_TC = 0x0001,
26 MOUSEf_ATTR_HAS_DEFAULT = 0x0002,
27 MOUSEf_ATTR_HAS_BUILDER = 0x0004,
28 MOUSEf_ATTR_HAS_INITIALIZER = 0x0008, /* not used in Mouse */
29 MOUSEf_ATTR_HAS_TRIGGER = 0x0010,
31 MOUSEf_ATTR_IS_LAZY = 0x0020,
32 MOUSEf_ATTR_IS_WEAK_REF = 0x0040,
33 MOUSEf_ATTR_IS_REQUIRED = 0x0080,
35 MOUSEf_ATTR_SHOULD_COERCE = 0x0100,
37 MOUSEf_ATTR_SHOULD_AUTO_DEREF
39 MOUSEf_TC_IS_ARRAYREF = 0x0400,
40 MOUSEf_TC_IS_HASHREF = 0x0800,
42 MOUSEf_OTHER1 = 0x1000,
43 MOUSEf_OTHER2 = 0x2000,
44 MOUSEf_OTHER3 = 0x4000,
45 MOUSEf_OTHER4 = 0x8000,
47 MOUSEf_MOUSE_MASK = 0xFFFF /* not used */
50 static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */
54 mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
58 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
61 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
62 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
67 croak("Cant call %s as a class method", GvNAME(CvGV(cv)));
74 mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
75 SV* const slot = mcall0(attr, mouse_name);
76 AV* const xa = newAV();
83 xsub = newXS(NULL, accessor_impl, __FILE__);
84 sv_2mortal((SV*)xsub);
86 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
89 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
91 CvXSUBANY(xsub).any_ptr = (void*)mg;
93 av_extend(xa, MOUSE_XA_last - 1);
95 av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
97 /* prepare attribute status */
98 /* XXX: making it lazy is a good way? */
100 if(SvTRUEx(mcall0s(attr, "has_type_constraint"))){
102 flags |= MOUSEf_ATTR_HAS_TC;
107 tc = mcall0s(attr, "type_constraint");
108 av_store(xa, MOUSE_XA_TC, newSVsv(tc));
110 if(SvTRUEx(mcall0s(attr, "should_auto_deref"))){
111 flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
112 if( SvTRUEx(mcall1s(tc, "is_a_type_of", newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
113 flags |= MOUSEf_TC_IS_ARRAYREF;
115 else if( SvTRUEx(mcall1s(tc, "is_a_type_of", newSVpvs_flags("HashRef", SVs_TEMP))) ){
116 flags |= MOUSEf_TC_IS_HASHREF;
119 mouse_throw_error(attr, tc,
120 "Can not auto de-reference the type constraint '%"SVf"'",
121 mcall0(tc, mouse_name));
125 if(SvTRUEx(mcall0s(attr, "should_coerce"))){
126 flags |= MOUSEf_ATTR_SHOULD_COERCE;
133 if(SvTRUEx(mcall0s(attr, "has_trigger"))){
134 flags |= MOUSEf_ATTR_HAS_TRIGGER;
137 if(SvTRUEx(mcall0s(attr, "is_lazy"))){
138 flags |= MOUSEf_ATTR_IS_LAZY;
140 if(SvTRUEx(mcall0s(attr, "has_builder"))){
141 flags |= MOUSEf_ATTR_HAS_BUILDER;
143 else if(SvTRUEx(mcall0s(attr, "has_default"))){
144 flags |= MOUSEf_ATTR_HAS_DEFAULT;
148 if(SvTRUEx(mcall0s(attr, "is_weak_ref"))){
149 flags |= MOUSEf_ATTR_IS_WEAK_REF;
152 if(SvTRUEx(mcall0s(attr, "is_required"))){
153 flags |= MOUSEf_ATTR_IS_REQUIRED;
156 MOUSE_mg_flags(mg) = flags;
162 mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
163 SV* const tc = MOUSE_xa_tc(xa);
166 if(flags & MOUSEf_ATTR_SHOULD_COERCE){
167 value = mcall1s(tc, "coerce", value);
170 if(!SvOK(MOUSE_xa_tc_code(xa))){
171 XS(XS_Mouse__Util__TypeConstraints_Item); /* prototype defined in Mouse.xs */
173 tc_code = mcall0s(tc, "_compiled_type_constraint");
175 if(SvROK(tc_code) && SvTYPE(SvRV(tc_code))
176 && CvXSUB((CV*)SvRV(tc_code)) == XS_Mouse__Util__TypeConstraints_Item){
177 /* built-in type constraints */
178 mouse_tc const id = CvXSUBANY((CV*)SvRV(tc_code)).any_i32;
179 av_store(xa, MOUSE_XA_TC_CODE, newSViv(id));
182 av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
186 tc_code = MOUSE_xa_tc_code(xa);
189 if(!mouse_tc_check(aTHX_ tc_code, value)){
190 mouse_throw_error(MOUSE_xa_attribute(xa), value,
191 "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
192 mcall0(MOUSE_xa_attribute(xa), mouse_name),
193 mcall1s(tc, "get_message", value));
199 #define PUSH_VALUE(value, flags) STMT_START { \
200 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
201 mouse_push_values(aTHX_ value, (flags)); \
205 XPUSHs(value ? value : &PL_sv_undef); \
210 /* pushes return values, does auto-deref if needed */
212 mouse_push_values(pTHX_ SV* const value, U16 const flags){
215 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
217 if(!(value && SvOK(value))){
221 if(flags & MOUSEf_TC_IS_ARRAYREF){
222 AV* const av = (AV*)SvRV(value);
226 if(SvTYPE(av) != SVt_PVAV){
227 croak("Mouse-panic: Not an ARRAY reference");
230 len = av_len(av) + 1;
232 for(i = 0; i < len; i++){
233 SV** const svp = av_fetch(av, i, FALSE);
234 PUSHs(svp ? *svp : &PL_sv_undef);
237 else if(flags & MOUSEf_TC_IS_HASHREF){
238 HV* const hv = (HV*)SvRV(value);
241 if(SvTYPE(hv) != SVt_PVHV){
242 croak("Mouse-panic: Not a HASH reference");
246 while((he = hv_iternext(hv))){
248 PUSHs(hv_iterkeysv(he));
249 PUSHs(hv_iterval(hv, he));
257 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
258 U16 const flags = MOUSE_mg_flags(mg);
259 SV* const slot = MOUSE_mg_slot(mg);
262 value = mouse_instance_get_slot(aTHX_ self, slot);
265 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
266 AV* const xa = MOUSE_mg_xa(mg);
267 SV* const attr = MOUSE_xa_attribute(xa);
269 /* get default value by $attr->default or $attr->builder */
270 if(flags & MOUSEf_ATTR_HAS_DEFAULT){
271 value = mcall0s(attr, "default");
273 if(SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVCV){
274 value = mcall0(self, value);
277 else if(flags & MOUSEf_ATTR_HAS_BUILDER){
278 SV* const builder = mcall0s(attr, "builder");
279 value = mcall0(self, builder);
283 value = sv_newmortal();
286 /* apply coerce and type constraint */
287 if(flags & MOUSEf_ATTR_HAS_TC){
288 value = mouse_apply_type_constraint(aTHX_ xa, value, flags);
291 /* store value to slot */
292 value = mouse_instance_set_slot(aTHX_ self, slot, value);
295 PUSH_VALUE(value, flags);
299 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
300 U16 const flags = MOUSE_mg_flags(mg);
301 SV* const slot = MOUSE_mg_slot(mg);
303 if(flags & MOUSEf_ATTR_HAS_TC){
304 value = mouse_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
307 mouse_instance_set_slot(aTHX_ self, slot, value);
309 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
310 mouse_instance_weaken_slot(aTHX_ self, slot);
313 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
314 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
323 call_sv(trigger, G_VOID | G_DISCARD);
324 /* need not SPAGAIN */
327 PUSH_VALUE(value, flags);
330 XS(mouse_xs_accessor)
334 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
336 SP -= items; /* PPCODE */
339 if(items == 1){ /* reader */
340 mouse_attr_get(aTHX_ self, mg);
342 else if (items == 2){ /* writer */
343 mouse_attr_set(aTHX_ self, mg, ST(1));
346 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
347 "Expected exactly one or two argument for an accessor");
356 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
359 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
360 "Cannot assign a value to a read-only accessor");
363 SP -= items; /* PPCODE */
366 mouse_attr_get(aTHX_ self, mg);
373 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
376 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
377 "Too few arguments for a write-only accessor");
380 SP -= items; /* PPCODE */
383 mouse_attr_set(aTHX_ self, mg, ST(1));
386 /* simple accessors */
390 mouse_accessor_get_mg(pTHX_ CV* const xsub){
391 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
396 mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl){
397 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
398 SV* const slot = newSVpvn_share(key, keylen, 0U);
402 /* anonymous xsubs need sv_2mortal */
403 sv_2mortal((SV*)xsub);
406 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, NULL, 0);
407 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
410 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
412 CvXSUBANY(xsub).any_ptr = (void*)mg;
417 XS(mouse_xs_simple_reader)
421 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
425 croak("Expected exactly one argument for a reader for '%"SVf"'", slot);
428 value = mouse_instance_get_slot(aTHX_ self, slot);
429 ST(0) = value ? value : &PL_sv_undef;
434 XS(mouse_xs_simple_writer)
438 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
441 croak("Expected exactly two argument for a writer for '%"SVf"'", slot);
444 ST(0) = mouse_instance_set_slot(aTHX_ self, slot, ST(1));
448 XS(mouse_xs_simple_clearer)
452 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
456 croak("Expected exactly one argument for a clearer for '%"SVf"'", slot);
459 value = mouse_instance_delete_slot(aTHX_ self, slot);
460 ST(0) = value ? value : &PL_sv_undef;
464 XS(mouse_xs_simple_predicate)
468 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
471 croak("Expected exactly one argument for a predicate for '%"SVf"'", slot);
474 ST(0) = boolSV( mouse_instance_has_slot(aTHX_ self, slot) );
478 /* simple instance slot accessor */
481 mouse_instance_create(pTHX_ HV* const stash) {
483 return sv_bless( newRV_noinc((SV*)newHV()), stash );
487 mouse_instance_clone(pTHX_ SV* const instance) {
491 CHECK_INSTANCE(instance);
492 proto = newHVhv((HV*)SvRV(instance));
493 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
497 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
500 CHECK_INSTANCE(instance);
501 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
505 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
509 CHECK_INSTANCE(instance);
510 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
511 return he ? HeVAL(he) : NULL;
515 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
521 CHECK_INSTANCE(instance);
522 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
524 sv_setsv_mg(sv, value);
529 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
532 CHECK_INSTANCE(instance);
533 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
537 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
541 CHECK_INSTANCE(instance);
542 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
544 sv_rvweaken(HeVAL(he));
548 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
551 VERSIONCHECK: DISABLE
554 _generate_accessor(klass, SV* attr, metaclass)
557 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_accessor);
563 _generate_reader(klass, SV* attr, metaclass)
566 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_reader);
572 _generate_writer(klass, SV* attr, metaclass)
575 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_writer);
581 _generate_clearer(klass, SV* attr, metaclass)
584 SV* const slot = mcall0s(attr, "name");
586 const char* const pv = SvPV_const(slot, len);
587 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_clearer);
593 _generate_predicate(klass, SV* attr, metaclass)
596 SV* const slot = mcall0s(attr, "name");
598 const char* const pv = SvPV_const(slot, len);
599 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_predicate);