include/psaml/psaml_clos.h

filter: Statementstmt 100% 47 / 47 Branchbran 68.5% 37 / 54 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 83.1% 84 / 101 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/psaml/psaml_metadata.h ↑ Summary include/psaml/psaml_response.h »
1 #ifndef PSAML_CLOS_H
2 #define PSAML_CLOS_H
3  
4 /* Closures, and the small predicates the rest of this distribution reads
5 * options with.
6 *
7 * This is Punk-Feed's pfeed_closure, which is Punk-Authorisation's
8 * pau_closure, which is Punk-TOTP's pp_closure, which is a copy of
9 * Punk's private punk_closure: a CV built with newXS carrying captured
10 * SVs in PERL_MAGIC_ext. Punk does not export it - pk_abi.h is an
11 * observer interface and says so - so a plugin that wants a body with
12 * state carries its own.
13 *
14 * The capture is an AV rather than a fixed pair, because the bodies here
15 * capture [app], [app, idp] and [app, idp, opts]. */
16  
17 #include "psaml_compat.h"
18  
19 typedef struct { AV *cap; } psaml_clos_t;
20  
21 166 static int psaml_clos_free(pTHX_ SV *sv, MAGIC *mg) {
22 166 psaml_clos_t *c = (psaml_clos_t *)mg->mg_ptr;
23 PERL_UNUSED_ARG(sv);
24 166 ▶ if (c) {
Branch: 1 branch
BranchTrueFalseTotal
if (c) { 166 0 166
25 166 ▶ if (c->cap) SvREFCNT_dec((SV *)c->cap);
Branch: 1 branch
BranchTrueFalseTotal
if (c->cap) SvREFCNT_dec((SV *)c->cap); 166 0 166
26 166 Safefree(c);
27 }
28 166 return 0;
29 }
30  
31 static MGVTBL psaml_clos_vtbl = { NULL, NULL, NULL, NULL, psaml_clos_free,
32 NULL, NULL, NULL };
33  
34 /* Takes ownership of cap. */
35 406 PERL_STATIC_INLINE SV *psaml_closure(pTHX_ XSUBADDR_t body, AV *cap) {
36 406 CV *cv = (CV *)newXS(NULL, body, (char *)__FILE__);
37 psaml_clos_t *c;
38 406 Newxz(c, 1, psaml_clos_t);
39 406 c->cap = cap;
40 406 sv_magicext((SV *)cv, NULL, PERL_MAGIC_ext, &psaml_clos_vtbl, (char *)c, 0);
41 406 return newRV_noinc((SV *)cv);
42 }
43  
44 219 PERL_STATIC_INLINE AV *psaml_cap_of(pTHX_ CV *cv) {
45 219 MAGIC *mg = mg_findext((SV *)cv, PERL_MAGIC_ext, &psaml_clos_vtbl);
46 219 ▶ AV *cap = mg ? ((psaml_clos_t *)mg->mg_ptr)->cap : NULL;
Branch: 1 branch
BranchTrueFalseTotal
AV *cap = mg ? ((psaml_clos_t *)mg->mg_ptr)->cap : NULL; 219 0 219
47 219 ▶ if (!cap) croak("Punk::Plugin::SAML: a closure lost its capture");
Branch: 1 branch
BranchTrueFalseTotal
if (!cap) croak("Punk::Plugin::SAML: a closure lost its capture"); 0 219 219
48 219 return cap;
49 }
50  
51 /* One capture slot, borrowed. */
52 219 PERL_STATIC_INLINE SV *psaml_cap_slot(pTHX_ CV *cv, SSize_t i) {
53 219 AV *cap = psaml_cap_of(aTHX_ cv);
54 219 SV **e = av_fetch(cap, i, 0);
55 219 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 219 0 219
return (e && *e) ? *e : NULL; 219 0 219
56 }
57  
58 /* ---- option predicates ----------------------------------------------- */
59  
60 /* A defined, non-empty string option, borrowed, or NULL.
61 *
62 * SvPV is taken once into a named variable rather than called twice,
63 * because a caller writing f(SvPV(sv, l), l) reads l before SvPV has set
64 * it: the two arguments are siblings and their evaluation order is not
65 * ordered by anything. */
66 PERL_STATIC_INLINE const char *psaml_opt_str(pTHX_ HV *o, const char *key, STRLEN *len) {
67 SV **e = hv_fetch(o, key, (I32)strlen(key), 0);
68 const char *p;
69 STRLEN n;
70 if (!e || !*e || !SvOK(*e)) return NULL;
71 p = SvPV_const(*e, n);
72 if (!n) return NULL;
73 if (len) *len = n;
74 return p;
75 }
76  
77 /* A boolean option with a stated default.
78 *
79 * SvTRUE is a multi-evaluating macro, so the fetched SV goes into a
80 * variable first. Around a POPs it corrupts the stack; here it would
81 * merely fetch twice, but the habit is the point: this family has been
82 * bitten by SvTRUE and SvIV on expressions with side effects more than
83 * once. */
84 1573 PERL_STATIC_INLINE int psaml_opt_bool(pTHX_ HV *o, const char *key, int dflt) {
85 1573 SV **e = hv_fetch(o, key, (I32)strlen(key), 0);
86 SV *sv;
87 1573 ▶ if (!e || !*e || !SvOK(*e)) return dflt;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) return dflt; 420 1153 1573
if (!e || !*e || !SvOK(*e)) return dflt; 420 0 420
if (!e || !*e || !SvOK(*e)) return dflt; 0 420 420
88 420 sv = *e;
89 420 return SvTRUE(sv) ? 1 : 0;
90 }
91  
92 597 PERL_STATIC_INLINE IV psaml_opt_iv(pTHX_ HV *o, const char *key, IV dflt) {
93 597 SV **e = hv_fetch(o, key, (I32)strlen(key), 0);
94 SV *sv;
95 597 ▶ if (!e || !*e || !SvOK(*e)) return dflt;
Branch: 3 branches
BranchTrueFalseTotal
if (!e || !*e || !SvOK(*e)) return dflt; 200 397 597
if (!e || !*e || !SvOK(*e)) return dflt; 200 0 200
if (!e || !*e || !SvOK(*e)) return dflt; 0 200 200
96 200 sv = *e;
97 200 return SvIV(sv);
98 }
99  
100 /* ---- small readers ---------------------------------------------------- */
101  
102 6184 PERL_STATIC_INLINE SV *psaml_hget(pTHX_ HV *h, const char *k) {
103 6184 ▶ SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV **e = h ? hv_fetch(h, k, (I32)strlen(k), 0) : NULL; 6184 0 6184
104 6184 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 3971 2213 6184
return (e && *e) ? *e : NULL; 3971 0 3971
105 }
106  
107 221 PERL_STATIC_INLINE int psaml_is_code(pTHX_ SV *sv) {
108 221 ▶ return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV;
Branch: 3 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; 197 24 221
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; 172 25 197
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVCV; 171 1 172
109 }
110  
111 127 PERL_STATIC_INLINE int psaml_is_hash(pTHX_ SV *sv) {
112 127 ▶ return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV;
Branch: 3 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; 127 0 127
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; 127 0 127
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVHV; 127 0 127
113 }
114  
115 189 PERL_STATIC_INLINE int psaml_is_array(pTHX_ SV *sv) {
116 189 ▶ return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV;
Branch: 3 branches
BranchTrueFalseTotal
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; 189 0 189
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; 20 169 189
return sv && SvROK(sv) && SvTYPE(SvRV(sv)) == SVt_PVAV; 19 1 20
117 }
118  
119 /* A non-negative integer option with a default. A negative skew or ttl is
120 * not a smaller one, it is a comparison that runs backwards. */
121 173 PERL_STATIC_INLINE IV psaml_opt_uv(pTHX_ HV *in, const char *k, IV dflt) {
122 173 SV *v = psaml_hget(aTHX_ in, k);
123 IV n;
124 173 ▶ if (!v || !SvOK(v)) return dflt;
Branch: 2 branches
BranchTrueFalseTotal
if (!v || !SvOK(v)) return dflt; 2 171 173
if (!v || !SvOK(v)) return dflt; 0 2 2
125 2 n = SvIV(v);
126 2 ▶ if (n < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) 1 1 2
127 1 croak("%s: `%s` must be zero or more, not %" IVdf, PSAML_WHO, k, n);
128 1 return n;
129 }
130  
131 #endif /* PSAML_CLOS_H */
« include/psaml/psaml_metadata.h ↑ Summary include/psaml/psaml_response.h »