| 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 |
145 |
| static HV *scope_hv(pTHX_ SV *self) { |
| 17 |
|
| /* |
| 18 |
|
| # uncoverable branch true |
| 19 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 20 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 21 |
145 |
▶ | if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
145 |
0 |
145 |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) |
0 |
145 |
145 |
|
| 22 |
0 |
| croak("Punk::Router::Scope: not a scope object"); /* # uncoverable statement */ |
| 23 |
145 |
| return (HV *)SvRV(self); |
| 24 |
|
| } |
| 25 |
|
| |
| 26 |
282 |
| static SV *scope_get(pTHX_ HV *h, const char *k) { |
| 27 |
282 |
| SV **e = hv_fetch(h, k, (I32)strlen(k), 0); |
| 28 |
|
| /* |
| 29 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 30 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 31 |
282 |
▶ | return (e && *e) ? *e : NULL; |
Branch: 2 branches
| Branch | True | False | Total |
| return (e && *e) ? *e : NULL; |
282 |
0 |
282 |
| return (e && *e) ? *e : NULL; |
282 |
0 |
282 |
|
| 32 |
|
| } |
| 33 |
|
| |
| 34 |
|
| /* the scope's guard chain, or NULL when it holds none */ |
| 35 |
95 |
| static AV *scope_guards_av(pTHX_ HV *h) { |
| 36 |
95 |
| SV *g = scope_get(aTHX_ h, "guards"); |
| 37 |
|
| /* |
| 38 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 39 |
|
| # uncoverable condition right count:2 class:ignore_covered_err |
| 40 |
|
| # uncoverable condition right count:3 class:ignore_covered_err */ |
| 41 |
95 |
▶ | return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; |
95 |
0 |
95 |
| return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; |
95 |
0 |
95 |
| return (g && SvROK(g) && SvTYPE(SvRV(g)) == SVt_PVAV) ? (AV *)SvRV(g) : NULL; |
95 |
0 |
95 |
|
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| /* A fresh arrayref holding this scope's guards, optionally with one more |
| 45 |
|
| * appended. The copy matters: the scope hands its chain to every route it |
| 46 |
|
| * registers and to every nested scope, and a shared arrayref would let a |
| 47 |
|
| * later `under` retroactively add a guard to routes already recorded. */ |
| 48 |
95 |
| static SV *scope_guards_copy(pTHX_ HV *h, SV *extra) { |
| 49 |
95 |
| AV *src = scope_guards_av(aTHX_ h); |
| 50 |
95 |
| AV *out = newAV(); |
| 51 |
|
| /* |
| 52 |
|
| # uncoverable branch false |
| 53 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 54 |
95 |
▶ | if (src) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (src) { |
95 |
0 |
95 |
|
| 55 |
95 |
| SSize_t i, n = av_len(src) + 1; |
| 56 |
95 |
| av_extend(out, n); |
| 57 |
167 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
72 |
95 |
167 |
|
| 58 |
72 |
| SV **e = av_fetch(src, i, 0); |
| 59 |
|
| /* |
| 60 |
|
| # uncoverable condition right count:1 class:ignore_covered_err |
| 61 |
|
| # uncoverable condition right count:2 class:ignore_covered_err */ |
| 62 |
72 |
▶ | av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); |
Branch: 2 branches
| Branch | True | False | Total |
| av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); |
72 |
0 |
72 |
| av_push(out, (e && *e) ? newSVsv(*e) : newSV(0)); |
72 |
0 |
72 |
|
| 63 |
|
| } |
| 64 |
|
| } |
| 65 |
95 |
▶ | if (extra && SvOK(extra)) av_push(out, newSVsv(extra)); |
Branch: 2 branches
| Branch | True | False | Total |
| if (extra && SvOK(extra)) av_push(out, newSVsv(extra)); |
5 |
90 |
95 |
| if (extra && SvOK(extra)) av_push(out, newSVsv(extra)); |
2 |
3 |
5 |
|
| 66 |
95 |
| return newRV_noinc((SV *)out); |
| 67 |
|
| } |
| 68 |
|
| |
| 69 |
|
| /* prefix . path, with the two edge cases the Perl had: |
| 70 |
|
| * a '/' path contributes nothing (so `under '/admin'` plus get '/' is |
| 71 |
|
| * '/admin', not '/admin/'), and an empty result is the root. Mortal. */ |
| 72 |
63 |
| static SV *scope_full_path(pTHX_ HV *h, SV *path) { |
| 73 |
63 |
| SV *pfx = scope_get(aTHX_ h, "prefix"); |
| 74 |
|
| /* |
| 75 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 76 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 77 |
63 |
▶ | SV *out = newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no); |
Branch: 2 branches
| Branch | True | False | Total |
| SV *out = newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no); |
63 |
0 |
63 |
| SV *out = newSVsv(pfx && SvOK(pfx) ? pfx : &PL_sv_no); |
63 |
0 |
63 |
|
| 78 |
|
| STRLEN pl; |
| 79 |
126 |
▶ | const char *p = SvOK(path) ? SvPV_const(path, pl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| const char *p = SvOK(path) ? SvPV_const(path, pl) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ |
63 |
0 |
63 |
|
| 80 |
|
| /* |
| 81 |
|
| # uncoverable branch true |
| 82 |
|
| # uncoverable condition left count:1 class:ignore_covered_err */ |
| 83 |
63 |
▶ | if (!SvOK(path)) pl = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvOK(path)) pl = 0; |
0 |
63 |
63 |
|
| 84 |
|
| /* |
| 85 |
|
| # uncoverable branch true |
| 86 |
|
| # uncoverable condition left count:1 class:ignore_covered_err |
| 87 |
|
| # uncoverable condition left count:2 class:ignore_covered_err */ |
| 88 |
63 |
▶ | if (!(pfx && SvOK(pfx))) sv_setpvs(out, ""); |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(pfx && SvOK(pfx))) sv_setpvs(out, ""); |
63 |
0 |
63 |
| if (!(pfx && SvOK(pfx))) sv_setpvs(out, ""); |
0 |
63 |
63 |
|
| 89 |
63 |
▶ | if (!(pl == 1 && p[0] == '/')) sv_catpvn(out, p, pl); /* # uncoverable condition left count:2 class:ignore_covered_err */ |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(pl == 1 && p[0] == '/')) sv_catpvn(out, p, pl); /* # uncoverable condition left count:2 class:ignore_covered_err */ |
3 |
60 |
63 |
| if (!(pl == 1 && p[0] == '/')) sv_catpvn(out, p, pl); /* # uncoverable condition left count:2 class:ignore_covered_err */ |
0 |
3 |
3 |
|
| 90 |
63 |
▶ | if (!SvCUR(out)) sv_setpvs(out, "/"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvCUR(out)) sv_setpvs(out, "/"); |
1 |
62 |
63 |
|
| 91 |
63 |
| return sv_2mortal(out); |
| 92 |
|
| } |
| 93 |
|
| |
| 94 |
|
| /* Build a scope of the same class as $self (so a subclass nests as itself), |
| 95 |
|
| * through its own new - the constructor is the documented seam. */ |
| 96 |
5 |
| static SV *scope_new_like(pTHX_ SV *self, SV *app, SV *prefix, SV *guards) { |
| 97 |
|
| SV *argv[6]; |
| 98 |
5 |
| SV *class = sv_2mortal(newSVpv(sv_reftype(SvRV(self), 1), 0)); |
| 99 |
|
| SV *r; |
| 100 |
5 |
▶ | argv[0] = sv_2mortal(newSVpvs("app")); argv[1] = app ? app : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| argv[0] = sv_2mortal(newSVpvs("app")); argv[1] = app ? app : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
5 |
0 |
5 |
|
| 101 |
5 |
| argv[2] = sv_2mortal(newSVpvs("prefix")); argv[3] = prefix; |
| 102 |
5 |
| argv[4] = sv_2mortal(newSVpvs("guards")); argv[5] = guards; |
| 103 |
5 |
| r = pcx_call_meth(aTHX_ class, "new", argv, 6, 1); |
| 104 |
5 |
▶ | return r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| return r ? r : &PL_sv_undef; /* # uncoverable condition left count:1 class:ignore_covered_err */ |
5 |
0 |
5 |
|
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| /* $self->{app}->$meth(...), the one shape every registration here takes. */ |
| 108 |
66 |
| static void scope_app_call(pTHX_ HV *h, const char *meth, SV **argv, int nargs) { |
| 109 |
66 |
| SV *app = scope_get(aTHX_ h, "app"); |
| 110 |
66 |
▶ | SV *r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, meth, argv, nargs, 1); /* # uncoverable condition left count:1 class:ignore_covered_err */ |
Branch: 1 branch
| Branch | True | False | Total |
| SV *r = pcx_call_meth(aTHX_ app ? app : &PL_sv_undef, meth, argv, nargs, 1); /* # uncoverable condition left count:1 class:ignore_covered_err */ |
66 |
0 |
66 |
|
| 111 |
|
| /* |
| 112 |
|
| # uncoverable branch false |
| 113 |
|
| # uncoverable condition right count:1 class:ignore_covered_err */ |
| 114 |
66 |
▶ | if (r) SvREFCNT_dec(r); |
Branch: 1 branch
| Branch | True | False | Total |
| if (r) SvREFCNT_dec(r); |
66 |
0 |
66 |
|
| 115 |
66 |
| } |
| 116 |
|
| |
| 117 |
|
| #endif /* PUNK_SCOPE_H */ |