include/punk/punk_scope.h

filter: Statementstmt 97.6% 41 / 42 Branchbran 59.6% 31 / 52 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 76.6% 72 / 94 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_blob.h ↑ Summary include/punk/punk_accept.h »
1 /* punk_scope.h - the `under` handle, in C.
2 *
3 * A scope is a blessed hash carrying its accumulated prefix and its
4 * outer-to-inner guard chain (flattened at creation, never at request time).
5 * It records into the application exactly as the top-level DSL does; to_app
6 * resolves and freezes. Nothing here runs per request - this is boot-time
7 * registration - so the point of the port is not speed but keeping the whole
8 * registrar surface in one place now that Punk::App is C.
9 *
10 * Must be included after punk_context.h (for pcx_call_meth).
11 */
12  
13 #ifndef PUNK_SCOPE_H
14 #define PUNK_SCOPE_H
15  
16 122 static HV *scope_hv(pTHX_ SV *self) {
17 122 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 122 0 122
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 122 122
18 0 croak("Punk::Router::Scope: not a scope object");
19 122 return (HV *)SvRV(self);
20 }
21  
22 237 static SV *scope_get(pTHX_ HV *h, const char *k) {
23 237 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
24 237 ▶ return (e && *e) ? *e : NULL;
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; 237 0 237
return (e && *e) ? *e : NULL; 237 0 237
25 }
26  
27 /* the scope's guard chain, or NULL when it holds none */
28 80 static AV *scope_guards_av(pTHX_ HV *h) {
29 80 SV *g = scope_get(aTHX_ h, "guards");
30 80 ▶ return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; 80 0 80
return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; 80 0 80
return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; 80 0 80
31 }
32  
33 /* A fresh arrayref holding this scope's guards, optionally with one more
34 * appended. The copy matters: the scope hands its chain to every route it
35 * registers and to every nested scope, and a shared arrayref would let a
36 * later `under` retroactively add a guard to routes already recorded. */
37 80 static SV *scope_guards_copy(pTHX_ HV *h, SV *extra) {
38 80 AV *src = scope_guards_av(aTHX_ h);
39 80 AV *out = newAV();
40 80 ▶ if (src) {
Branch: 1 branch
BranchTrueFalseTotal
if (src) { 80 0 80
41 80 SSize_t i, n = av_len(src) + 1;
42 80 av_extend(out, n);
43 141 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 61 80 141
44 61 SV **e = av_fetch(src, i, 0);
45 61 ▶ av_push(out, (e && *e) ? newSVsv(*e) : newSV(0));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); 61 0 61
av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); 61 0 61
46 }
47 }
48 80 ▶ if (extra && SvOK(extra)) av_push(out, newSVsv(extra));
Branch: 2 branches
BranchTrueFalseTotal
if (extra && SvOK(extra)) av_push(out, newSVsv(extra)); 5 75 80
if (extra && SvOK(extra)) av_push(out, newSVsv(extra)); 2 3 5
49 80 return newRV_noinc((SV *)out);
50 }
51  
52 /* prefix . path, with the two edge cases the Perl had:
53 * a '/' path contributes nothing (so `under '/admin'` plus get '/' is
54 * '/admin', not '/admin/'), and an empty result is the root. Mortal. */
55 52 static SV *scope_full_path(pTHX_ HV *h, SV *path) {
56 52 SV *pfx = scope_get(aTHX_ h, "prefix");
57 52 ▶ SV *out = newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no);
Branch: 2 branches
BranchTrueFalseTotal
SV *out = newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no); 52 0 52
SV *out = newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no); 52 0 52
58 STRLEN pl;
59 104 ▶ const char *p = SvOK(path) ? SvPV_const(path, pl) : "";
Branch: 1 branch
BranchTrueFalseTotal
const char *p = SvOK(path) ? SvPV_const(path, pl) : ""; 52 0 52
60 52 ▶ if (!SvOK(path)) pl = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(path)) pl = 0; 0 52 52
61 52 ▶ if (!(pfx && SvOK(pfx))) sv_setpvs(out, "");
Branch: 2 branches
BranchTrueFalseTotal
if (!(pfx && SvOK(pfx))) sv_setpvs(out, ""); 52 0 52
if (!(pfx && SvOK(pfx))) sv_setpvs(out, ""); 0 52 52
62 52 ▶ if (!(pl == 1 && p[0] == '/')) sv_catpvn(out, p, pl);
Branch: 2 branches
BranchTrueFalseTotal
if (!(pl == 1 && p[0] == '/')) sv_catpvn(out, p, pl); 3 49 52
if (!(pl == 1 && p[0] == '/')) sv_catpvn(out, p, pl); 0 3 3
63 52 ▶ if (!SvCUR(out)) sv_setpvs(out, "/");
Branch: 1 branch
BranchTrueFalseTotal
if (!SvCUR(out)) sv_setpvs(out, "/"); 1 51 52
64 52 return sv_2mortal(out);
65 }
66  
67 /* Build a scope of the same class as $self (so a subclass nests as itself),
68 * through its own new - the constructor is the documented seam. */
69 5 static SV *scope_new_like(pTHX_ SV *self, SV *app, SV *prefix, SV *guards) {
70 SV *argv[6];
71 5 SV *class = sv_2mortal(newSVpv(sv_reftype(SvRV(self), 1), 0));
72 SV *r;
73 5 ▶ argv[0] = sv_2mortal(newSVpvs("app")); argv[1] = app ? app : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
argv[0] = sv_2mortal(newSVpvs("app")); argv[1] = app ? app : &PL_sv_undef; 5 0 5
74 5 argv[2] = sv_2mortal(newSVpvs("prefix")); argv[3] = prefix;
75 5 argv[4] = sv_2mortal(newSVpvs("guards")); argv[5] = guards;
76 5 r = pcx_call_meth(aTHX_ class, "new", argv, 6, 1);
77 5 ▶ return r ? r : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
return r ? r : &PL_sv_undef; 5 0 5
78 }
79  
80 /* $self->{app}->$meth(...), the one shape every registration here takes. */
81 55 static void scope_app_call(pTHX_ HV *h, const char *meth, SV **argv, int nargs) {
82 55 SV *app = scope_get(aTHX_ h, "app");
83 55 ▶ SV *r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, meth, argv, nargs, 1);
Branch: 1 branch
BranchTrueFalseTotal
SV *r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, meth, argv, nargs, 1); 55 0 55
84 55 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 55 0 55
85 55 }
86  
87 #endif /* PUNK_SCOPE_H */
« include/punk/punk_blob.h ↑ Summary include/punk/punk_accept.h »