Tidy
[gitmo/Mouse.git] / xs-src / MouseAccessor.xs
CommitLineData
646c0371 1#include "mouse.h"
2
73d8d597 3#define CHECK_INSTANCE(instance) STMT_START{ \
fcdc12ac 4 assert(instance); \
73d8d597 5 if(!(SvROK(instance) && SvTYPE(SvRV(instance)) == SVt_PVHV)){ \
ef53a04b 6 croak("Invalid object instance: '%"SVf"'", instance); \
73d8d597 7 } \
646c0371 8 } STMT_END
9
646c0371 10
11#define MOUSE_mg_attribute(mg) MOUSE_xa_attribute(MOUSE_mg_xa(mg))
12
646c0371 13static MGVTBL mouse_accessor_vtbl; /* MAGIC identity */
14
cc7cd81f 15#define dMOUSE_self SV* const self = mouse_accessor_get_self(aTHX_ ax, items, cv)
646c0371 16
fc650569 17STATIC_INLINE SV*
646c0371 18mouse_accessor_get_self(pTHX_ I32 const ax, I32 const items, CV* const cv) {
646c0371 19 if(items < 1){
20 croak("Too few arguments for %s", GvNAME(CvGV(cv)));
21 }
22
23 /* NOTE: If self has GETMAGIC, $self->accessor will invoke GETMAGIC
24 * before calling methods, so SvGETMAGIC(self) is not necessarily needed here.
25 */
26
76770976 27 return ST(0);
646c0371 28}
29
30
31CV*
a17f6313 32mouse_accessor_generate(pTHX_ SV* const attr, XSUBADDR_t const accessor_impl){
0aad0266 33 AV* const xa = mouse_get_xa(aTHX_ attr);
646c0371 34 CV* xsub;
35 MAGIC* mg;
646c0371 36
37 xsub = newXS(NULL, accessor_impl, __FILE__);
38 sv_2mortal((SV*)xsub);
39
8d33c73a 40 mg = sv_magicext((SV*)xsub, MOUSE_xa_slot(xa),
41 PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)xa, HEf_SVKEY);
0aad0266 42
4e7e3250 43 MOUSE_mg_flags(mg) = (U16)MOUSE_xa_flags(xa);
646c0371 44
45 /* NOTE:
8d33c73a 46 * although we use MAGIC for gc, we also store mg to
47 * CvXSUBANY for efficiency (gfx)
646c0371 48 */
49 CvXSUBANY(xsub).any_ptr = (void*)mg;
50
646c0371 51 return xsub;
52}
53
646c0371 54
208ffaeb 55#define PUSH_VALUE(value, flags) STMT_START { \
56 if((flags) & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY){ \
57 mouse_push_values(aTHX_ value, (flags)); \
58 } \
59 else{ \
60 dSP; \
61 XPUSHs(value ? value : &PL_sv_undef); \
62 PUTBACK; \
63 } \
64 } STMT_END \
646c0371 65
66/* pushes return values, does auto-deref if needed */
67static void
68mouse_push_values(pTHX_ SV* const value, U16 const flags){
69 dSP;
70
208ffaeb 71 assert( flags & MOUSEf_ATTR_SHOULD_AUTO_DEREF && GIMME_V == G_ARRAY );
646c0371 72
208ffaeb 73 if(!(value && SvOK(value))){
74 return;
75 }
646c0371 76
208ffaeb 77 if(flags & MOUSEf_TC_IS_ARRAYREF){
80aa5731 78 AV* av;
208ffaeb 79 I32 len;
80 I32 i;
646c0371 81
80aa5731 82 if(!IsArrayRef(value)){
208ffaeb 83 croak("Mouse-panic: Not an ARRAY reference");
646c0371 84 }
646c0371 85
80aa5731 86 av = (AV*)SvRV(value);
208ffaeb 87 len = av_len(av) + 1;
88 EXTEND(SP, len);
89 for(i = 0; i < len; i++){
90 SV** const svp = av_fetch(av, i, FALSE);
91 PUSHs(svp ? *svp : &PL_sv_undef);
646c0371 92 }
93 }
caf77af6 94 else{
80aa5731 95 HV* hv;
208ffaeb 96 HE* he;
97
caf77af6 98 assert(flags & MOUSEf_TC_IS_HASHREF);
99
80aa5731 100 if(!IsHashRef(value)){
208ffaeb 101 croak("Mouse-panic: Not a HASH reference");
102 }
103
80aa5731 104 hv = (HV*)SvRV(value);
208ffaeb 105 hv_iterinit(hv);
106 while((he = hv_iternext(hv))){
107 EXTEND(SP, 2);
108 PUSHs(hv_iterkeysv(he));
109 PUSHs(hv_iterval(hv, he));
110 }
646c0371 111 }
112
113 PUTBACK;
114}
115
116static void
117mouse_attr_get(pTHX_ SV* const self, MAGIC* const mg){
118 U16 const flags = MOUSE_mg_flags(mg);
646c0371 119 SV* value;
120
4e7e3250 121 value = get_slot(self, MOUSE_mg_slot(mg));
646c0371 122
123 /* check_lazy */
124 if( !value && flags & MOUSEf_ATTR_IS_LAZY ){
4e7e3250 125 value = mouse_xa_set_default(aTHX_ MOUSE_mg_xa(mg), self);
646c0371 126 }
127
208ffaeb 128 PUSH_VALUE(value, flags);
646c0371 129}
130
131static void
132mouse_attr_set(pTHX_ SV* const self, MAGIC* const mg, SV* value){
133 U16 const flags = MOUSE_mg_flags(mg);
134 SV* const slot = MOUSE_mg_slot(mg);
135
136 if(flags & MOUSEf_ATTR_HAS_TC){
4e7e3250 137 value = mouse_xa_apply_type_constraint(aTHX_ MOUSE_mg_xa(mg), value, flags);
646c0371 138 }
139
ca8e67d6 140 value = set_slot(self, slot, value);
646c0371 141
142 if(flags & MOUSEf_ATTR_IS_WEAK_REF){
6fe2272b 143 weaken_slot(self, slot);
646c0371 144 }
145
146 if(flags & MOUSEf_ATTR_HAS_TRIGGER){
147 SV* const trigger = mcall0s(MOUSE_mg_attribute(mg), "trigger");
148 dSP;
149
8ab2c6ab 150 /* NOTE: triggers can remove value, so
151 value must be copied here,
152 revealed by Net::Google::DataAPI (DANJOU).
153 */
154 value = sv_mortalcopy(value);
155
646c0371 156 PUSHMARK(SP);
157 EXTEND(SP, 2);
158 PUSHs(self);
159 PUSHs(value);
160
161 PUTBACK;
b3cd4c14 162 call_sv_safe(trigger, G_VOID | G_DISCARD);
646c0371 163 /* need not SPAGAIN */
8ab2c6ab 164
165 assert(SvTYPE(value) != SVTYPEMASK);
646c0371 166 }
167
208ffaeb 168 PUSH_VALUE(value, flags);
646c0371 169}
170
30e11004 171XS(XS_Mouse_accessor)
646c0371 172{
173 dVAR; dXSARGS;
174 dMOUSE_self;
175 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
176
177 SP -= items; /* PPCODE */
178 PUTBACK;
179
180 if(items == 1){ /* reader */
181 mouse_attr_get(aTHX_ self, mg);
182 }
183 else if (items == 2){ /* writer */
184 mouse_attr_set(aTHX_ self, mg, ST(1));
185 }
186 else{
187 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
fad99075 188 "Expected exactly one or two argument for an accessor of %"SVf,
189 MOUSE_mg_slot(mg));
646c0371 190 }
191}
192
193
30e11004 194XS(XS_Mouse_reader)
646c0371 195{
196 dVAR; dXSARGS;
197 dMOUSE_self;
198 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
199
200 if (items != 1) {
201 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
fad99075 202 "Cannot assign a value to a read-only accessor of %"SVf,
203 MOUSE_mg_slot(mg));
646c0371 204 }
205
206 SP -= items; /* PPCODE */
207 PUTBACK;
208
209 mouse_attr_get(aTHX_ self, mg);
210}
211
30e11004 212XS(XS_Mouse_writer)
646c0371 213{
214 dVAR; dXSARGS;
215 dMOUSE_self;
216 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
217
218 if (items != 2) {
219 mouse_throw_error(MOUSE_mg_attribute(mg), NULL,
fad99075 220 "Too few arguments for a write-only accessor of %"SVf,
221 MOUSE_mg_slot(mg));
646c0371 222 }
223
224 SP -= items; /* PPCODE */
225 PUTBACK;
226
227 mouse_attr_set(aTHX_ self, mg, ST(1));
228}
229
230/* simple accessors */
231
232/*
233static MAGIC*
234mouse_accessor_get_mg(pTHX_ CV* const xsub){
235 return moose_mg_find(aTHX_ (SV*)xsub, &mouse_simple_accessor_vtbl, MOOSEf_DIE_ON_FAIL);
236}
237*/
238
239CV*
fad99075 240mouse_simple_accessor_generate(pTHX_
241 const char* const fq_name, const char* const key, I32 const keylen,
242 XSUBADDR_t const accessor_impl, void* const dptr, I32 const dlen) {
646c0371 243 CV* const xsub = newXS((char*)fq_name, accessor_impl, __FILE__);
244 SV* const slot = newSVpvn_share(key, keylen, 0U);
245 MAGIC* mg;
246
247 if(!fq_name){
8d33c73a 248 /* anonymous xsubs need sv_2mortal() */
646c0371 249 sv_2mortal((SV*)xsub);
250 }
251
8d33c73a 252 mg = sv_magicext((SV*)xsub, slot,
253 PERL_MAGIC_ext, &mouse_accessor_vtbl, (char*)dptr, dlen);
254
646c0371 255 SvREFCNT_dec(slot); /* sv_magicext() increases refcnt in mg_obj */
e058b279 256 if(dlen == HEf_SVKEY){
257 SvREFCNT_dec(dptr);
258 }
646c0371 259
260 /* NOTE:
8d33c73a 261 * although we use MAGIC for gc, we also store mg to CvXSUBANY
262 * for efficiency (gfx)
646c0371 263 */
264 CvXSUBANY(xsub).any_ptr = (void*)mg;
265
266 return xsub;
267}
268
30e11004 269XS(XS_Mouse_simple_reader)
646c0371 270{
271 dVAR; dXSARGS;
272 dMOUSE_self;
e058b279 273 MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
646c0371 274 SV* value;
275
276 if (items != 1) {
fad99075 277 croak("Expected exactly one argument for a reader of %"SVf,
278 MOUSE_mg_slot(mg));
646c0371 279 }
280
e058b279 281 value = get_slot(self, MOUSE_mg_slot(mg));
2468f1d7 282 if(!value) {
283 if(MOUSE_mg_ptr(mg)){
284 /* the default value must be a SV */
285 assert(MOUSE_mg_len(mg) == HEf_SVKEY);
286 value = (SV*)MOUSE_mg_ptr(mg);
287 }
288 else{
289 value = &PL_sv_undef;
290 }
291 }
292
293 ST(0) = value;
646c0371 294 XSRETURN(1);
295}
296
297
30e11004 298XS(XS_Mouse_simple_writer)
646c0371 299{
300 dVAR; dXSARGS;
301 dMOUSE_self;
302 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
303
304 if (items != 2) {
fad99075 305 croak("Expected exactly two argument for a writer of %"SVf,
306 slot);
646c0371 307 }
308
6fe2272b 309 ST(0) = set_slot(self, slot, ST(1));
646c0371 310 XSRETURN(1);
311}
312
30e11004 313XS(XS_Mouse_simple_clearer)
646c0371 314{
315 dVAR; dXSARGS;
316 dMOUSE_self;
317 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
318 SV* value;
319
320 if (items != 1) {
fad99075 321 croak("Expected exactly one argument for a clearer of %"SVf,
322 slot);
646c0371 323 }
324
6fe2272b 325 value = delete_slot(self, slot);
646c0371 326 ST(0) = value ? value : &PL_sv_undef;
327 XSRETURN(1);
328}
329
30e11004 330XS(XS_Mouse_simple_predicate)
646c0371 331{
332 dVAR; dXSARGS;
333 dMOUSE_self;
334 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
335
336 if (items != 1) {
fad99075 337 croak("Expected exactly one argument for a predicate of %"SVf, slot);
646c0371 338 }
339
6fe2272b 340 ST(0) = boolSV( has_slot(self, slot) );
646c0371 341 XSRETURN(1);
342}
343
fad99075 344/* Class::Data::Inheritable-like class accessor */
345XS(XS_Mouse_inheritable_class_accessor) {
346 dVAR; dXSARGS;
347 dMOUSE_self;
348 SV* const slot = MOUSE_mg_slot((MAGIC*)XSANY.any_ptr);
349 SV* value;
fad99075 350 HV* stash;
351
352 if(items == 1){ /* reader */
353 value = NULL;
354 }
355 else if (items == 2){ /* writer */
356 value = ST(1);
357 }
358 else{
359 croak("Expected exactly one or two argument for a class data accessor"
360 "of %"SVf, slot);
361 value = NULL; /* -Wuninitialized */
362 }
363
05e658dc 364 stash = mouse_get_namespace(aTHX_ self);
fad99075 365
366 if(!value) { /* reader */
367 value = get_slot(self, slot);
368 if(!value) {
369 AV* const isa = mro_get_linear_isa(stash);
370 I32 const len = av_len(isa) + 1;
371 I32 i;
372 for(i = 1; i < len; i++) {
373 SV* const klass = MOUSE_av_at(isa, i);
374 SV* const meta = get_metaclass(klass);
375 if(!SvOK(meta)){
376 continue; /* skip non-Mouse classes */
377 }
378 value = get_slot(meta, slot);
379 if(value) {
380 break;
381 }
382 }
383 if(!value) {
384 value = &PL_sv_undef;
385 }
386 }
387 }
388 else { /* writer */
389 set_slot(self, slot, value);
390 mro_method_changed_in(stash);
391 }
392
393 ST(0) = value;
394 XSRETURN(1);
395}
396
76770976 397/* simple instance slot accessor (or Mouse::Meta::Instance) */
646c0371 398
399SV*
400mouse_instance_create(pTHX_ HV* const stash) {
fcdc12ac 401 SV* instance;
646c0371 402 assert(stash);
aa2d2e2c 403 assert(SvTYPE(stash) == SVt_PVHV);
fcdc12ac 404 instance = sv_bless( newRV_noinc((SV*)newHV()), stash );
405 return sv_2mortal(instance);
646c0371 406}
407
408SV*
409mouse_instance_clone(pTHX_ SV* const instance) {
fcdc12ac 410 SV* proto;
646c0371 411 CHECK_INSTANCE(instance);
fcdc12ac 412 assert(SvOBJECT(SvRV(instance)));
413
414 proto = newRV_noinc((SV*)newHVhv((HV*)SvRV(instance)));
415 sv_bless(proto, SvSTASH(SvRV(instance)));
416 return sv_2mortal(proto);
646c0371 417}
418
419bool
420mouse_instance_has_slot(pTHX_ SV* const instance, SV* const slot) {
646c0371 421 assert(slot);
422 CHECK_INSTANCE(instance);
423 return hv_exists_ent((HV*)SvRV(instance), slot, 0U);
424}
425
426SV*
427mouse_instance_get_slot(pTHX_ SV* const instance, SV* const slot) {
428 HE* he;
646c0371 429 assert(slot);
430 CHECK_INSTANCE(instance);
431 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
432 return he ? HeVAL(he) : NULL;
433}
434
435SV*
436mouse_instance_set_slot(pTHX_ SV* const instance, SV* const slot, SV* const value) {
437 HE* he;
438 SV* sv;
646c0371 439 assert(slot);
440 assert(value);
441 CHECK_INSTANCE(instance);
442 he = hv_fetch_ent((HV*)SvRV(instance), slot, TRUE, 0U);
443 sv = HeVAL(he);
957474ad 444 sv_setsv(sv, value);
445 SvSETMAGIC(sv);
646c0371 446 return sv;
447}
448
449SV*
450mouse_instance_delete_slot(pTHX_ SV* const instance, SV* const slot) {
451 assert(instance);
452 assert(slot);
453 CHECK_INSTANCE(instance);
454 return hv_delete_ent((HV*)SvRV(instance), slot, 0, 0U);
455}
456
457void
458mouse_instance_weaken_slot(pTHX_ SV* const instance, SV* const slot) {
459 HE* he;
646c0371 460 assert(slot);
461 CHECK_INSTANCE(instance);
462 he = hv_fetch_ent((HV*)SvRV(instance), slot, FALSE, 0U);
463 if(he){
464 sv_rvweaken(HeVAL(he));
465 }
466}
346a3ab8 467
646c0371 468MODULE = Mouse::Meta::Method::Accessor::XS PACKAGE = Mouse::Meta::Method::Accessor::XS
469
470PROTOTYPES: DISABLE
471VERSIONCHECK: DISABLE
472
473CV*
474_generate_accessor(klass, SV* attr, metaclass)
475CODE:
476{
a17f6313 477 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_accessor);
646c0371 478}
479OUTPUT:
480 RETVAL
481
482CV*
483_generate_reader(klass, SV* attr, metaclass)
484CODE:
485{
a17f6313 486 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_reader);
646c0371 487}
488OUTPUT:
489 RETVAL
490
491CV*
492_generate_writer(klass, SV* attr, metaclass)
493CODE:
494{
a17f6313 495 RETVAL = mouse_accessor_generate(aTHX_ attr, XS_Mouse_writer);
646c0371 496}
497OUTPUT:
498 RETVAL
499
500CV*
501_generate_clearer(klass, SV* attr, metaclass)
502CODE:
503{
06afbf31 504 SV* const slot = mcall0(attr, mouse_name);
646c0371 505 STRLEN len;
506 const char* const pv = SvPV_const(slot, len);
a17f6313 507 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_clearer, NULL, 0);
646c0371 508}
509OUTPUT:
510 RETVAL
511
512CV*
513_generate_predicate(klass, SV* attr, metaclass)
514CODE:
515{
06afbf31 516 SV* const slot = mcall0(attr, mouse_name);
646c0371 517 STRLEN len;
518 const char* const pv = SvPV_const(slot, len);
a17f6313 519 RETVAL = mouse_simple_accessor_generate(aTHX_ NULL, pv, len, XS_Mouse_simple_predicate, NULL, 0);
646c0371 520}
521OUTPUT:
522 RETVAL
523