include/punk/punk_app.h

filter: Statementstmt 95.0% 154 / 163 (1 uncoverable) Branchbran 66.6% 136 / 204 (14 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 79.2% 290 / 367 (15 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/txn.xs ↑ Summary include/punk/punk_context.h »
1 /* punk_app.h - the Punk::App registrar, in C.
2 *
3 * The application object is a blessed hash (the DSL keywords and plugins push
4 * into its slots at boot); new and the registrar methods - route, under, api,
5 * docs, static, mount, websocket, views, database, model_class, hook,
6 * middleware, on_error, helper, plugin - are XSUBs (xs/app.xs). The boot-time
7 * orchestration that drives the other XS subsystems (compile, the config
8 * loader, target/context resolution) stays Perl in Punk::App, since it is a
9 * sequence of require / subsystem ->compile calls.
10 *
11 * Must be included after punk_context.h (for pcx_call_meth).
12 */
13  
14 #ifndef PUNK_APP_H
15 #define PUNK_APP_H
16  
17 135654 static HV *app_hv(pTHX_ SV *self) {
18 /*
19 # uncoverable branch true
20 # uncoverable condition left count:1 class:ignore_covered_err
21 # uncoverable condition left count:2 class:ignore_covered_err */
22 135654 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 135654 0 135654
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVHV) 0 135654 135654
23 0 croak("Punk::App: not an application object"); /* # uncoverable statement */
24 135654 return (HV *)SvRV(self);
25 }
26  
27 300011 static SV *app_get(pTHX_ HV *h, const char *k) {
28 300011 SV **e = hv_fetch(h, k, (I32)strlen(k), 0);
29 300011 ▶ return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 292727 7284 300011
return (e && *e) ? *e : NULL; /* # uncoverable condition right count:2 class:ignore_covered_err */ 292727 0 292727
30 }
31  
32 /* the AV in slot k, creating an empty one if absent */
33 5146 static AV *app_av(pTHX_ HV *h, const char *k) {
34 5146 SV *v = app_get(aTHX_ h, k);
35 /*
36 # uncoverable condition right count:2 class:ignore_covered_err
37 # uncoverable condition right count:3 class:ignore_covered_err */
38 5146 ▶ if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v);
Branch: 3 branches
BranchTrueFalseTotal
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); 3928 1218 5146
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); 3928 0 3928
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVAV) return (AV *)SvRV(v); 3928 0 3928
39 {
40 1218 AV *a = newAV();
41 1218 (void)hv_store(h, k, (I32)strlen(k), newRV_noinc((SV *)a), 0);
42 1218 return a;
43 }
44 }
45  
46 /* the HV in slot k, creating an empty one if absent */
47 2424 static HV *app_hash(pTHX_ HV *h, const char *k) {
48 2424 SV *v = app_get(aTHX_ h, k);
49 /*
50 # uncoverable condition right count:2 class:ignore_covered_err
51 # uncoverable condition right count:3 class:ignore_covered_err */
52 2424 ▶ if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v);
Branch: 3 branches
BranchTrueFalseTotal
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); 2295 129 2424
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); 2295 0 2295
if (v && SvROK(v) && SvTYPE(SvRV(v)) == SVt_PVHV) return (HV *)SvRV(v); 2295 0 2295
53 {
54 129 HV *n = newHV();
55 129 (void)hv_store(h, k, (I32)strlen(k), newRV_noinc((SV *)n), 0);
56 129 return n;
57 }
58 }
59  
60 /* A deep copy of plain data: unblessed hash and array references are copied
61 * through, everything else (scalars, blessed objects, coderefs) by newSVsv.
62 * For the read-back accessors - a plugin must not be able to edit the
63 * application's configuration through a nested hash the shallow copy shared. */
64 76 static SV *app_deep_copy(pTHX_ SV *sv) {
65 /*
66 # uncoverable condition right count:1 class:ignore_covered_err
67 # uncoverable condition right count:3 class:ignore_covered_err */
68 76 ▶ if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) {
Branch: 3 branches
BranchTrueFalseTotal
if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { 76 0 76
if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { 29 47 76
if (sv && SvROK(sv) && !SvOBJECT(SvRV(sv))) { 29 0 29
69 29 SV *t = SvRV(sv);
70 29 ▶ if (SvTYPE(t) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(t) == SVt_PVHV) { 23 6 29
71 23 HV *src = (HV *)t, *dst = newHV();
72 HE *e;
73 23 hv_iterinit(src);
74 81 ▶ while ((e = hv_iternext(src)))
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(src))) 58 23 81
75 58 (void)hv_store_ent(dst, hv_iterkeysv(e),
76 app_deep_copy(aTHX_ hv_iterval(src, e)), 0);
77 23 return newRV_noinc((SV *)dst);
78 }
79 6 ▶ if (SvTYPE(t) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(t) == SVt_PVAV) { 3 3 6
80 3 AV *src = (AV *)t, *dst = newAV();
81 3 SSize_t i, n = av_len(src) + 1;
82 12 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 9 3 12
83 9 SV **x = av_fetch(src, i, 0);
84 /*
85 # uncoverable condition right count:1 class:ignore_covered_err
86 # uncoverable condition right count:2 class:ignore_covered_err */
87 9 ▶ av_push(dst, app_deep_copy(aTHX_ (x && *x) ? *x : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(dst, app_deep_copy(aTHX_ (x && *x) ? *x : &PL_sv_undef)); 9 0 9
av_push(dst, app_deep_copy(aTHX_ (x && *x) ? *x : &PL_sv_undef)); 9 0 9
88 }
89 3 return newRV_noinc((SV *)dst);
90 }
91 }
92 50 ▶ return newSVsv(sv ? sv : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
return newSVsv(sv ? sv : &PL_sv_undef); /* # uncoverable condition left count:1 class:ignore_covered_err */ 50 0 50
93 }
94  
95 /* strip a single trailing '/' from a fresh copy of an SV's string */
96 186 static SV *app_strip_slash(pTHX_ SV *sv) {
97 STRLEN l;
98 372 ▶ const char *p = SvOK(sv) ? SvPV_const(sv, l) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
const char *p = SvOK(sv) ? SvPV_const(sv, l) : ""; /* # uncoverable condition right count:1 class:ignore_covered_err */ 186 0 186
99 /*
100 # uncoverable branch true
101 # uncoverable condition left count:1 class:ignore_covered_err */
102 186 ▶ if (!SvOK(sv)) l = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvOK(sv)) l = 0; 0 186 186
103 186 ▶ if (l && p[l - 1] == '/') l--; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (l && p[l - 1] == '/') l--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 186 0 186
if (l && p[l - 1] == '/') l--; /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 171 186
104 186 return newSVpvn(p, l);
105 }
106  
107 /* ---- config-shape helpers (for _apply_config) ------------------------------
108 * These mirror Punk::App's _pairs / _list: turning the loose YAML shapes a
109 * config block may take into a uniform list to drive the registrar with. */
110  
111 /* a mapping (name => opts) or a list of names / [name,opts] / {name=>opts}
112 * into a mortal AV of [name, opts] pair-refs (opts defaulting to {}). */
113 24 static AV *app_pairs(pTHX_ SV *node, const char *what) {
114 24 AV *out = (AV *)sv_2mortal((SV *)newAV());
115 46 ▶ if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { 23 1 24
if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVHV) { 22 1 23
116 22 HV *h = (HV *)SvRV(node); HE *e;
117 22 hv_iterinit(h);
118 44 ▶ while ((e = hv_iternext(h))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(h))) { 22 22 44
119 22 SV *v = hv_iterval(h, e);
120 22 AV *pair = newAV();
121 22 av_push(pair, newSVsv(hv_iterkeysv(e)));
122 /*
123 # uncoverable condition right count:1 class:ignore_covered_err
124 # uncoverable condition right count:2 class:ignore_covered_err */
125 22 ▶ av_push(pair, (v && SvOK(v)) ? newSVsv(v)
Branch: 2 branches
BranchTrueFalseTotal
av_push(pair, (v && SvOK(v)) ? newSVsv(v) 22 0 22
av_push(pair, (v && SvOK(v)) ? newSVsv(v) 22 0 22
126 : newRV_noinc((SV *)newHV()));
127 22 av_push(out, newRV_noinc((SV *)pair));
128 }
129 }
130 3 ▶ else if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
else if (SvROK(node) && SvTYPE(SvRV(node)) == SVt_PVAV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
131 1 AV *a = (AV *)SvRV(node); SSize_t i, n = av_len(a) + 1;
132 4 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 1 4
133 3 SV **ep = av_fetch(a, i, 0);
134 /*
135 # uncoverable condition right count:1 class:ignore_covered_err
136 # uncoverable condition right count:2 class:ignore_covered_err */
137 3 ▶ SV *el = (ep && *ep) ? *ep : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
SV *el = (ep && *ep) ? *ep : &PL_sv_undef; 3 0 3
SV *el = (ep && *ep) ? *ep : &PL_sv_undef; 3 0 3
138 3 AV *pair = newAV();
139 4 ▶ if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVAV) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVAV) { 2 1 3
if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVAV) { 1 1 2
140 1 AV *ea = (AV *)SvRV(el);
141 1 SV **k = av_fetch(ea, 0, 0), **v = av_fetch(ea, 1, 0);
142 /*
143 # uncoverable condition right count:1 class:ignore_covered_err
144 # uncoverable condition right count:2 class:ignore_covered_err */
145 1 ▶ av_push(pair, newSVsv((k && *k) ? *k : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(pair, newSVsv((k && *k) ? *k : &PL_sv_undef)); 1 0 1
av_push(pair, newSVsv((k && *k) ? *k : &PL_sv_undef)); 1 0 1
146 /*
147 # uncoverable condition right count:1 class:ignore_covered_err
148 # uncoverable condition right count:2 class:ignore_covered_err
149 # uncoverable condition right count:3 class:ignore_covered_err */
150 1 ▶ av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v)
Branch: 3 branches
BranchTrueFalseTotal
av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) 1 0 1
av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) 1 0 1
av_push(pair, (v && *v && SvOK(*v)) ? newSVsv(*v) 1 0 1
151 : newRV_noinc((SV *)newHV()));
152 }
153 3 ▶ else if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 1 2
else if (SvROK(el) && SvTYPE(SvRV(el)) == SVt_PVHV) { /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
154 1 HV *eh = (HV *)SvRV(el); HE *he;
155 1 hv_iterinit(eh);
156 /*
157 # uncoverable branch false
158 # uncoverable condition right count:1 class:ignore_covered_err */
159 1 ▶ if ((he = hv_iternext(eh))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((he = hv_iternext(eh))) { 1 0 1
160 1 SV *v = hv_iterval(eh, he);
161 1 av_push(pair, newSVsv(hv_iterkeysv(he)));
162 /*
163 # uncoverable condition right count:1 class:ignore_covered_err
164 # uncoverable condition right count:2 class:ignore_covered_err */
165 1 ▶ av_push(pair, (v && SvOK(v)) ? newSVsv(v)
Branch: 2 branches
BranchTrueFalseTotal
av_push(pair, (v && SvOK(v)) ? newSVsv(v) 1 0 1
av_push(pair, (v && SvOK(v)) ? newSVsv(v) 1 0 1
166 : newRV_noinc((SV *)newHV()));
167 }
168 }
169 else {
170 1 av_push(pair, newSVsv(el));
171 1 av_push(pair, newRV_noinc((SV *)newHV()));
172 }
173 3 av_push(out, newRV_noinc((SV *)pair));
174 }
175 }
176 else {
177 1 croak("Punk: the config %s block must be a mapping or a list", what);
178 }
179 23 return out;
180 }
181  
182 /* a scalar / arrayref / hashref (keys) into a mortal AV of names */
183 6 static AV *app_list(pTHX_ SV *node, const char *what) {
184 6 AV *out = (AV *)sv_2mortal((SV *)newAV());
185 6 ▶ if (!SvROK(node)) { av_push(out, newSVsv(node)); return out; }
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(node)) { av_push(out, newSVsv(node)); return out; } 1 5 6
186 5 ▶ if (SvTYPE(SvRV(node)) == SVt_PVAV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(node)) == SVt_PVAV) { 3 2 5
187 3 AV *a = (AV *)SvRV(node); SSize_t i, n = av_len(a) + 1;
188 6 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 3 3 6
189 3 SV **e = av_fetch(a, i, 0);
190 /*
191 # uncoverable condition right count:1 class:ignore_covered_err
192 # uncoverable condition right count:2 class:ignore_covered_err */
193 3 ▶ av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef));
Branch: 2 branches
BranchTrueFalseTotal
av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); 3 0 3
av_push(out, newSVsv((e && *e) ? *e : &PL_sv_undef)); 3 0 3
194 }
195 3 return out;
196 }
197 2 ▶ if (SvTYPE(SvRV(node)) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvTYPE(SvRV(node)) == SVt_PVHV) { 1 1 2
198 1 HV *h = (HV *)SvRV(node); HE *e;
199 1 hv_iterinit(h);
200 2 ▶ while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e)));
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e))); 1 1 2
201 1 return out;
202 }
203 1 croak("Punk: the config %s block must be a list", what);
204 return out;
205 }
206  
207 /* the sorted (string) keys of an HV as a mortal AV of key SVs */
208 880 static AV *app_sorted_keys(pTHX_ HV *h) {
209 880 AV *out = (AV *)sv_2mortal((SV *)newAV());
210 HE *e;
211 880 hv_iterinit(h);
212 1276 ▶ while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e)));
Branch: 1 branch
BranchTrueFalseTotal
while ((e = hv_iternext(h))) av_push(out, newSVsv(hv_iterkeysv(e))); 396 880 1276
213 880 sortsv(AvARRAY(out), (size_t)(av_len(out) + 1), Perl_sv_cmp);
214 880 return out;
215 }
216  
217 /* call $self->$meth(@$args), discarding the (chainable) return */
218 /* Is a config value the bare "on" form? A YAML `true` does not arrive as
219 * the scalar 1: Punk::Config loads booleans as JSON::PP::Boolean objects,
220 * a blessed scalar ref whose truth is an overload. So a plain truthy
221 * scalar is on, a boolean object is whatever it says, and any other
222 * reference - a mapping, a list - is not this form at all. */
223 9 static int app_cfg_on(pTHX_ SV *sv) {
224 /*
225 # uncoverable branch true
226 # uncoverable condition left count:1 class:ignore_covered_err
227 # uncoverable condition left count:2 class:ignore_covered_err */
228 9 ▶ if (!sv || !SvOK(sv)) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return 0; 9 0 9
if (!sv || !SvOK(sv)) return 0; 0 9 9
229 9 ▶ if (!SvROK(sv)) return SvTRUE(sv) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(sv)) return SvTRUE(sv) ? 1 : 0; 3 6 9
230 /*
231 # uncoverable branch false
232 # uncoverable condition right count:1 class:ignore_covered_err
233 # uncoverable condition right count:2 class:ignore_covered_err */
234 6 ▶ if (SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) < SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) < SVt_PVAV) 6 0 6
if (SvOBJECT(SvRV(sv)) && SvTYPE(SvRV(sv)) < SVt_PVAV) 6 0 6
235 6 return SvTRUE(sv) ? 1 : 0;
236 0 return 0; /* # uncoverable statement */
237 }
238  
239 88 static void app_call_list(pTHX_ SV *self, const char *meth, AV *args) {
240 88 SSize_t n = av_len(args) + 1, i;
241 SV **argv, *r;
242 88 ▶ Newx(argv, n > 0 ? n : 1, SV *);
Branch: 3 branches
BranchTrueFalseTotal
Newx(argv, n > 0 ? n : 1, SV *); 78 10 88
Newx(argv, n > 0 ? n : 1, SV *); 0 78 78
Newx(argv, n > 0 ? n : 1, SV *); 78 10 88
243 245 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 157 88 245
244 157 SV **e = av_fetch(args, i, 0);
245 /*
246 # uncoverable condition right count:1 class:ignore_covered_err
247 # uncoverable condition right count:2 class:ignore_covered_err */
248 157 ▶ argv[i] = (e && *e) ? *e : &PL_sv_undef;
Branch: 2 branches
BranchTrueFalseTotal
argv[i] = (e && *e) ? *e : &PL_sv_undef; 157 0 157
argv[i] = (e && *e) ? *e : &PL_sv_undef; 157 0 157
249 }
250 88 r = pcx_call_meth(aTHX_ self, meth, argv, (int)n, 0);
251 /*
252 # uncoverable branch true
253 # uncoverable condition left count:1 class:ignore_covered_err */
254 88 ▶ if (r) SvREFCNT_dec(r);
Branch: 1 branch
BranchTrueFalseTotal
if (r) SvREFCNT_dec(r); 0 88 88
255 88 Safefree(argv);
256 88 }
257  
258 /* ---- the one-hashref route form -------------------------------------------
259 *
260 * post '/upload' => { cb => 'Web::File#create', max_body => 50_000_000 };
261 *
262 * instead of the positional
263 *
264 * post '/upload' => 'Web::File#create', { max_body => 50_000_000 };
265 *
266 * Both spellings are supported; this splits the first into the second at the
267 * front door, so the router, the compiled records and every consumer
268 * downstream see exactly what they have always seen.
269 *
270 * Purely additive: a hashref in the target slot was a boot croak before this
271 * existed ("must be a coderef or 'Controller#method'"), so no application can
272 * have been relying on the behaviour being replaced.
273 *
274 * `what` names the declaration for the croaks ("route POST", "websocket",
275 * "sse"); `path` is only ever read for those messages. Both out-params are
276 * left alone when `*target` is not a hashref.
277 */
278 61992 static void pk_spec_split(pTHX_ const char *what, SV *path,
279 SV **target, SV **opts) {
280 HV *spec, *o;
281 SV **cb;
282 HE *he;
283  
284 61992 ▶ if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 61992 0 61992
if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 61949 43 61992
if (!(*target && SvROK(*target) && SvTYPE(SvRV(*target)) == SVt_PVHV)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 61924 25 61949
285 61967 return;
286 25 spec = (HV *)SvRV(*target);
287  
288 /*
289 # uncoverable condition right count:1 class:ignore_covered_err
290 # uncoverable condition right count:2 class:ignore_covered_err */
291 25 ▶ if (opts && *opts && SvOK(*opts))
Branch: 3 branches
BranchTrueFalseTotal
if (opts && *opts && SvOK(*opts)) 25 0 25
if (opts && *opts && SvOK(*opts)) 25 0 25
if (opts && *opts && SvOK(*opts)) 1 24 25
292 2 ▶ croak("Punk: %s %s takes its options either inside the hashref or "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: %s %s takes its options either inside the hashref or " 1 0 1
293 "after the target, not both",
294 what, SvOK(path) ? SvPV_nolen(path) : "(no path)"); /* # uncoverable condition right count:1 class:ignore_covered_err */
295  
296 24 cb = hv_fetchs(spec, "cb", 0);
297 24 ▶ if (!(cb && *cb && SvOK(*cb))) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 3 branches
BranchTrueFalseTotal
if (!(cb && *cb && SvOK(*cb))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 22 2 24
if (!(cb && *cb && SvOK(*cb))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 22 0 22
if (!(cb && *cb && SvOK(*cb))) /* # uncoverable condition left count:2 class:ignore_covered_err */ 1 21 22
298 6 ▶ croak("Punk: %s %s has options but no `cb` - a route needs a handler "
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk: %s %s has options but no `cb` - a route needs a handler " 3 0 3
299 "(a coderef or 'Controller#method')",
300 what, SvOK(path) ? SvPV_nolen(path) : "(no path)"); /* # uncoverable condition right count:1 class:ignore_covered_err */
301  
302 /* Copy the remaining keys rather than deleting `cb` from the caller's
303 * hashref. A spec built once and declared twice
304 *
305 * my %spec = (cb => \&h, max_body => 10);
306 * get '/a' => \%spec; post '/b' => \%spec;
307 *
308 * must give two working routes; mutating it would leave the second
309 * without a handler, and would pass every test but that one. */
310 21 o = newHV();
311 21 hv_iterinit(spec);
312 54 ▶ while ((he = hv_iternext(spec))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(spec))) { 33 21 54
313 STRLEN kl;
314 33 ▶ const char *k = HePV(he, kl);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, kl); 0 33 33
315 33 ▶ if (kl == 2 && memEQ(k, "cb", 2)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 2 && memEQ(k, "cb", 2)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 21 12 33
if (kl == 2 && memEQ(k, "cb", 2)) continue; /* # uncoverable condition right count:2 class:ignore_covered_err */ 21 0 21
316 12 (void)hv_store(o, k, (I32)kl, newSVsv(hv_iterval(spec, he)), 0);
317 }
318  
319 /* the target LAST: it aliases into the spec we have just finished
320 * reading, and reassigning it first would be reading a freed hashref if
321 * the caller held no other reference */
322 21 *target = sv_2mortal(newSVsv(*cb));
323 /*
324 # uncoverable branch false
325 # uncoverable condition right count:1 class:ignore_covered_err */
326 21 ▶ if (opts) *opts = sv_2mortal(newRV_noinc((SV *)o));
Branch: 1 branch
BranchTrueFalseTotal
if (opts) *opts = sv_2mortal(newRV_noinc((SV *)o)); 21 0 21
327 0 else SvREFCNT_dec((SV *)o); /* # uncoverable statement */
328 }
329  
330 /* "METHOD PATH" -> the compiled record, as a mortal HV.
331 *
332 * A route option is recorded at declaration as { method, path, ... } and
333 * compiled at to_app onto the record the router built, which is the only
334 * place the guards AV lives. Every option that becomes a guard needs the
335 * same lookup, so it is written once; `what` names the option in the two
336 * croaks, which are both "the application declared this against a route
337 * that does not exist". */
338 14 static HV *pk_route_index(pTHX_ SV *self, const char *what) {
339 14 HV *h = (HV *)SvRV(self);
340 14 SV **rop = hv_fetchs(h, K_ROUTER, 0);
341 SV *recs_rv;
342 AV *recs;
343 HV *by;
344 SSize_t ri, rn;
345 /*
346 # uncoverable branch true
347 # uncoverable condition left count:1 class:ignore_covered_err
348 # uncoverable condition left count:2 class:ignore_covered_err
349 # uncoverable condition left count:3 class:ignore_covered_err */
350 14 ▶ if (!(rop && *rop && SvOK(*rop)))
Branch: 3 branches
BranchTrueFalseTotal
if (!(rop && *rop && SvOK(*rop))) 14 0 14
if (!(rop && *rop && SvOK(*rop))) 14 0 14
if (!(rop && *rop && SvOK(*rop))) 0 14 14
351 0 croak("Punk: %s routes with no router", what); /* # uncoverable statement */
352 14 recs_rv = pcx_call_meth(aTHX_ *rop, "records", NULL, 0, 1);
353 /*
354 # uncoverable branch true
355 # uncoverable condition left count:1 class:ignore_covered_err
356 # uncoverable condition left count:2 class:ignore_covered_err
357 # uncoverable condition left count:3 class:ignore_covered_err */
358 14 ▶ if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { 14 0 14
if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { 14 0 14
if (!(recs_rv && SvROK(recs_rv) && SvTYPE(SvRV(recs_rv)) == SVt_PVAV)) { 0 14 14
359 /*
360 # uncoverable statement
361 # uncoverable branch true
362 # uncoverable branch false
363 # uncoverable condition left count:1
364 # uncoverable condition right count:1 */
365 0 ▶ if (recs_rv) SvREFCNT_dec(recs_rv);
Branch: 1 branch
BranchTrueFalseTotal
if (recs_rv) SvREFCNT_dec(recs_rv); 0 0 0
366 0 croak("Punk: %s routes but no compiled records", what); /* # uncoverable statement */
367 }
368 14 sv_2mortal(recs_rv);
369 14 recs = (AV *)SvRV(recs_rv);
370 14 by = (HV *)sv_2mortal((SV *)newHV());
371 14 rn = av_len(recs) + 1;
372 57 ▶ for (ri = 0; ri < rn; ri++) {
Branch: 1 branch
BranchTrueFalseTotal
for (ri = 0; ri < rn; ri++) { 43 14 57
373 43 SV **rp = av_fetch(recs, ri, 0);
374 HV *rec;
375 SV **m, **p, *key;
376 /*
377 # uncoverable branch true
378 # uncoverable condition left count:1 class:ignore_covered_err
379 # uncoverable condition left count:2 class:ignore_covered_err
380 # uncoverable condition left count:3 class:ignore_covered_err
381 # uncoverable condition left count:4 class:ignore_covered_err */
382 43 ▶ if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 43 0 43
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 43 0 43
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 43 0 43
if (!(rp && *rp && SvROK(*rp) && SvTYPE(SvRV(*rp)) == SVt_PVHV)) 0 43 43
383 0 continue; /* # uncoverable statement */
384 43 rec = (HV *)SvRV(*rp);
385 43 m = hv_fetchs(rec, K_METHOD, 0);
386 43 p = hv_fetchs(rec, K_PATH, 0);
387 /*
388 # uncoverable branch true
389 # uncoverable condition left count:1 class:ignore_covered_err
390 # uncoverable condition left count:2 class:ignore_covered_err
391 # uncoverable condition left count:3 class:ignore_covered_err
392 # uncoverable condition left count:4 class:ignore_covered_err */
393 43 ▶ if (!(m && *m && p && *p)) continue;
Branch: 4 branches
BranchTrueFalseTotal
if (!(m && *m && p && *p)) continue; 43 0 43
if (!(m && *m && p && *p)) continue; 43 0 43
if (!(m && *m && p && *p)) continue; 43 0 43
if (!(m && *m && p && *p)) continue; 0 43 43
394 43 key = sv_2mortal(newSVsv(*m));
395 43 sv_catpvs(key, " ");
396 43 sv_catsv(key, *p);
397 43 (void)hv_store_ent(by, key, newSVsv(*rp), 0);
398 }
399 14 return by;
400 }
401  
402 /* Append a guard closure to a compiled record's guards, vivifying the AV.
403 * Takes the +1 the caller holds. */
404 18 static void pk_route_guard_push(pTHX_ HV *rec, SV *guard) {
405 18 SV **gp = hv_fetchs(rec, K_GUARDS, 0);
406 /*
407 # uncoverable branch true
408 # uncoverable condition left count:1 class:ignore_covered_err
409 # uncoverable condition left count:2 class:ignore_covered_err
410 # uncoverable condition left count:3 class:ignore_covered_err
411 # uncoverable condition left count:4 class:ignore_covered_err */
412 18 ▶ if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) {
Branch: 4 branches
BranchTrueFalseTotal
if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { 18 0 18
if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { 18 0 18
if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { 18 0 18
if (!(gp && *gp && SvROK(*gp) && SvTYPE(SvRV(*gp)) == SVt_PVAV)) { 0 18 18
413 0 (void)hv_stores(rec, K_GUARDS, newRV_noinc((SV *)newAV())); /* # uncoverable statement */
414 0 gp = hv_fetchs(rec, K_GUARDS, 0); /* # uncoverable statement */
415 }
416 18 av_push((AV *)SvRV(*gp), guard);
417 18 }
418  
419 #endif /* PUNK_APP_H */
« xs/txn.xs ↑ Summary include/punk/punk_context.h »