include/punk/pk_abi_impl.h

filter: Statementstmt 100% 39 / 39 Branchbran 56.4% 35 / 62 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 73.2% 74 / 101 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/session.xs ↑ Summary xs/cache.xs »
1 /* pk_abi_impl.h - Punk's side of its public C ABI (pk_abi.h).
2 *
3 * Included by Punk.xs AFTER punk_context.h (the PCX_* slot layout) and
4 * punk_obs.h (the observer registry). Everything here is private to Punk's
5 * translation unit; a consumer reaches it only through the PK_ABI table
6 * returned by Punk::_abi_ptr.
7 */
8  
9 #ifndef PK_ABI_IMPL_H
10 #define PK_ABI_IMPL_H
11  
12 #include "pk_abi.h"
13  
14 /* A context's AV, or NULL for anything that is not one. The whole table has
15 * to be safe to call on a value the consumer only thinks is a context, so
16 * this never croaks the way pcx_av does. */
17 1451 static AV *pk_abi_av(pTHX_ SV *c) {
18 PERL_UNUSED_CONTEXT;
19 1451 ▶ if (!c || !SvROK(c) || SvTYPE(SvRV(c)) != SVt_PVAV) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!c || !SvROK(c) || SvTYPE(SvRV(c)) != SVt_PVAV) return NULL; 1451 0 1451
if (!c || !SvROK(c) || SvTYPE(SvRV(c)) != SVt_PVAV) return NULL; 1451 0 1451
if (!c || !SvROK(c) || SvTYPE(SvRV(c)) != SVt_PVAV) return NULL; 0 1451 1451
20 1451 return (AV *)SvRV(c);
21 }
22  
23 /* One slot, borrowed; NULL when absent or undef. */
24 1403 static SV *pk_abi_slot(pTHX_ SV *c, SSize_t i) {
25 1403 AV *av = pk_abi_av(aTHX_ c);
26 1403 ▶ SV **e = av ? av_fetch(av, i, 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = av ? av_fetch(av, i, 0) : NULL; 1403 0 1403
27 1403 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 1403 0 1403
return (e && *e && SvOK(*e)) ? *e : NULL; 1403 0 1403
return (e && *e && SvOK(*e)) ? *e : NULL; 1403 0 1403
28 }
29  
30 677 static SV *pk_abi_env_of(pTHX_ SV *c) { return pk_abi_slot(aTHX_ c, PCX_ENV); }
31 48 static SV *pk_abi_app_of(pTHX_ SV *c) { return pk_abi_slot(aTHX_ c, PCX_APP); }
32 678 static SV *pk_abi_match_of(pTHX_ SV *c) { return pk_abi_slot(aTHX_ c, PCX_MATCH); }
33  
34 /* The stash, created on first ask. A consumer's first act is normally to
35 * leave something for the response side to find, and it should not have to
36 * know the slot layout to do it. */
37 48 static SV *pk_abi_stash_of(pTHX_ SV *c) {
38 48 AV *av = pk_abi_av(aTHX_ c);
39 SV *st;
40 48 ▶ if (!av) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!av) return NULL; 0 48 48
41 48 st = pcx_get(aTHX_ av, PCX_STASH);
42 48 ▶ if (!st) {
Branch: 1 branch
BranchTrueFalseTotal
if (!st) { 24 24 48
43 24 st = newRV_noinc((SV *)newHV());
44 24 ▶ if (!av_store(av, PCX_STASH, st)) { SvREFCNT_dec(st); return NULL; }
Branch: 1 branch
BranchTrueFalseTotal
if (!av_store(av, PCX_STASH, st)) { SvREFCNT_dec(st); return NULL; } 0 24 24
45 }
46 48 return st;
47 }
48  
49 /* The match hash, or NULL. */
50 630 static HV *pk_abi_match_hv(pTHX_ SV *c) {
51 630 SV *m = pk_abi_match_of(aTHX_ c);
52 630 ▶ if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) return NULL;
Branch: 3 branches
BranchTrueFalseTotal
if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) return NULL; 630 0 630
if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) return NULL; 630 0 630
if (!(m && SvROK(m) && SvTYPE(SvRV(m)) == SVt_PVHV)) return NULL; 0 630 630
53 630 return (HV *)SvRV(m);
54 }
55  
56 /* The declared route path, through the route record the dispatcher stores in
57 * the match. NULL whenever there is no matched route to name, which includes
58 * every request-start callback: routing has not happened yet. */
59 350 static SV *pk_abi_route_pattern_of(pTHX_ SV *c) {
60 350 HV *mh = pk_abi_match_hv(aTHX_ c);
61 SV **r, **p;
62 350 ▶ if (!mh) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!mh) return NULL; 0 350 350
63 350 r = hv_fetchs(mh, "route", 0);
64 350 ▶ if (!(r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVHV)) return NULL;
Branch: 4 branches
BranchTrueFalseTotal
if (!(r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVHV)) return NULL; 86 264 350
if (!(r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVHV)) return NULL; 86 0 86
if (!(r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVHV)) return NULL; 86 0 86
if (!(r && *r && SvROK(*r) && SvTYPE(SvRV(*r)) == SVt_PVHV)) return NULL; 0 86 86
65 86 p = hv_fetchs((HV *)SvRV(*r), K_PATH, 0);
66 86 ▶ return (p && *p && SvOK(*p)) ? *p : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (p && *p && SvOK(*p)) ? *p : NULL; 86 0 86
return (p && *p && SvOK(*p)) ? *p : NULL; 86 0 86
return (p && *p && SvOK(*p)) ? *p : NULL; 86 0 86
67 }
68  
69 280 static SV *pk_abi_operation_of(pTHX_ SV *c) {
70 280 HV *mh = pk_abi_match_hv(aTHX_ c);
71 280 ▶ SV **o = mh ? hv_fetchs(mh, "operation", 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **o = mh ? hv_fetchs(mh, "operation", 0) : NULL; 280 0 280
72 280 ▶ return (o && *o && SvOK(*o)) ? *o : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (o && *o && SvOK(*o)) ? *o : NULL; 2 278 280
return (o && *o && SvOK(*o)) ? *o : NULL; 2 0 2
return (o && *o && SvOK(*o)) ? *o : NULL; 2 0 2
73 }
74  
75 /* The status out of a PSGI triplet; -1 when there is none to read - a
76 * streaming coderef, or anything that is not a triplet. */
77 326 static IV pk_abi_status_of(pTHX_ SV *response) {
78 AV *ra;
79 SV **s;
80 326 ▶ if (!response || !SvROK(response) || SvTYPE(SvRV(response)) != SVt_PVAV)
Branch: 3 branches
BranchTrueFalseTotal
if (!response || !SvROK(response) || SvTYPE(SvRV(response)) != SVt_PVAV) 326 0 326
if (!response || !SvROK(response) || SvTYPE(SvRV(response)) != SVt_PVAV) 326 0 326
if (!response || !SvROK(response) || SvTYPE(SvRV(response)) != SVt_PVAV) 1 325 326
81 1 return -1;
82 325 ra = (AV *)SvRV(response);
83 325 s = av_fetch(ra, 0, 0);
84 325 ▶ return (s && *s && SvOK(*s)) ? SvIV(*s) : -1;
Branch: 3 branches
BranchTrueFalseTotal
return (s && *s && SvOK(*s)) ? SvIV(*s) : -1; 325 0 325
return (s && *s && SvOK(*s)) ? SvIV(*s) : -1; 325 0 325
return (s && *s && SvOK(*s)) ? SvIV(*s) : -1; 325 0 325
85 }
86  
87 static const pk_abi PK_ABI = {
88 PK_ABI_VERSION,
89 pk_abi_env_of,
90 pk_abi_app_of,
91 pk_abi_match_of,
92 pk_abi_stash_of,
93 pk_abi_route_pattern_of,
94 pk_abi_operation_of,
95 pk_abi_status_of,
96 pk_obs_add_req,
97 pk_obs_add_res,
98 pk_obs_add_query, /* v2 */
99 pl_observe, /* v3 */
100 pl_observe_ctx, /* v4 */
101 pk_cur_get, /* v5 */
102 };
103  
104 #endif /* PK_ABI_IMPL_H */
« xs/session.xs ↑ Summary xs/cache.xs »