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