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