Bind.xs

filter: Statementstmt 57.8% 85 / 147 Branchbran 56.1% 55 / 98 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 57.1% 140 / 245 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Data/Bind.pm ↑ Summary
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4  
5 static int
6 113 alias_mg_get(pTHX_ SV *sv, MAGIC *mg)
7 {
8 113 SV *const target = mg->mg_obj;
9 assert (target);
10  
11 113 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 5 108 113
12 5 sv_unref_flags(sv, 0);
13 }
14 113 ▶ SvOK_off(sv);
Branch: 4 branches
BranchTrueFalseTotal
SvOK_off(sv); 0 113 113
SvOK_off(sv); 0 0 0
SvOK_off(sv); 0 0 0
SvOK_off(sv); 0 113 113
15 113 ▶ if (SvTYPE(sv) >= SVt_PV && SvLEN(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(sv) >= SVt_PV && SvLEN(sv)) { 113 0 113
if (SvTYPE(sv) >= SVt_PV && SvLEN(sv)) { 3 110 113
16 3 Safefree(SvPVX(sv));
17 3 SvLEN(sv) = 0;
18 3 SvPVX(sv) = NULL;
19 }
20
21 113 SvGETMAGIC(target);
22 113 ▶ if (SvROK(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(target)) { 9 104 113
23 9 SvROK_on(sv);
24 9 SvRV(sv) = SvREFCNT_inc(SvRV(target));
25 /* Won't yet cope with getting blessing or overloading.
26 Worse still, there is no way to catch a bless or overload on the
27 target and send it back, or to ensure that the alias keeps reading
28 the class from the target, rather than having a stale local cache of
29 it. */
30 } else {
31 104 ▶ if (SvPOKp(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOKp(target)) { 67 37 104
32 67 SvPVX(sv) = SvPVX(target);
33 67 SvCUR(sv) = SvCUR(target);
34 /* SvLEN remains 0, meaning that we don't own the buffer or free
35 it. The assumption is that as we're magic our caller musn't
36 rely on our buffer existing for long.
37 Oh, and they are evil and wrong if they assume that they can
38 write to it directly, because they will come a cropper on any
39 other magic scalar. */
40 67 SvPOKp_on(sv);
41 }
42 104 ▶ if (SvIOKp(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOKp(target)) { 37 67 104
43 37 SvIVX(sv) = SvIVX(target);
44 37 SvIOKp_on(sv);
45 37 ▶ if (SvIsUV(target))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(target)) 0 37 37
46 0 SvIsUV_on(sv);
47 }
48 104 ▶ if (SvNOKp(target)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOKp(target)) { 0 104 104
49 0 SvNVX(sv) = SvNVX(target);
50 0 SvNOKp_on(sv);
51 }
52 }
53 113 return 0;
54 }
55  
56 static int
57 5 alias_mg_set(pTHX_ SV *sv, MAGIC *mg)
58 {
59 5 SV *const target = mg->mg_obj;
60 assert (target);
61  
62 5 sv_force_normal(target);
63 5 ▶ SvOK_off(target);
Branch: 4 branches
BranchTrueFalseTotal
SvOK_off(target); 0 5 5
SvOK_off(target); 0 0 0
SvOK_off(target); 0 0 0
SvOK_off(target); 0 5 5
64 5 ▶ if (SvROK(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvROK(sv)) { 0 5 5
65 0 ▶ if (SvTYPE(target) >= SVt_PV && SvLEN(target)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTYPE(target) >= SVt_PV && SvLEN(target)) { 0 0 0
if (SvTYPE(target) >= SVt_PV && SvLEN(target)) { 0 0 0
66 0 Safefree(SvPVX(target));
67 0 SvLEN(target) = 0;
68 }
69 0 SvROK_on(target);
70 0 SvRV(target) = SvREFCNT_inc(SvRV(sv));
71 } else {
72 5 ▶ if (SvPOKp(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvPOKp(sv)) { 4 1 5
73 4 ▶ SvGROW(target, SvCUR(sv) + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(target, SvCUR(sv) + 1); 4 0 4
SvGROW(target, SvCUR(sv) + 1); 0 4 4
74 4 Copy(SvPVX(sv), SvPVX(target), SvCUR(sv) + 1, char);
75 4 SvCUR(target) = SvCUR(sv);
76 4 SvPOKp_on(target);
77 }
78 5 ▶ if (SvIOKp(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIOKp(sv)) { 1 4 5
79 1 SvIVX(target) = SvIVX(sv);
80 1 SvIOKp_on(target);
81 1 ▶ if (!SvPOKp(sv) && !SvNOKp(sv)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvPOKp(sv) && !SvNOKp(sv)) { 1 0 1
if (!SvPOKp(sv) && !SvNOKp(sv)) { 1 0 1
82 /* This is really getting too fragile. 5.8.0 onwards has
83 arguably-a-bug in save_magic. This line:
84  
85 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_NOK|SVp_POK)) >> PRIVSHIFT;
86  
87 should probably read
88  
89 SvFLAGS(sv) |= (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK)) >> PRIVSHIFT;
90  
91 The upshot is that scalars passed in with just SVp_IOK
92 aren't getting acknowledged as having any defined value.
93  
94 The whole private/not flags business is already rather too
95 fragile, as 5.8.0 onwards assign different meaning to
96 private/not private for regular scalars and for magical
97 scalars, both meanings requireing paired bits. And now we're
98 trying to proxy values across from one sort (magic) to the
99 other sort (magic), which (I think) means we're trying to
100 squeeze two local bits of information on one side into one
101 at the other. */
102 1 SvIOK_on(target);
103 }
104 1 ▶ if (SvIsUV(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvIsUV(sv)) 0 1 1
105 0 SvIsUV_on(target);
106 }
107 5 ▶ if (SvNOKp(sv)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvNOKp(sv)) { 0 5 5
108 0 SvNVX(target) = SvNVX(sv);
109 0 SvNOKp_on(target);
110 }
111 }
112 5 ▶ SvSETMAGIC(target);
Branch: 1 branch
BranchTrueFalseTotal
SvSETMAGIC(target); 0 5 5
113 5 }
114  
115 static U32
116 0 alias_mg_len(pTHX_ SV *sv, MAGIC *mg)
117 {
118 0 return sv_len(mg->mg_obj);
119 }
120  
121 /* Not sure if the last few need to become conditionally compiled based on
122 perl version */
123 MGVTBL alias_vtbl = {
124 alias_mg_get, /* get */
125 alias_mg_set, /* set */
126 alias_mg_len, /* len */
127 0, /* clear */
128 0, /* free */
129 0, /* copy */
130 0 /* dup */
131 };
132  
133  
134 typedef SV *SVREF;
135  
136 void
137 65 __alias_a_to_b(SVREF a, SVREF b, int read_only) {
138 /* This bit of evil lifted straight from Perl_newSVrv */
139 65 const U32 refcnt = SvREFCNT(a);
140 65 int is_my = SvPADMY(a);
141 65 svtype type = SvTYPE(b);
142 65 SvREFCNT(a) = 0;
143 65 sv_clear(a);
144 65 SvFLAGS(a) = 0;
145 65 SvPADMY_on(a);
146 65 SvREFCNT(a) = refcnt;
147  
148 65 ▶ SvUPGRADE(a, SVt_PVMG);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(a, SVt_PVMG); 65 0 65
149 assert(SvIVX(a) == 0);
150 assert(SvNVX(a) == 0.0);
151 assert(SvPVX(a) == NULL);
152  
153 /* If a magic is assigned to $a, $a has SVt_MAGIC but no MAGIC attached.
154 This breaks the magicext below */
155 65 ▶ if ( type == SVt_PVMG && !SvMAGIC(b) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( type == SVt_PVMG && !SvMAGIC(b) ) { 2 63 65
if ( type == SVt_PVMG && !SvMAGIC(b) ) { 0 2 2
156 0 type = 0;
157 }
158  
159 /* other mg */
160 65 ▶ if ( type == SVt_PVMG && SvMAGIC(b)->mg_virtual != &alias_vtbl) {
Branch: 2 branches
BranchTrueFalseTotal
if ( type == SVt_PVMG && SvMAGIC(b)->mg_virtual != &alias_vtbl) { 2 63 65
if ( type == SVt_PVMG && SvMAGIC(b)->mg_virtual != &alias_vtbl) { 0 2 2
161 0 type = 0;
162 }
163  
164 /* if @array is bound to other thing, binding $array[1] to $x */
165 65 ▶ if ( type == SVt_PVLV && SvMAGIC(b) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ( type == SVt_PVLV && SvMAGIC(b) ) { 0 65 65
if ( type == SVt_PVLV && SvMAGIC(b) ) { 0 0 0
166 0 type = 0;
167 }
168  
169 65 ▶ if (type >= SVt_PVMG) {
Branch: 1 branch
BranchTrueFalseTotal
if (type >= SVt_PVMG) { 6 59 65
170 6 switch (type) {
171 4 case SVt_PVHV:
172 case SVt_PVAV: {
173 4 SV *tie = newRV_noinc((SV*)newHV());
174 4 ▶ HV *stash = gv_stashpv(type == SVt_PVHV ?
Branch: 1 branch
BranchTrueFalseTotal
HV *stash = gv_stashpv(type == SVt_PVHV ? 0 4 4
175 "Data::Bind::Hash" : "Data::Bind::Array",
176 TRUE);
177 4 hv_store((HV*)SvRV(tie), "real", 4, newRV_inc((SV *)b), 0);
178 4 sv_bless(tie, stash);
179 4 ▶ SvUPGRADE(a, SVt_PVAV);
Branch: 1 branch
BranchTrueFalseTotal
SvUPGRADE(a, SVt_PVAV); 4 0 4
180 4 hv_magic((HV*)a, (GV *)tie, PERL_MAGIC_tied);
181 4 break;
182 }
183 2 case SVt_PVMG: {
184 // SvMAGIC(a) = SvMAGIC(b);
185 2 sv_magicext(a, SvMAGIC(b)->mg_obj, PERL_MAGIC_ext, &alias_vtbl, 0, 0);
186 // SvREFCNT(SvMAGIC(a)->mg_obj)++;
187 2 mg_get(a);
188 2 break;
189 }
190 0 case SVt_PVLV: {
191 0 break;
192 }
193 0 default:
194 0 croak("don't know what to do yet for %d", type);
195 }
196 }
197 60 ▶ else if (type == SVt_RV && SvAMAGIC(b)) {
Branch: 4 branches
BranchTrueFalseTotal
else if (type == SVt_RV && SvAMAGIC(b)) { 28 31 59
else if (type == SVt_RV && SvAMAGIC(b)) { 4 24 28
else if (type == SVt_RV && SvAMAGIC(b)) { 3 1 4
else if (type == SVt_RV && SvAMAGIC(b)) { 1 2 3
198 1 SV *x = sv_newmortal();
199 1 sv_setsv(a, newRV_inc(SvRV(b)));
200 1 SvAMAGIC_on(a);
201 }
202 else {
203 58 sv_magicext(a, b, PERL_MAGIC_ext, &alias_vtbl, 0, 0);
204 58 mg_get(a);
205 }
206  
207 65 ▶ if (read_only || SvREADONLY(b)) {
Branch: 2 branches
BranchTrueFalseTotal
if (read_only || SvREADONLY(b)) { 20 45 65
if (read_only || SvREADONLY(b)) { 2 18 20
208 47 SvREADONLY_on(a);
209 }
210 65 }
211  
212  
213 0 OP *___bind_pad(pTHX)
214 {
215 0 dMARK; dAX; dSP; dITEMS;
216 0 int n = PL_op->op_targ;
217 int i;
218 0 ▶ for (i = 0; i < items; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < items; ++i) { 0 0 0
219 0 SAVECLEARSV(PAD_SVl(i+1));
220 // rw only for now
221 0 PAD_SVl(i+1) = SvREFCNT_inc(ST(i));
222 }
223 0 return NORMAL;
224 }
225  
226 /* format: [ [ order, mode, defaultsub ]... ] */
227  
228 0 OP *___bind_pad2(pTHX)
229 {
230 0 dSP;
231 0 AV *_defargs = GvAV(PL_defgv);
232 0 AV *av = (AV *)SvRV(cSVOPx_sv(PL_op));
233 int i;
234 0 ▶ for (i = 0; i <= av_len(av); ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i <= av_len(av); ++i) { 0 0 0
235 0 SV *current_arg = *av_fetch(_defargs, i, 0);
236 0 SV *entry = *av_fetch(av, i, 0);
237 0 IV order = SvIVX(*av_fetch((AV *)SvRV(entry), 0, 0));
238 0 SV *mode = *av_fetch((AV *)SvRV(entry), 1, 0); // XXX: should do SvOK
239 0 SV *default_sub = *av_fetch((AV *)SvRV(entry), 2, 0);
240 0 SAVECLEARSV(PAD_SVl(order));
241 /* XXX: check if order is over items, if so it means it's empty and we should apply default_sub->() */
242 0 ▶ if (SvIVX(mode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvIVX(mode)) { 0 0 0
243 0 PAD_SVl(order) = newSV(0);
244 0 ▶ SvSetSV(PAD_SVl(order), SvREFCNT_inc(current_arg));
Branch: 1 branch
BranchTrueFalseTotal
SvSetSV(PAD_SVl(order), SvREFCNT_inc(current_arg)); 0 0 0
245 }
246 else {
247 0 PAD_SVl(order) = SvREFCNT_inc(current_arg);
248 // PAD_SVl(order) = newSV(0);
249 // __alias_a_to_b(PAD_SVl(order), current_arg, 1);
250 }
251 }
252 0 RETURN;
253 }
254  
255  
256 MODULE = Data::Bind PACKAGE = Data::Bind
257  
258 void
259 OP_bind_pad(flags, n)
260 I32 flags
261 I32 n
262 SV** sparepad = NO_INIT
263 OP *o = NO_INIT
264 OP *saveop = NO_INIT
265 I32 typenum = NO_INIT
266 CODE:
267 0 sparepad = PL_curpad;
268 0 saveop = PL_op;
269 0 PL_curpad = AvARRAY(PL_comppad);
270 0 o = newOP(OP_CUSTOM, flags);
271 0 o->op_ppaddr = ___bind_pad;
272 0 o->op_targ = n;
273 0 PL_curpad = sparepad;
274 0 PL_op = saveop;
275 0 ST(0) = sv_newmortal();
276 0 sv_setiv(newSVrv(ST(0), "B::OP"), PTR2IV(o));
277  
278 void
279 OP_bind_pad2(flags, spec)
280 I32 flags
281 SV *spec
282 SV** sparepad = NO_INIT
283 OP *o = NO_INIT
284 OP *saveop = NO_INIT
285 I32 typenum = NO_INIT
286 CODE:
287 0 sparepad = PL_curpad;
288 0 saveop = PL_op;
289 0 PL_curpad = AvARRAY(PL_comppad);
290 0 o = newSVOP(OP_CONST, flags, SvREFCNT_inc(spec));
291 0 o->op_ppaddr = ___bind_pad2;
292 0 PL_curpad = sparepad;
293 0 PL_op = saveop;
294 0 ST(0) = sv_newmortal();
295 0 sv_setiv(newSVrv(ST(0), "B::OP"), PTR2IV(o));
296  
297 void
298 _forget_unlocal(IV howmany)
299 CODE:
300 {
301 int lv;
302 3 ▶ for(lv=1; lv <= howmany; ++lv) {
Branch: 1 branch
BranchTrueFalseTotal
for(lv=1; lv <= howmany; ++lv) { 2 1 3
303 2 PL_scopestack[PL_scopestack_ix - (lv + 1)] = PL_savestack_ix;
304 }
305 }
306  
307 void
308 _av_store(SV *av_ref, I32 key, SV *val)
309 CODE:
310 {
311 /* XXX many checks */
312 55 AV *av = (AV *)SvRV(av_ref);
313 /* XXX unref the old one in slot? */
314 55 av_store(av, key, SvREFCNT_inc(SvRV(val)));
315 }
316  
317 void
318 _hv_store(SV *hv_ref, const char *key, SV *val)
319 CODE:
320 {
321 /* XXX many checks */
322 27 HV *hv = (HV *)SvRV(hv_ref);
323 /* XXX unref the old one in slot? */
324 27 hv_store(hv, key, strlen(key), SvREFCNT_inc((SvRV(val))), 0);
325 }
326  
327 void
328 _alias_a_to_b(SVREF a, SVREF b, int read_only)
329 CODE:
330 {
331 65 __alias_a_to_b(a, b, read_only);
332 }
« blib/lib/Data/Bind.pm ↑ Summary