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