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) {
56 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
59 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
60 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
68 mouse_instantiate_xs_accessor(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
69 SV* const slot = mcall0(attr, mouse_name);
70 AV* const xa = newAV();
77 xsub = newXS(NULL, accessor_impl, __FILE__);
78 sv_2mortal((SV*)xsub);
80 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
83 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
85 CvXSUBANY(xsub).any_ptr = (void*)mg;
87 av_extend(xa, MOUSE_XA_last - 1);
89 av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
91 /* prepare attribute status */
92 /* XXX: making it lazy is a good way? */
94 if(SvTRUEx(mcall0s(attr, "has_type_constraint"))){
96 flags |= MOUSEf_ATTR_HAS_TC;
101 tc = mcall0s(attr, "type_constraint");
102 av_store(xa, MOUSE_XA_TC, newSVsv(tc));
104 if(SvTRUEx(mcall0s(attr, "should_auto_deref"))){
105 flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
106 if( SvTRUEx(mcall1s(tc, "is_a_type_of", newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
107 flags |= MOUSEf_TC_IS_ARRAYREF;
109 else if( SvTRUEx(mcall1s(tc, "is_a_type_of", newSVpvs_flags("HashRef", SVs_TEMP))) ){
110 flags |= MOUSEf_TC_IS_HASHREF;
113 mouse_throw_error(attr, tc,
114 "Can not auto de-reference the type constraint '%"SVf"'",
115 mcall0(tc, mouse_name));
119 if(SvTRUEx(mcall0s(attr, "should_coerce"))){
120 flags |= MOUSEf_ATTR_SHOULD_COERCE;
127 if(SvTRUEx(mcall0s(attr, "has_trigger"))){
128 flags |= MOUSEf_ATTR_HAS_TRIGGER;
131 if(SvTRUEx(mcall0s(attr, "is_lazy"))){
132 flags |= MOUSEf_ATTR_IS_LAZY;
134 if(SvTRUEx(mcall0s(attr, "has_builder"))){
135 flags |= MOUSEf_ATTR_HAS_BUILDER;
137 else if(SvTRUEx(mcall0s(attr, "has_default"))){
138 flags |= MOUSEf_ATTR_HAS_DEFAULT;
142 if(SvTRUEx(mcall0s(attr, "is_weak_ref"))){
143 flags |= MOUSEf_ATTR_IS_WEAK_REF;
146 if(SvTRUEx(mcall0s(attr, "is_required"))){
147 flags |= MOUSEf_ATTR_IS_REQUIRED;
150 MOUSE_mg_flags(mg) = flags;
156 mouse_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
157 SV* const tc = MOUSE_xa_tc(xa);
160 if(flags & MOUSEf_ATTR_SHOULD_COERCE){
161 value = mcall1s(tc, "coerce", value);
164 if(!SvOK(MOUSE_xa_tc_code(xa))){
165 tc_code = mcall0s(tc, "_compiled_type_constraint");
166 av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
168 if(!(SvROK(tc_code) && SvTYPE(SvRV(tc_code)) == SVt_PVCV)){
169 mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference");
173 tc_code = MOUSE_xa_tc_code(xa);
176 if(!mouse_tc_check(aTHX_ tc_code, value)){
177 mouse_throw_error(MOUSE_xa_attribute(xa), value,
178 "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
179 mcall0(MOUSE_xa_attribute(xa), mouse_name),
180 mcall1s(tc, "get_message", value));
186 #define PUSH_VALUE(value, flags) STMT_START { \
187 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
188 mouse_push_values(aTHX_ value, (flags)); \
192 XPUSHs(value ? value : &PL_sv_undef); \
197 /* pushes return values, does auto-deref if needed */
199 mouse_push_values(pTHX_ SV* const value, U16 const flags){
202 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
204 if(!(value && SvOK(value))){
208 if(flags & MOUSEf_TC_IS_ARRAYREF){
209 AV* const av = (AV*)SvRV(value);
213 if(SvTYPE(av) != SVt_PVAV){
214 croak("Mouse-panic: Not an ARRAY reference");
217 len = av_len(av) + 1;
219 for(i = 0; i < len; i++){
220 SV** const svp = av_fetch(av, i, FALSE);
221 PUSHs(svp ? *svp : &PL_sv_undef);
224 else if(flags & MOUSEf_TC_IS_HASHREF){
225 HV* const hv = (HV*)SvRV(value);
228 if(SvTYPE(hv) != SVt_PVHV){
229 croak("Mouse-panic: Not a HASH reference");
233 while((he = hv_iternext(hv))){
235 PUSHs(hv_iterkeysv(he));
236 PUSHs(hv_iterval(hv, he));
244 mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
245 U16 const flags = MOUSE_mg_flags(mg);
246 SV* const slot = MOUSE_mg_slot(mg);
249 value = get_slot(self, slot);
252 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
253 AV* const xa = MOUSE_mg_xa(mg);
254 SV* const attr = MOUSE_xa_attribute(xa);
256 /* get default value by $attr->default or $attr->builder */
257 if(flags & MOUSEf_ATTR_HAS_DEFAULT){
258 value = mcall0s(attr, "default");
260 if(SvROK(value) && SvTYPE(SvRV(value)) == SVt_PVCV){
261 value = mcall0(self, value);
264 else if(flags & MOUSEf_ATTR_HAS_BUILDER){
265 SV* const builder = mcall0s(attr, "builder");
266 value = mcall0(self, builder);
270 value = sv_newmortal();
273 /* apply coerce and type constraint */
274 if(flags & MOUSEf_ATTR_HAS_TC){
275 value = mouse_apply_type_constraint(aTHX_ xa, value, flags);
278 /* store value to slot */
279 value = set_slot(self, slot, value);
282 PUSH_VALUE(value, flags);
286 mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
287 U16 const flags = MOUSE_mg_flags(mg);
288 SV* const slot = MOUSE_mg_slot(mg);
290 if(flags & MOUSEf_ATTR_HAS_TC){
291 value = mouse_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
294 set_slot(self, slot, value);
296 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
297 weaken_slot(self, slot);
300 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
301 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
310 call_sv(trigger, G_VOID | G_DISCARD);
311 /* need not SPAGAIN */
314 PUSH_VALUE(value, flags);
317 XS(mouse_xs_accessor)
321 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
323 SP -= items; /* PPCODE */
326 if(items == 1){ /* reader */
327 mouse_attr_get(aTHX_ self, mg);
329 else if (items == 2){ /* writer */
330 mouse_attr_set(aTHX_ self, mg, ST(1));
333 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
334 "Expected exactly one or two argument for an accessor");
343 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
346 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
347 "Cannot assign a value to a read-only accessor");
350 SP -= items; /* PPCODE */
353 mouse_attr_get(aTHX_ self, mg);
360 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
363 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
364 "Too few arguments for a write-only accessor");
367 SP -= items; /* PPCODE */
370 mouse_attr_set(aTHX_ self, mg, ST(1));
373 /* simple accessors */
377 mouse_accessor_get_mg(pTHX_ CV* const xsub){
378 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
383 mouse_install_simple_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSUBADDR_t const accessor_impl){
384 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
385 SV* const slot = newSVpvn_share(key, keylen, 0U);
389 /* anonymous xsubs need sv_2mortal */
390 sv_2mortal((SV*)xsub);
393 mg = sv_magicext((SV*)xsub, slot, PERL_MAGIC_ext, &mouse_accessor_vtbl, NULL, 0);
394 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
397 * although we use MAGIC for gc, we also store mg to CvXSUBANY for efficiency (gfx)
399 CvXSUBANY(xsub).any_ptr = (void*)mg;
404 XS(mouse_xs_simple_reader)
408 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
412 croak("Expected exactly one argument for a reader for '%"SVf"'", slot);
415 value = get_slot(self, slot);
416 ST(0) = value ? value : &PL_sv_undef;
421 XS(mouse_xs_simple_writer)
425 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
428 croak("Expected exactly two argument for a writer for '%"SVf"'", slot);
431 ST(0) = set_slot(self, slot, ST(1));
435 XS(mouse_xs_simple_clearer)
439 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
443 croak("Expected exactly one argument for a clearer for '%"SVf"'", slot);
446 value = delete_slot(self, slot);
447 ST(0) = value ? value : &PL_sv_undef;
451 XS(mouse_xs_simple_predicate)
455 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
458 croak("Expected exactly one argument for a predicate for '%"SVf"'", slot);
461 ST(0) = boolSV( has_slot(self, slot) );
465 /* simple instance slot accessor (or Mouse::Meta::Instance) */
468 mouse_instance_create(pTHX_ HV* const stash) {
470 return sv_bless( newRV_noinc((SV*)newHV()), stash );
474 mouse_instance_clone(pTHX_ SV* const instance) {
478 CHECK_INSTANCE(instance);
479 proto = newHVhv((HV*)SvRV(instance));
480 return sv_bless( newRV_noinc((SV*)proto), SvSTASH(SvRV(instance)) );
484 mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
487 CHECK_INSTANCE(instance);
488 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
492 mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
496 CHECK_INSTANCE(instance);
497 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
498 return he ? HeVAL(he) : NULL;
502 mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
508 CHECK_INSTANCE(instance);
509 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
511 sv_setsv_mg(sv, value);
516 mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
519 CHECK_INSTANCE(instance);
520 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
524 mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
528 CHECK_INSTANCE(instance);
529 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
531 sv_rvweaken(HeVAL(he));
535 MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
538 VERSIONCHECK: DISABLE
541 _generate_accessor(klass, SV* attr, metaclass)
544 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_accessor);
550 _generate_reader(klass, SV* attr, metaclass)
553 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_reader);
559 _generate_writer(klass, SV* attr, metaclass)
562 RETVAL = mouse_instantiate_xs_accessor(aTHX_ attr, mouse_xs_writer);
568 _generate_clearer(klass, SV* attr, metaclass)
571 SV* const slot = mcall0s(attr, "name");
573 const char* const pv = SvPV_const(slot, len);
574 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_clearer);
580 _generate_predicate(klass, SV* attr, metaclass)
583 SV* const slot = mcall0s(attr, "name");
585 const char* const pv = SvPV_const(slot, len);
586 RETVAL = mouse_install_simple_accessor(aTHX_ NULL, pv, len, mouse_xs_simple_predicate);