4086d2b64fc70eb5ee44b98cf0f08481c94b113e
[gitmo/Mouse.git] / xs-src / MouseAttribute.xs
1 #include "mouse.h"
2
3 static MGVTBL mouse_xa_vtbl; /* identity */
4
5 static AV*
6 mouse_build_xa(pTHX_ SV* const attr) {
7     AV*    xa;
8     MAGIC* mg;
9
10     SV* slot;
11     STRLEN len;
12     const char* pv;
13     U16 flags = 0x00;
14
15     ENTER;
16     SAVETMPS;
17
18     xa = newAV();
19
20     mg = sv_magicext(SvRV(attr), (SV*)xa, PERL_MAGIC_ext, &mouse_xa_vtbl, NULL, 0);
21     SvREFCNT_dec(xa); /* refcnt++ in sv_magicext */
22
23     av_extend(xa, MOUSE_XA_last - 1);
24
25     slot = mcall0(attr, mouse_name);
26     pv = SvPV_const(slot, len);
27     av_store(xa, MOUSE_XA_SLOT, newSVpvn_share(pv, len, 0U));
28
29     av_store(xa, MOUSE_XA_ATTRIBUTE, newSVsv(attr));
30
31     av_store(xa, MOUSE_XA_INIT_ARG, newSVsv(mcall0s(attr, "init_arg")));
32
33     if(predicate_calls(attr, "has_type_constraint")){
34         SV* tc;
35         flags |= MOUSEf_ATTR_HAS_TC;
36
37         tc = mcall0s(attr, "type_constraint");
38         av_store(xa, MOUSE_XA_TC, newSVsv(tc));
39
40         if(predicate_calls(attr, "should_auto_deref")){
41             SV* const is_a_type_of = sv_2mortal(newSVpvs_share("is_a_type_of"));
42
43             flags |= MOUSEf_ATTR_SHOULD_AUTO_DEREF;
44             if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("ArrayRef", SVs_TEMP))) ){
45                 flags |= MOUSEf_TC_IS_ARRAYREF;
46             }
47             else if( sv_true(mcall1(tc, is_a_type_of, newSVpvs_flags("HashRef", SVs_TEMP))) ){
48                 flags |= MOUSEf_TC_IS_HASHREF;
49             }
50             else{
51                 mouse_throw_error(attr, tc,
52                     "Can not auto de-reference the type constraint '%"SVf"'",
53                         mcall0(tc, mouse_name));
54             }
55         }
56
57         if(predicate_calls(attr, "should_coerce") && predicate_calls(tc, "has_coercion")){
58             flags |= MOUSEf_ATTR_SHOULD_COERCE;
59         }
60
61     }
62
63     if(predicate_calls(attr, "has_trigger")){
64         flags |= MOUSEf_ATTR_HAS_TRIGGER;
65     }
66
67     if(predicate_calls(attr, "is_lazy")){
68         flags |= MOUSEf_ATTR_IS_LAZY;
69     }
70     if(predicate_calls(attr, "has_builder")){
71         flags |= MOUSEf_ATTR_HAS_BUILDER;
72     }
73     else if(predicate_calls(attr, "has_default")){
74         flags |= MOUSEf_ATTR_HAS_DEFAULT;
75     }
76
77     if(predicate_calls(attr, "is_weak_ref")){
78         flags |= MOUSEf_ATTR_IS_WEAK_REF;
79     }
80
81     if(predicate_calls(attr, "is_required")){
82         flags |= MOUSEf_ATTR_IS_REQUIRED;
83     }
84
85     av_store(xa, MOUSE_XA_FLAGS, newSVuv(flags));
86     MOUSE_mg_flags(mg) = flags;
87
88     FREETMPS;
89     LEAVE;
90
91     return xa;
92 }
93
94 AV*
95 mouse_get_xa(pTHX_ SV* const attr) {
96     AV*    xa;
97     MAGIC* mg;
98
99     if(!IsObject(attr)){
100         croak("Not a Mouse meta attribute");
101     }
102
103     mg = mouse_mg_find(aTHX_ SvRV(attr), &mouse_xa_vtbl, 0x00);
104     if(!mg){
105         xa = mouse_build_xa(aTHX_ attr);
106     }
107     else{
108         xa = (AV*)MOUSE_mg_obj(mg);
109
110         assert(xa);
111         assert(SvTYPE(xa) == SVt_PVAV);
112     }
113
114     return xa;
115 }
116
117 SV*
118 mouse_xa_apply_type_constraint(pTHX_ AV* const xa, SV* value, U16 const flags){
119     SV* const tc = MOUSE_xa_tc(xa);
120     SV* tc_code;
121
122     if(flags & MOUSEf_ATTR_SHOULD_COERCE){
123           value = mcall1(tc, mouse_coerce, value);
124     }
125
126     if(!SvOK(MOUSE_xa_tc_code(xa))){
127         tc_code = mcall0s(tc, "_compiled_type_constraint");
128         av_store(xa, MOUSE_XA_TC_CODE, newSVsv(tc_code));
129
130         if(!IsCodeRef(tc_code)){
131             mouse_throw_error(MOUSE_xa_attribute(xa), tc, "Not a CODE reference");
132         }
133     }
134     else{
135         tc_code = MOUSE_xa_tc_code(xa);
136     }
137
138     if(!mouse_tc_check(aTHX_ tc_code, value)){
139         mouse_throw_error(MOUSE_xa_attribute(xa), value,
140             "Attribute (%"SVf") does not pass the type constraint because: %"SVf,
141                 mcall0(MOUSE_xa_attribute(xa), mouse_name),
142                 mcall1s(tc, "get_message", value));
143     }
144
145     return value;
146 }
147
148
149 SV*
150 mouse_xa_set_default(pTHX_ AV* const xa, SV* const object) {
151     U16 const flags = (U16)MOUSE_xa_flags(xa);
152     SV* value;
153
154     ENTER;
155     SAVETMPS;
156
157     /* get default value by $attr->builder or $attr->default */
158     if(flags & MOUSEf_ATTR_HAS_BUILDER){
159         SV* const builder = mcall0s(MOUSE_xa_attribute(xa), "builder");
160         value = mcall0(object, builder); /* $object->$builder() */
161     }
162     else {
163         value = mcall0s(MOUSE_xa_attribute(xa), "default");
164
165         if(IsCodeRef(value)){
166             value = mcall0(object, value);
167         }
168     }
169
170     /* apply coerce and type constraint */
171     if(flags & MOUSEf_ATTR_HAS_TC){
172         value = mouse_xa_apply_type_constraint(aTHX_ xa, value, flags);
173     }
174
175     /* store value to slot */
176     value = set_slot(object, MOUSE_xa_slot(xa), value);
177     if(flags & MOUSEf_ATTR_IS_WEAK_REF && SvROK(value)){
178         weaken_slot(object, MOUSE_xa_slot(xa));
179     }
180
181     FREETMPS;
182     LEAVE;
183
184     return value;
185 }
186
187 static void
188 mouse_check_isa_does_does(pTHX_ SV* const klass, SV* const name, SV* const isa, SV* const does){
189     STRLEN len;
190     const char* const pv = SvPV_const(isa, len); /* need strigify */
191     bool does_ok;
192     dSP;
193
194     ENTER;
195     SAVETMPS;
196
197     SAVESPTR(ERRSV);
198     ERRSV = sv_newmortal();
199
200     PUSHMARK(SP);
201     EXTEND(SP, 2);
202     mPUSHp(pv, len);
203     PUSHs(does);
204     PUTBACK;
205
206     call_method("does", G_EVAL | G_SCALAR);
207
208     SPAGAIN;
209     does_ok = sv_true(POPs);
210     PUTBACK;
211
212     FREETMPS;
213     LEAVE;
214
215     if(!does_ok){
216         mouse_throw_error(klass, NULL,
217             "Cannot have both an isa option and a does option"
218             "because '%"SVf"' does not do '%"SVf"' on attribute (%"SVf")",
219             isa, does, name
220         );
221     }
222 }
223
224 MODULE = Mouse::Meta::Attribute  PACKAGE = Mouse::Meta::Attribute
225
226 PROTOTYPES: DISABLE
227
228 BOOT:
229     /* readers */
230     INSTALL_SIMPLE_READER(Attribute, name);
231     INSTALL_SIMPLE_READER(Attribute, associated_class);
232     INSTALL_SIMPLE_READER(Attribute, accessor);
233     INSTALL_SIMPLE_READER(Attribute, reader);
234     INSTALL_SIMPLE_READER(Attribute, writer);
235     INSTALL_SIMPLE_READER(Attribute, predicate);
236     INSTALL_SIMPLE_READER(Attribute, clearer);
237     INSTALL_SIMPLE_READER(Attribute, handles);
238
239     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, _is_metadata, is);
240     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_required, required);
241     INSTALL_SIMPLE_READER(Attribute, default);
242     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_lazy, lazy);
243     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_lazy_build, lazy_build);
244     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, is_weak_ref, weak_ref);
245     INSTALL_SIMPLE_READER(Attribute, init_arg);
246     INSTALL_SIMPLE_READER(Attribute, type_constraint);
247     INSTALL_SIMPLE_READER(Attribute, trigger);
248     INSTALL_SIMPLE_READER(Attribute, builder);
249     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_auto_deref, auto_deref);
250     INSTALL_SIMPLE_READER_WITH_KEY(Attribute, should_coerce, coerce);
251     INSTALL_SIMPLE_READER(Attribute, documentation);
252     INSTALL_SIMPLE_READER(Attribute, insertion_order);
253
254     /* predicates */
255     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_accessor, accessor);
256     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_reader, reader);
257     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_writer, writer);
258     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_predicate, predicate);
259     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_clearer, clearer);
260     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_handles, handles);
261
262     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_default, default);
263     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_type_constraint, type_constraint);
264     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_trigger, trigger);
265     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_builder, builder);
266     INSTALL_SIMPLE_PREDICATE_WITH_KEY(Attribute, has_documentation, documentation);
267
268     INSTALL_CLASS_HOLDER(Attribute, accessor_metaclass, "Mouse::Meta::Method::Accessor::XS");
269
270 void
271 _process_options(SV* klass, SV* name, HV* args)
272 CODE:
273 {
274     SV** svp;
275     SV* tc = NULL;
276
277     /* 'required' requires eigher 'init_arg', 'builder', or 'default' */
278     bool can_be_required = FALSE;
279     bool has_default     = FALSE;
280     bool has_builder     = FALSE;
281
282     /* taken from Class::MOP::Attribute::new */
283
284     must_defined(name, "an attribute name");
285
286     svp = hv_fetchs(args, "init_arg", FALSE);
287     if(!svp){
288         (void)hv_stores(args, "init_arg", newSVsv(name));
289         can_be_required = TRUE;
290     }
291     else{
292         can_be_required = SvOK(*svp) ? TRUE : FALSE;
293     }
294
295     svp = hv_fetchs(args, "builder", FALSE);
296     if(svp){
297         if(!SvOK(*svp)){
298             mouse_throw_error(klass, *svp,
299                 "builder must be a defined scalar value which is a method name");
300         }
301         can_be_required = TRUE;
302         has_builder     = TRUE;
303     }
304     else if((svp = hv_fetchs(args, "default", FALSE))){
305         if(SvROK(*svp) && SvTYPE(SvRV(*svp)) != SVt_PVCV) {
306             mouse_throw_error(klass, *svp,
307                "References are not allowed as default values, you must "
308                 "wrap the default of '%"SVf"' in a CODE reference "
309                 "(ex: sub { [] } and not [])", name);
310         }
311         can_be_required = TRUE;
312         has_default     = TRUE;
313     }
314
315     svp = hv_fetchs(args, "required", FALSE);
316     if( (svp && sv_true(*svp)) && !can_be_required){
317         mouse_throw_error(klass, NULL,
318             "You cannot have a required attribute (%"SVf") "
319             "without a default, builder, or an init_arg", name);
320     }
321
322     /* taken from Mouse::Meta::Attribute->new and ->_process_args */
323
324     svp = hv_fetchs(args, "is", FALSE);
325     if(svp){
326         const char* const is = SvOK(*svp) ? SvPV_nolen_const(*svp) : "undef";
327         if(strEQ(is, "ro")){
328             svp = hv_fetchs(args, "reader", TRUE);
329             if(!sv_true(*svp)){
330                 sv_setsv(*svp, name);
331             }
332         }
333         else if(strEQ(is, "rw")){
334             if(hv_fetchs(args, "writer", FALSE)){
335                 svp = hv_fetchs(args, "reader", TRUE);
336             }
337             else{
338                 svp = hv_fetchs(args, "accessor", TRUE);
339             }
340             sv_setsv(*svp, name);
341         }
342         else if(strEQ(is, "bare")){
343             /* do nothing, but might complain later about missing methods */
344         }
345         else{
346             mouse_throw_error(klass, NULL,
347                 "I do not understand this option (is => %s) on attribute (%"SVf")",
348                 is, name);
349         }
350     }
351
352     svp = hv_fetchs(args, "isa", FALSE);
353     if(svp){
354         SPAGAIN;
355         PUSHMARK(SP);
356         XPUSHs(*svp);
357         PUTBACK;
358
359         call_pv("Mouse::Util::TypeConstraints::find_or_create_isa_type_constraint",
360             G_SCALAR);
361         SPAGAIN;
362         tc = newSVsv(POPs);
363         PUTBACK;
364     }
365
366     if((svp = hv_fetchs(args, "does", FALSE))){
367         /* check 'isa' does 'does' */
368         if(tc){
369             mouse_check_isa_does_does(aTHX_ klass, name, tc, *svp);
370             /* nothing to do */
371         }
372         else{
373             SPAGAIN;
374             PUSHMARK(SP);
375             XPUSHs(*svp);
376             PUTBACK;
377
378             call_pv("Mouse::Util::TypeConstraints::find_or_create_does_type_constraint",
379                 G_SCALAR);
380             SPAGAIN;
381             tc = newSVsv(POPs);
382             PUTBACK;
383         }
384     }
385     if(tc){
386         (void)hv_stores(args, "type_constraint", tc);
387     }
388
389     svp = hv_fetchs(args, "coerce", FALSE);
390     if(svp){
391         if(!tc){
392             mouse_throw_error(klass, NULL,
393                 "You cannot have coercion without specifying a type constraint "
394                 "on attribute (%"SVf")", name);
395         }
396         svp = hv_fetchs(args, "weak_ref", FALSE);
397         if(svp && sv_true(*svp)){
398             mouse_throw_error(klass, NULL,
399                 "You cannot have a weak reference to a coerced value on "
400                 "attribute (%"SVf")", name);
401         }
402     }
403
404     svp = hv_fetchs(args, "lazy_build", FALSE);
405     if(svp){
406         SV* clearer;
407         SV* predicate;
408         if(has_default){
409             mouse_throw_error(klass, NULL,
410                 "You can not use lazy_build and default for the same "
411                 "attribute (%"SVf")", name);
412         }
413
414         svp = hv_fetchs(args, "lazy", TRUE);
415         sv_setiv(*svp, TRUE);
416
417         svp = hv_fetchs(args, "builder", TRUE);
418         if(!sv_true(*svp)){
419             sv_setpvf(*svp, "_build_%"SVf, name);
420         }
421         has_builder = TRUE;
422
423         clearer   = *hv_fetchs(args, "clearer",   TRUE);
424         predicate = *hv_fetchs(args, "predicate", TRUE);
425
426         if(SvPV_nolen_const(name)[0] == '_'){
427             if(!sv_true(clearer)){
428                 sv_setpvf(clearer, "_clear%"SVf, name);
429             }
430             if(!sv_true(predicate)){
431                 sv_setpvf(predicate, "_has%"SVf, name);
432             }
433         }
434         else{
435             if(!sv_true(clearer)){
436                 sv_setpvf(clearer, "clear_%"SVf, name);
437             }
438             if(!sv_true(predicate)){
439                 sv_setpvf(predicate, "has_%"SVf, name);
440             }
441         }
442     }
443
444     svp = hv_fetchs(args, "auto_deref", FALSE);
445     if(svp && sv_true(*svp)){
446         SV* const meth = sv_2mortal(newSVpvs_share("is_a_type_of"));
447         if(!tc){
448             mouse_throw_error(klass, NULL,
449                 "You cannot auto-dereference without specifying a type "
450                 "constraint on attribute (%"SVf")", name);
451         }
452
453         if(!(sv_true(mcall1(tc, meth, newSVpvs_flags("ArrayRef", SVs_TEMP)))
454             || sv_true(mcall1(tc, meth, newSVpvs_flags("HashRef", SVs_TEMP))) )){
455             mouse_throw_error(klass, NULL,
456                 "You cannot auto-dereference anything other than a ArrayRef "
457                 "or HashRef on attribute (%"SVf")", name);
458         }
459     }
460
461     svp = hv_fetchs(args, "trigger", FALSE);
462     if(svp){
463         if(!IsCodeRef(*svp)){
464             mouse_throw_error(klass, NULL,
465                 "Trigger must be a CODE ref on attribute (%"SVf")",
466                 name);
467         }
468     }
469
470
471     svp = hv_fetchs(args, "lazy", FALSE);
472     if(svp && sv_true(*svp)){
473         if(!(has_default || has_builder)){
474             mouse_throw_error(klass, NULL,
475                 "You cannot have lazy attribute (%"SVf") without specifying "
476                 "a default value for it", name);
477         }
478     }
479 }