4 static MGVTBL mop_accessor_vtbl = { /* the MAGIC identity */
19 mop_accessor_get_mg(pTHX_ CV* const xsub){
20 return mop_mg_find(aTHX_ (SV*)xsub, &mop_accessor_vtbl, MOPf_DIE_ON_FAIL);
24 mop_install_accessor(pTHX_ const char* const fq_name, const char* const key, I32 const keylen, XSPROTO(accessor_impl), const mop_instance_vtbl* vtbl){
25 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
26 SV* const keysv = newSVpvn_share(key, keylen, 0U);
30 vtbl = mop_get_default_instance_vtbl(aTHX);
34 /* generated_xsub need sv_2mortal */
35 sv_2mortal((SV*)xsub);
38 mg = sv_magicext((SV*)xsub, keysv, PERL_MAGIC_ext, &mop_accessor_vtbl, (char*)vtbl, 0);
39 SvREFCNT_dec(keysv); /* sv_magicext() increases refcnt in mg_obj */
42 * although we use MAGIC for gc, we also store mg to CvXSUBANY slot for efficiency (gfx)
44 CvXSUBANY(xsub).any_ptr = (void*)mg;
51 mop_instantiate_xs_accessor(pTHX_ SV* const accessor, XSPROTO(accessor_impl), mop_instance_vtbl* const vtbl){
52 /* $key = $accessor->associated_attribute->name */
53 SV* const attr = mop_call0(aTHX_ accessor, mop_associated_attribute);
54 SV* const key = mop_call0(aTHX_ attr, mop_name);
56 const char* const kpv = SvPV_const(key, klen);
58 return mop_install_accessor(aTHX_ NULL /* anonymous */, kpv, klen, accessor_impl, vtbl);
62 mop_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
66 croak("too few arguments for %s", GvNAME(CvGV(cv)));
69 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
70 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
74 if(!(SvROK(self) && SvOBJECT(SvRV(self)))){
75 croak("cant call %s as a class method", GvNAME(CvGV(cv)));
80 XS(mop_xs_simple_accessor)
83 dMOP_METHOD_COMMON; /* self, mg */
86 if(items == 1){ /* reader */
87 value = MOP_mg_get_slot(mg, self);
89 else if (items == 2){ /* writer */
90 value = MOP_mg_set_slot(mg, self, ST(1));
93 croak("expected exactly one or two argument");
96 ST(0) = value ? value : &PL_sv_undef;
101 XS(mop_xs_simple_reader)
104 dMOP_METHOD_COMMON; /* self, mg */
108 croak("expected exactly one argument");
111 value = MOP_mg_get_slot(mg, self);
112 ST(0) = value ? value : &PL_sv_undef;
116 XS(mop_xs_simple_writer)
119 dMOP_METHOD_COMMON; /* self, mg */
122 croak("expected exactly two argument");
125 ST(0) = MOP_mg_set_slot(mg, self, ST(1));
129 XS(mop_xs_simple_clearer)
132 dMOP_METHOD_COMMON; /* self, mg */
136 croak("expected exactly one argument");
139 value = MOP_mg_delete_slot(mg, self);
140 ST(0) = value ? value : &PL_sv_undef;
145 XS(mop_xs_simple_predicate)
148 dMOP_METHOD_COMMON; /* self, mg */
151 croak("expected exactly one argument");
154 ST(0) = boolSV( MOP_mg_has_slot(mg, self) );
159 XS(mop_xs_simple_predicate_for_metaclass)
162 dMOP_METHOD_COMMON; /* self, mg */
166 croak("expected exactly one argument");
169 value = MOP_mg_get_slot(mg, self);
170 ST(0) = boolSV( value && SvOK(value ));
174 MODULE = Class::MOP::Method::Accessor PACKAGE = Class::MOP::Method::Accessor
179 INSTALL_SIMPLE_READER_WITH_KEY(Method::Accessor, associated_attribute, attribute);
180 INSTALL_SIMPLE_READER(Method::Accessor, accessor_type);
184 _generate_accessor_method_xs(SV* self, void* instance_vtbl)
186 RETVAL = mop_instantiate_xs_accessor(aTHX_ self, mop_xs_simple_accessor, instance_vtbl);
191 _generate_reader_method_xs(SV* self, void* instance_vtbl)
193 RETVAL = mop_instantiate_xs_accessor(aTHX_ self, mop_xs_simple_reader, instance_vtbl);
198 _generate_writer_method_xs(SV* self, void* instance_vtbl)
200 RETVAL = mop_instantiate_xs_accessor(aTHX_ self, mop_xs_simple_writer, instance_vtbl);
205 _generate_predicate_method_xs(SV* self, void* instance_vtbl)
207 RETVAL = mop_instantiate_xs_accessor(aTHX_ self, mop_xs_simple_predicate, instance_vtbl);
212 _generate_clearer_method_xs(SV* self, void* instance_vtbl)
214 RETVAL = mop_instantiate_xs_accessor(aTHX_ self, mop_xs_simple_clearer, instance_vtbl);