3 #define CHECK_INSTANCE(instance) STMT_START{ \
4 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
5 croak("Invalid object instance"); \
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.
70 mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
71 SV* const slot = mcall0(attr, mouse_name);
72 AV* const xa = newAV();
79 xsub = newXS(NULL, accessor_impl, __FILE__);
80 sv_2mortal((SV*)xsub);
82 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
85 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
87 CvXSUBANY(xsub).any_ptr = (void*)mg;
89 av_extend(xa, MOUSE_XA_last - 1);
91 av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
93 /* prepare attribute status */
94 /* XXX: making it lazy is a good way? */
96 if(SvTRUEx(mcall0s(attr, "has_type_constraint"))){
98 flags |= MOUSEf_ATTR_HAS_TC;
103 tc = mcall0s(attr, "type_constraint");
104 av_store(xa, MOUSE_XA_TC, newSVsv(tc));
106 if(SvTRUEx(mcall0s(attr, "should_auto_deref"))){
107 flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
108 if( SvTRUEx(mcall1s(tc, "is_a_type_of", newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
109 flags |= MOUSEf_TC_IS_ARRAYREF;
111 else if( SvTRUEx(mcall1s(tc, "is_a_type_of", newSVpvs_flags("HashRef", SVs_TEMP))) ){
112 flags |= MOUSEf_TC_IS_HASHREF;
115 mouse_throw_error(attr, tc,
116 "Can not auto de-reference the type constraint '%"SVf"'",
117 mcall0(tc, mouse_name));
121 if(SvTRUEx(mcall0s(attr, "should_coerce"))){
122 flags |= MOUSEf_ATTR_SHOULD_COERCE;
129 if(SvTRUEx(mcall0s(attr, "has_trigger"))){
130 flags |= MOUSEf_ATTR_HAS_TRIGGER;
133 if(SvTRUEx(mcall0s(attr, "is_lazy"))){
134 flags |= MOUSEf_ATTR_IS_LAZY;
136 if(SvTRUEx(mcall0s(attr, "has_builder"))){
137 flags |= MOUSEf_ATTR_HAS_BUILDER;
139 else if(SvTRUEx(mcall0s(attr, "has_default"))){
140 flags |= MOUSEf_ATTR_HAS_DEFAULT;
144 if(SvTRUEx(mcall0s(attr, "is_weak_ref"))){
145 flags |= MOUSEf_ATTR_IS_WEAK_REF;
148 if(SvTRUEx(mcall0s(attr, "is_required"))){
149 flags |= MOUSEf_ATTR_IS_REQUIRED;
152 MOUSE_mg_flags(mg) = flags;
158 mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
159 SV* const tc = MOUSE_xa_tc(xa);
162 if(flags & MOUSEf_ATTR_SHOULD_COERCE){
163 value = mcall1s(tc, "coerce", value);
166 if(!SvOK(MOUSE_xa_tc_code(xa))){
167 XS(XS_Mouse__Util__TypeConstraints_Item); /* prototype defined in Mouse.xs */
169 tc_code = mcall0s(tc, "_compiled_type_constraint");
171 if(SvROK(tc_code) && SvTYPE(SvRV(tc_code))
172 && CvXSUB((CV*)SvRV(tc_code)) == XS_Mouse__Util__TypeConstraints_Item){
173 /* built-in type constraints */
174 mouse_tc const id = CvXSUBANY((CV*)SvRV(tc_code)).any_i32;
175 av_store(xa, MOUSE_XA_TC_CODE, newSViv(id));
178 av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
182 tc_code = MOUSE_xa_tc_code(xa);
185 if(!mouse_tc_check(aTHX_ tc_code, value)){
186 mouse_throw_error(MOUSE_xa_attribute(xa), value,
187 "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
188 mcall0(MOUSE_xa_attribute(xa), mouse_name),
189 mcall1s(tc, "get_message", value));
195 #define PUSH_VALUE(value, flags) STMT_START { \
196 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
197 mouse_push_values(aTHX_ value, (flags)); \
201 XPUSHs(value ? value : &PL_sv_undef); \
206 /* pushes return values, does auto-deref if needed */
208 mouse_push_values(pTHX_ SV* const value, U16 const flags){
211 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
213 if(!(value && SvOK(value))){
217 if(flags & MOUSEf_TC_IS_ARRAYREF){
218 AV* const av = (AV*)SvRV(value);
222 if(SvTYPE(av) != SVt_PVAV){
223 croak("Mouse-panic: Not an ARRAY reference");
226 len = av_len(av) + 1;
228 for(i = 0; i < len; i++){
229 SV** const svp = av_fetch(av, i, FALSE);
230 PUSHs(svp ? *svp : &PL_sv_undef);
233 else if(flags & MOUSEf_TC_IS_HASHREF){
234 HV* const hv = (HV*)SvRV(value);
237 if(SvTYPE(hv) != SVt_PVHV){
238 croak("Mouse-panic: Not a HASH reference");
242 while((he = hv_iternext(hv))){
244 PUSHs(hv_iterkeysv(he));
245 PUSHs(hv_iterval(hv, he));
253 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
254 U16 const flags = MOUSE_mg_flags(mg);
255 SV* const slot = MOUSE_mg_slot(mg);
258 value = mouse_instance_get_slot(aTHX_ self, slot);
261 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
262 AV* const xa = MOUSE_mg_xa(mg);
263 SV* const attr = MOUSE_xa_attribute(xa);
265 /* get default value by $attr->default or $attr->builder */
266 if(flags & MOUSEf_ATTR_HAS_DEFAULT){
267 value = mcall0s(attr, "default");
269 if(SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVCV){
270 value = mcall0(self, value);
273 else if(flags & MOUSEf_ATTR_HAS_BUILDER){
274 SV* const builder = mcall0s(attr, "builder");
275 value = mcall0(self, builder);
279 value = sv_newmortal();
282 /* apply coerce and type constraint */
283 if(flags & MOUSEf_ATTR_HAS_TC){
284 value = mouse_apply_type_constraint(aTHX_ xa, value, flags);
287 /* store value to slot */
288 value = mouse_instance_set_slot(aTHX_ self, slot, value);
291 PUSH_VALUE(value, flags);
295 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
296 U16 const flags = MOUSE_mg_flags(mg);
297 SV* const slot = MOUSE_mg_slot(mg);
299 if(flags & MOUSEf_ATTR_HAS_TC){
300 value = mouse_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
303 mouse_instance_set_slot(aTHX_ self, slot, value);
305 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
306 mouse_instance_weaken_slot(aTHX_ self, slot);
309 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
310 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
319 call_sv(trigger, G_VOID | G_DISCARD);
320 /* need not SPAGAIN */
323 PUSH_VALUE(value, flags);
326 XS(mouse_xs_accessor)
330 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
332 SP -= items; /* PPCODE */
335 if(items == 1){ /* reader */
336 mouse_attr_get(aTHX_ self, mg);
338 else if (items == 2){ /* writer */
339 mouse_attr_set(aTHX_ self, mg, ST(1));
342 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
343 "Expected exactly one or two argument for an accessor");
352 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
355 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
356 "Cannot assign a value to a read-only accessor");
359 SP -= items; /* PPCODE */
362 mouse_attr_get(aTHX_ self, mg);
369 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
372 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
373 "Too few arguments for a write-only accessor");
376 SP -= items; /* PPCODE */
379 mouse_attr_set(aTHX_ self, mg, ST(1));
382 /* simple accessors */
386 mouse_accessor_get_mg(pTHX_ CV* const xsub){
387 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
392 mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl){
393 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
394 SV* const slot = newSVpvn_share(key, keylen, 0U);
398 /* anonymous xsubs need sv_2mortal */
399 sv_2mortal((SV*)xsub);
402 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, NULL, 0);
403 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
406 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
408 CvXSUBANY(xsub).any_ptr = (void*)mg;
413 XS(mouse_xs_simple_reader)
417 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
421 croak("Expected exactly one argument for a reader for '%"SVf"'", slot);
424 value = mouse_instance_get_slot(aTHX_ self, slot);
425 ST(0) = value ? value : &PL_sv_undef;
430 XS(mouse_xs_simple_writer)
434 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
437 croak("Expected exactly two argument for a writer for '%"SVf"'", slot);
440 ST(0) = mouse_instance_set_slot(aTHX_ self, slot, ST(1));
444 XS(mouse_xs_simple_clearer)
448 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
452 croak("Expected exactly one argument for a clearer for '%"SVf"'", slot);
455 value = mouse_instance_delete_slot(aTHX_ self, slot);
456 ST(0) = value ? value : &PL_sv_undef;
460 XS(mouse_xs_simple_predicate)
464 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
467 croak("Expected exactly one argument for a predicate for '%"SVf"'", slot);
470 ST(0) = boolSV( mouse_instance_has_slot(aTHX_ self, slot) );
474 /* simple instance slot accessor (or Mouse::Meta::Instance) */
477 mouse_instance_create(pTHX_ HV* const stash) {
479 return sv_bless( newRV_noinc((SV*)newHV()), stash );
483 mouse_instance_clone(pTHX_ SV* const instance) {
487 CHECK_INSTANCE(instance);
488 proto = newHVhv((HV*)SvRV(instance));
489 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
493 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
496 CHECK_INSTANCE(instance);
497 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
501 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
505 CHECK_INSTANCE(instance);
506 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
507 return he ? HeVAL(he) : NULL;
511 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
517 CHECK_INSTANCE(instance);
518 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
520 sv_setsv_mg(sv, value);
525 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
528 CHECK_INSTANCE(instance);
529 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
533 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
537 CHECK_INSTANCE(instance);
538 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
540 sv_rvweaken(HeVAL(he));
544 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
547 VERSIONCHECK: DISABLE
550 _generate_accessor(klass, SV* attr, metaclass)
553 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_accessor);
559 _generate_reader(klass, SV* attr, metaclass)
562 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_reader);
568 _generate_writer(klass, SV* attr, metaclass)
571 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_writer);
577 _generate_clearer(klass, SV* attr, metaclass)
580 SV* const slot = mcall0s(attr, "name");
582 const char* const pv = SvPV_const(slot, len);
583 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_clearer);
589 _generate_predicate(klass, SV* attr, metaclass)
592 SV* const slot = mcall0s(attr, "name");
594 const char* const pv = SvPV_const(slot, len);
595 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_predicate);