lib/B/CallChecker.xs

filter: Statementstmt 96.6% 116 / 120 Branchbran 60.5% 23 / 38 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.9% 139 / 158 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/B/CallChecker.pm »
1 #define PERL_NO_GET_CONTEXT 1
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "callchecker0.h"
5 #include "XSUB.h"
6  
7 #ifndef GvCV_set
8 # define GvCV_set(gv, cv) (GvCV(gv) = (cv))
9 #endif /* !GvCV_set */
10  
11 #ifndef CvGV_set
12 # define CvGV_set(cv, gv) (CvGV(cv) = (gv))
13 #endif /* !CvGV_set */
14  
15 #ifndef CvISXSUB
16 # define CvISXSUB(cv) !!CvXSUB(cv)
17 #endif /* !CvISXSUB */
18  
19 #ifndef CvISXSUB_on
20 # define CvISXSUB_on(cv) ((void) (cv))
21 #endif /* !CvISXSUB_on */
22  
23 #ifndef sv_setpvs
24 # define sv_setpvs(SV, STR) sv_setpvn(SV, ""STR"", sizeof(STR)-1)
25 #endif /* !sv_setpvs */
26  
27 #ifndef gv_stashpvs
28 # define gv_stashpvs(name, flags) gv_stashpvn(""name"", sizeof(name)-1, flags)
29 #endif /* !gv_stashpvs */
30  
31 #ifndef PERL_UNUSED_ARG
32 # define PERL_UNUSED_ARG(x) PERL_UNUSED_VAR(x)
33 #endif /* !PERL_UNUSED_ARG */
34  
35 #ifndef Newx
36 # define Newx(v,n,t) New(0,v,n,t)
37 #endif /* !Newx */
38  
39 #ifndef ptr_table_new
40  
41 struct q_ptr_tbl_ent {
42 struct q_ptr_tbl_ent *next;
43 void *from, *to;
44 };
45  
46 # undef PTR_TBL_t
47 # define PTR_TBL_t struct q_ptr_tbl_ent *
48  
49 # define ptr_table_new() THX_ptr_table_new(aTHX)
50 static PTR_TBL_t *THX_ptr_table_new(pTHX)
51 {
52 PTR_TBL_t *tbl;
53 Newx(tbl, 1, PTR_TBL_t);
54 *tbl = NULL;
55 return tbl;
56 }
57  
58 # define ptr_table_free(tbl) THX_ptr_table_free(aTHX_ tbl)
59 static void THX_ptr_table_free(pTHX_ PTR_TBL_t *tbl)
60 {
61 struct q_ptr_tbl_ent *ent = *tbl;
62 Safefree(tbl);
63 while(ent) {
64 struct q_ptr_tbl_ent *nent = ent->next;
65 Safefree(ent);
66 ent = nent;
67 }
68 }
69  
70 # define ptr_table_store(tbl, from, to) THX_ptr_table_store(aTHX_ tbl, from, to)
71 static void THX_ptr_table_store(pTHX_ PTR_TBL_t *tbl, void *from, void *to)
72 {
73 struct q_ptr_tbl_ent *ent;
74 Newx(ent, 1, struct q_ptr_tbl_ent);
75 ent->next = *tbl;
76 ent->from = from;
77 ent->to = to;
78 *tbl = ent;
79 }
80  
81 # define ptr_table_fetch(tbl, from) THX_ptr_table_fetch(aTHX_ tbl, from)
82 static void *THX_ptr_table_fetch(pTHX_ PTR_TBL_t *tbl, void *from)
83 {
84 struct q_ptr_tbl_ent *ent;
85 for(ent = *tbl; ent; ent = ent->next) {
86 if(ent->from == from) return ent->to;
87 }
88 return NULL;
89 }
90  
91 #endif /* !ptr_table_new */
92  
93 #ifndef DPTR2FPTR
94 # define DPTR2FPTR(t,x) ((t)(UV)(x))
95 #endif /* !DPTR2FPTR */
96  
97 #ifndef FPTR2DPTR
98 # define FPTR2DPTR(t,x) ((t)(UV)(x))
99 #endif /* !FPTR2DPTR */
100  
101 #ifndef newSV_type
102 # define newSV_type(type) THX_newSV_type(aTHX_ type)
103 static SV *THX_newSV_type(pTHX_ svtype type)
104 {
105 SV *sv = newSV(0);
106 (void) SvUPGRADE(sv, type);
107 return sv;
108 }
109 #endif /* !newSV_type */
110  
111 /*
112 * representing op pointer as B::OP object reference
113 */
114  
115 static HV *stash_bop;
116  
117 #define decode_bop(bopref) THX_decode_bop(aTHX_ bopref)
118 12 static OP *THX_decode_bop(pTHX_ SV *bopref)
119 {
120 /*
121 * This logic comes from B's typemap entry for B::OP. It does
122 * not check for the alleged B::OP object being blessed into a
123 * B::OP class.
124 */
125 12 ▶ if(!SvROK(bopref)) croak("bad B::OP reference");
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(bopref)) croak("bad B::OP reference"); 0 12 12
126 12 return INT2PTR(OP*, SvIV(SvRV(bopref)));
127 }
128  
129 #define encode_bop(op) THX_encode_bop(aTHX_ op)
130 12 static SV *THX_encode_bop(pTHX_ OP *op)
131 {
132 /*
133 * All the logic for blessing into the right B::OP class
134 * is in internal functions in B. We really want to call
135 * make_op_object() from B.xs. This is a roundabout way of
136 * getting to it.
137 */
138 OP stalkop;
139 SV *stalkbop, *bop;
140 12 stalkop.op_next = op;
141 12 stalkbop = sv_2mortal(newRV_noinc(newSViv(INT2PTR(IV, &stalkop))));
142 12 sv_bless(stalkbop, stash_bop);
143 {
144 12 dSP;
145 12 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 12 12
146 12 ▶ XPUSHs(stalkbop);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(stalkbop); 0 12 12
147 12 PUTBACK;
148 12 call_method("next", G_SCALAR);
149 12 SPAGAIN;
150 12 bop = POPs;
151 12 PUTBACK;
152 }
153 12 return bop;
154 }
155  
156 /*
157 * representing C call-checker function as Perl sub
158 */
159  
160 static void xsfunc_c_ckfun(pTHX_ CV *);
161  
162 static PTR_TBL_t *ckfun_cap_map;
163  
164 #define ckfun_encode_c_as_perl(cckfun) THX_ckfun_encode_c_as_perl(aTHX_ cckfun)
165 12 static CV *THX_ckfun_encode_c_as_perl(pTHX_ Perl_call_checker cckfun)
166 {
167 12 void *vcckfun = FPTR2DPTR(void *, cckfun);
168 CV *ckfun;
169 12 ▶ if((ckfun = ptr_table_fetch(ckfun_cap_map, vcckfun)))
Branch: 1 branch
BranchTrueFalseTotal
if((ckfun = ptr_table_fetch(ckfun_cap_map, vcckfun))) 8 4 12
170 8 return ckfun;
171 4 ckfun = (CV*)newSV_type(SVt_PVCV);
172 4 sv_setpvs((SV*)ckfun, "$$$");
173 4 CvXSUBANY(ckfun).any_ptr = vcckfun;
174 4 CvXSUB(ckfun) = xsfunc_c_ckfun;
175 4 CvISXSUB_on(ckfun);
176 4 ptr_table_store(ckfun_cap_map, vcckfun, (void*)ckfun);
177 4 return ckfun;
178 }
179  
180 #define ckfun_perl_is_encoded_c(ckfun) THX_ckfun_perl_is_encoded_c(aTHX_ ckfun)
181 7 static bool THX_ckfun_perl_is_encoded_c(pTHX_ CV *ckfun)
182 {
183 7 ▶ return CvISXSUB(ckfun) && CvXSUB(ckfun) == xsfunc_c_ckfun;
Branch: 2 branches
BranchTrueFalseTotal
return CvISXSUB(ckfun) && CvXSUB(ckfun) == xsfunc_c_ckfun; 2 5 7
return CvISXSUB(ckfun) && CvXSUB(ckfun) == xsfunc_c_ckfun; 2 0 2
184 }
185  
186 #define ckfun_decode_c_as_perl(ckfun) THX_ckfun_decode_c_as_perl(aTHX_ ckfun)
187 4 static Perl_call_checker THX_ckfun_decode_c_as_perl(pTHX_ CV *ckfun)
188 {
189 4 return DPTR2FPTR(Perl_call_checker, CvXSUBANY(ckfun).any_ptr);
190 }
191  
192 2 static void xsfunc_c_ckfun(pTHX_ CV *ckfun)
193 {
194 SV *ckobj_st, *namegv_st, *entersubop_st, *ckobj;
195 GV *namegv;
196 OP *entersubop;
197 2 Perl_call_checker cckfun = ckfun_decode_c_as_perl(ckfun);
198 2 dSP; dMARK;
199 2 ▶ if(SP - MARK != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if(SP - MARK != 3) { 0 2 2
200 0 bad_args:
201 0 croak("non-Perl call checker called incorrectly");
202 }
203 2 ckobj_st = POPs;
204 2 namegv_st = POPs;
205 2 entersubop_st = TOPs;
206 2 PUTBACK;
207 2 ▶ if(!SvROK(ckobj_st)) goto bad_args;
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(ckobj_st)) goto bad_args; 0 2 2
208 2 ckobj = SvRV(ckobj_st);
209 2 ▶ if(!SvROK(namegv_st)) goto bad_args;
Branch: 1 branch
BranchTrueFalseTotal
if(!SvROK(namegv_st)) goto bad_args; 0 2 2
210 2 namegv = (GV*)SvRV(namegv_st);
211 2 ▶ if(SvTYPE((SV*)namegv) != SVt_PVGV) goto bad_args;
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE((SV*)namegv) != SVt_PVGV) goto bad_args; 0 2 2
212 2 entersubop = decode_bop(entersubop_st);
213 2 entersubop = cckfun(aTHX_ entersubop, namegv, ckobj);
214 2 entersubop_st = encode_bop(entersubop);
215 2 SPAGAIN;
216 2 TOPs = entersubop_st;
217 2 }
218  
219 /*
220 * representing Perl call-checker sub as C function
221 */
222  
223 static OP *cckfun_perl_ckfun(pTHX_ OP *entersubop, GV *namegv, SV *cckobj);
224  
225 #define ckfun_encode_perl_as_c(ckfun, ckobj, cckfun_p, cckobj_p) \
226 THX_ckfun_encode_perl_as_c(aTHX_ ckfun, ckobj, cckfun_p, cckobj_p)
227 5 static void THX_ckfun_encode_perl_as_c(pTHX_
228 CV *ckfun, SV *ckobj, Perl_call_checker *cckfun_p, SV **cckobj_p)
229 {
230 5 SV *cckobj = sv_2mortal((SV*)newAV());
231 5 av_extend((AV*)cckobj, 1);
232 5 av_store((AV*)cckobj, 0, SvREFCNT_inc((SV*)ckfun));
233 5 av_store((AV*)cckobj, 1, SvREFCNT_inc(ckobj));
234 5 *cckfun_p = cckfun_perl_ckfun;
235 5 *cckobj_p = cckobj;
236 5 }
237  
238 #define ckfun_c_is_encoded_perl(cckfun) \
239 THX_ckfun_c_is_encoded_perl(aTHX_ cckfun)
240 18 static bool THX_ckfun_c_is_encoded_perl(pTHX_ Perl_call_checker cckfun)
241 {
242 18 return cckfun == cckfun_perl_ckfun;
243 }
244  
245 #define ckfun_decode_perl_as_c(cckfun, cckobj, ckfun_p, ckobj_p) \
246 THX_ckfun_decode_perl_as_c(aTHX_ cckfun, cckobj, ckfun_p, ckobj_p)
247 16 static void THX_ckfun_decode_perl_as_c(pTHX_
248 Perl_call_checker cckfun, SV *cckobj, CV **ckfun_p, SV **ckobj_p)
249 {
250 SV **valp;
251 PERL_UNUSED_ARG(cckfun);
252 16 ▶ if(SvTYPE(cckobj) != SVt_PVAV || av_len((AV*)cckobj) != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if(SvTYPE(cckobj) != SVt_PVAV || av_len((AV*)cckobj) != 1) { 0 16 16
if(SvTYPE(cckobj) != SVt_PVAV || av_len((AV*)cckobj) != 1) { 0 16 16
253 0 bad_args:
254 0 croak("call checker shim called incorrectly");
255 }
256 16 *ckfun_p = (CV*)*av_fetch((AV*)cckobj, 0, 0);
257 16 ▶ if(SvTYPE((SV*)*ckfun_p) != SVt_PVCV) goto bad_args;
Branch: 1 branch
BranchTrueFalseTotal
if(SvTYPE((SV*)*ckfun_p) != SVt_PVCV) goto bad_args; 0 16 16
258 16 valp = av_fetch((AV*)cckobj, 1, 0);
259 16 ▶ *ckobj_p = valp ? *valp : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
*ckobj_p = valp ? *valp : &PL_sv_undef; 16 0 16
260 16 }
261  
262 6 static OP *cckfun_perl_ckfun(pTHX_ OP *entersubop, GV *namegv, SV *cckobj)
263 {
264 SV *ckobj_st, *namegv_st, *entersubop_st, *ckobj;
265 CV *ckfun;
266 6 ckfun_decode_perl_as_c(0, cckobj, &ckfun, &ckobj);
267 6 entersubop_st = encode_bop(entersubop);
268 6 namegv_st = sv_2mortal(newRV_inc((SV*)namegv));
269 6 ckobj_st = sv_2mortal(newRV_inc(ckobj));
270 6 ENTER;
271 {
272 6 dSP;
273 6 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 6 6
274 6 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 6 6
275 6 PUSHs(entersubop_st);
276 6 PUSHs(namegv_st);
277 6 PUSHs(ckobj_st);
278 6 PUTBACK;
279 6 call_sv((SV*)ckfun, G_SCALAR);
280 6 SPAGAIN;
281 6 entersubop_st = POPs;
282 6 PUTBACK;
283 }
284 6 LEAVE;
285 6 return decode_bop(entersubop_st);
286 }
287  
288 #define install_cv(cv, name) THX_install_cv(aTHX_ cv, name)
289 4 static void THX_install_cv(pTHX_ CV *cv, char const *name)
290 {
291 4 GV *gv = gv_fetchpv(name, GV_ADDMULTI, SVt_PVCV);
292 4 GvCV_set(gv, cv);
293 4 GvCVGEN(gv) = 0;
294 4 CvGV_set(cv, gv);
295 4 }
296  
297 typedef SV *SVREF;
298  
299 MODULE = B::CallChecker PACKAGE = B::CallChecker
300  
301 PROTOTYPES: DISABLE
302  
303 BOOT:
304 2 ckfun_cap_map = ptr_table_new();
305 2 stash_bop = gv_stashpvs("B::OP", 1);
306 2 install_cv(ckfun_encode_c_as_perl(Perl_ck_entersub_args_proto),
307 "B::CallChecker::ck_entersub_args_proto");
308 2 install_cv(ckfun_encode_c_as_perl(Perl_ck_entersub_args_proto_or_list),
309 "B::CallChecker::ck_entersub_args_proto_or_list");
310  
311 void
312 cv_get_call_checker(CV *tgtcv)
313 PROTOTYPE: $
314 PREINIT:
315 Perl_call_checker cckfun;
316 SV *cckobj;
317 CV *ckfun;
318 SV *ckobj;
319 PPCODE:
320 18 PUTBACK;
321 18 cv_get_call_checker(tgtcv, &cckfun, &cckobj);
322 18 ▶ if(ckfun_c_is_encoded_perl(cckfun)) {
Branch: 1 branch
BranchTrueFalseTotal
if(ckfun_c_is_encoded_perl(cckfun)) { 10 8 18
323 10 ckfun_decode_perl_as_c(cckfun, cckobj, &ckfun, &ckobj);
324 } else {
325 8 ckfun = ckfun_encode_c_as_perl(cckfun);
326 8 ckobj = cckobj;
327 }
328 18 SPAGAIN;
329 18 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 18 18
330 18 PUSHs(sv_2mortal(newRV_inc((SV*)ckfun)));
331 18 PUSHs(sv_2mortal(newRV_inc(ckobj)));
332  
333 void
334 cv_set_call_checker(CV *tgtcv, CV *ckfun, SVREF ckobj)
335 PROTOTYPE: $$$
336 PREINIT:
337 Perl_call_checker cckfun;
338 SV *cckobj;
339 CODE:
340 7 PUTBACK;
341 7 ▶ if(ckfun_perl_is_encoded_c(ckfun)) {
Branch: 1 branch
BranchTrueFalseTotal
if(ckfun_perl_is_encoded_c(ckfun)) { 2 5 7
342 2 cckfun = ckfun_decode_c_as_perl(ckfun);
343 2 cckobj = ckobj;
344 } else {
345 5 ckfun_encode_perl_as_c(ckfun, ckobj, &cckfun, &cckobj);
346 }
347 7 cv_set_call_checker(tgtcv, cckfun, cckobj);
348 7 SPAGAIN;
349  
350 OP *
351 ck_entersub_args_list(OP *entersubop)
352 PROTOTYPE: $
353 CODE:
354 4 PUTBACK;
355 4 RETVAL = ck_entersub_args_list(entersubop);
356 4 SPAGAIN;
357 OUTPUT:
358 RETVAL
↑ Summary blib/lib/B/CallChecker.pm »