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