include/punk/punk_views.h

filter: Statementstmt 98.8% 88 / 89 Branchbran 66.3% 68 / 104 (9 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.3% 156 / 193 (9 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_accept.h ↑ Summary include/punk/punk_config.h »
1 /* punk_views.h - the C view-engine registry.
2 *
3 * Punk::Views is entirely XS: the object is a blessed IV-ref to this struct.
4 * new() resolves each `views Name => \%opts` to Punk::View::Name (or the
5 * class as written for '+Full::Class'), loads and instantiates it (the boot
6 * -time Perl calls), and stores the engine object keyed by name; the first is
7 * the default. render() - the hot path - looks the engine up, calls its
8 * render, and assembles the PSGI triplet in C, so a rendered page crosses the
9 * Perl boundary only for the engine's own render (which is itself XS in
10 * Template::Stencil). lib/Punk/Views.pm is documentation only.
11 *
12 * Perl headers and punk_response.h must be included before this file.
13 */
14  
15 #ifndef PUNK_VIEWS_H
16 #define PUNK_VIEWS_H
17  
18 typedef struct {
19 HV *engines; /* engine name -> the engine object SV */
20 SV *deflt; /* the default engine's name (the first registered) */
21 } pv_views;
22  
23 2249 static pv_views *pv_of(pTHX_ SV *self) {
24 2249 ▶ if (!SvROK(self) || !SvIOK(SvRV(self))) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || !SvIOK(SvRV(self))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 2249 0 2249
if (!SvROK(self) || !SvIOK(SvRV(self))) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 2248 2249
25 1 croak("Punk::Views: not a view registry");
26 2248 return (pv_views *)INT2PTR(void *, SvIV(SvRV(self)));
27 }
28  
29 77 static pv_views *pv_new(pTHX) {
30 pv_views *v;
31 77 Newxz(v, 1, pv_views);
32 77 v->engines = newHV();
33 77 v->deflt = NULL;
34 77 return v;
35 }
36  
37 72 static void pv_free(pTHX_ pv_views *v) {
38 /*
39 # uncoverable branch true
40 # uncoverable condition left count:1 class:ignore_covered_err */
41 72 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 0 72 72
42 /*
43 # uncoverable branch false
44 # uncoverable condition right count:1 class:ignore_covered_err */
45 72 ▶ if (v->engines) SvREFCNT_dec((SV *)v->engines);
Branch: 1 branch
BranchTrueFalseTotal
if (v->engines) SvREFCNT_dec((SV *)v->engines); 72 0 72
46 /*
47 # uncoverable branch false
48 # uncoverable condition right count:1 class:ignore_covered_err */
49 72 ▶ if (v->deflt) SvREFCNT_dec(v->deflt);
Branch: 1 branch
BranchTrueFalseTotal
if (v->deflt) SvREFCNT_dec(v->deflt); 72 0 72
50 72 Safefree(v);
51 }
52  
53 /* Resolve, load and instantiate one engine, storing it under its name; the
54 * first registered becomes the default. Mirrors the old Perl Punk::Views::new
55 * exactly, including its croaks. */
56 80 static void pv_load_engine(pTHX_ pv_views *v, SV *name_sv, SV *opts) {
57 STRLEN nl;
58 80 const char *name = SvPV_const(name_sv, nl);
59 80 ▶ int plus = (nl > 0 && name[0] == '+'); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int plus = (nl > 0 && name[0] == '+'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 80 0 80
int plus = (nl > 0 && name[0] == '+'); /* # uncoverable condition right count:1 class:ignore_covered_err */ 13 67 80
60 80 ▶ const char *key = plus ? name + 1 : name;
Branch: 1 branch
BranchTrueFalseTotal
const char *key = plus ? name + 1 : name; 13 67 80
61 80 ▶ STRLEN keyl = plus ? nl - 1 : nl;
Branch: 1 branch
BranchTrueFalseTotal
STRLEN keyl = plus ? nl - 1 : nl; 13 67 80
62 80 SV *classsv = plus ? sv_2mortal(newSVpvn(name + 1, nl - 1))
63 80 ▶ : sv_2mortal(newSVpvf("Punk::View::%.*s", (int)nl, name));
Branch: 1 branch
BranchTrueFalseTotal
: sv_2mortal(newSVpvf("Punk::View::%.*s", (int)nl, name)); 13 67 80
64 STRLEN cl;
65 80 const char *class = SvPV_const(classsv, cl);
66 80 HV *stash = gv_stashpvn(class, cl, 0);
67 SV *engine;
68  
69 80 ▶ if (!stash || !gv_fetchmethod_autoload(stash, "render", 0)) { /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!stash || !gv_fetchmethod_autoload(stash, "render", 0)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 77 3 80
if (!stash || !gv_fetchmethod_autoload(stash, "render", 0)) { /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 77 77
70 3 SV *req = pk_require_src(aTHX_ class);
71 2 eval_sv(req, G_SCALAR);
72 2 ▶ if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 2 0 2
if (SvTRUE(ERRSV)) /* # uncoverable condition right count:1 class:ignore_covered_err */ 1 1 2
73 2 ▶ croak("Punk::Views: engine '%.*s' (%s) failed to load: %s",
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Views: engine '%.*s' (%s) failed to load: %s", 1 0 1
74 (int)keyl, key, class, SvPV_nolen(ERRSV));
75 1 stash = gv_stashpvn(class, cl, 0);
76 }
77 78 ▶ if (!stash || !gv_fetchmethod_autoload(stash, "new", 0)) /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!stash || !gv_fetchmethod_autoload(stash, "new", 0)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 78 0 78
if (!stash || !gv_fetchmethod_autoload(stash, "new", 0)) /* # uncoverable condition left count:1 class:ignore_covered_err */ 1 77 78
78 1 croak("Punk::Views: engine '%.*s' (%s) does not implement new",
79 (int)keyl, key, class);
80 77 ▶ if (!gv_fetchmethod_autoload(stash, "render", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!gv_fetchmethod_autoload(stash, "render", 0)) 1 76 77
81 1 croak("Punk::Views: engine '%.*s' (%s) does not implement render",
82 (int)keyl, key, class);
83 76 ▶ if (hv_exists(v->engines, key, (I32)keyl))
Branch: 1 branch
BranchTrueFalseTotal
if (hv_exists(v->engines, key, (I32)keyl)) 1 75 76
84 1 croak("Punk::Views: engine '%.*s' registered twice", (int)keyl, key);
85  
86 {
87 75 dSP;
88 int count;
89 75 ENTER; SAVETMPS;
90 75 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 75 75
91 75 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 75 75
92 75 PUSHs(classsv);
93 /*
94 # uncoverable condition left count:1 class:ignore_covered_err
95 # uncoverable condition left count:2 class:ignore_covered_err */
96 75 ▶ PUSHs((opts && SvOK(opts)) ? opts
Branch: 2 branches
BranchTrueFalseTotal
PUSHs((opts && SvOK(opts)) ? opts 75 0 75
PUSHs((opts && SvOK(opts)) ? opts 0 75 75
97 : sv_2mortal(newRV_noinc((SV *)newHV())));
98 75 PUTBACK;
99 75 count = call_method("new", G_SCALAR);
100 75 SPAGAIN;
101 75 ▶ engine = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
engine = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 75 0 75
102 75 PUTBACK;
103 75 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 75 0 75
104 }
105 75 (void)hv_store(v->engines, key, (I32)keyl, engine, 0);
106 75 ▶ if (!v->deflt) v->deflt = newSVpvn(key, keyl);
Branch: 1 branch
BranchTrueFalseTotal
if (!v->deflt) v->deflt = newSVpvn(key, keyl); 73 2 75
107 75 }
108  
109 /* The named (or default) engine object; unknown names croak listing what is
110 * registered. */
111 2174 static SV *pv_engine(pTHX_ pv_views *v, SV *name_sv) {
112 const char *name;
113 STRLEN nl;
114 SV **e;
115 2179 ▶ if (name_sv && SvOK(name_sv)) name = SvPV_const(name_sv, nl); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (name_sv && SvOK(name_sv)) name = SvPV_const(name_sv, nl); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 2169 2174
if (name_sv && SvOK(name_sv)) name = SvPV_const(name_sv, nl); /* # uncoverable condition right count:2 class:ignore_covered_err */ 5 0 5
116 /*
117 # uncoverable branch false
118 # uncoverable condition right count:1 class:ignore_covered_err */
119 4338 ▶ else if (v->deflt) name = SvPV_const(v->deflt, nl);
Branch: 1 branch
BranchTrueFalseTotal
else if (v->deflt) name = SvPV_const(v->deflt, nl); 2169 0 2169
120 0 else croak("Punk::Views: no view engine configured - add a views keyword"); /* # uncoverable statement */
121 2174 e = hv_fetch(v->engines, name, (I32)nl, 0);
122 2174 ▶ if (e && *e) return *e; /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (e && *e) return *e; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2173 1 2174
if (e && *e) return *e; /* # uncoverable condition right count:2 class:ignore_covered_err */ 2173 0 2173
123 {
124 1 SV *have = sv_2mortal(newSVpvs(""));
125 HE *he;
126 1 int first = 1;
127 1 hv_iterinit(v->engines);
128 2 ▶ while ((he = hv_iternext(v->engines))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(v->engines))) { 1 1 2
129 /*
130 # uncoverable branch true
131 # uncoverable condition left count:1 class:ignore_covered_err */
132 1 ▶ if (!first) sv_catpvs(have, ", ");
Branch: 1 branch
BranchTrueFalseTotal
if (!first) sv_catpvs(have, ", "); 0 1 1
133 1 sv_catsv(have, hv_iterkeysv(he));
134 1 first = 0;
135 }
136 1 croak("Punk::Views: no view engine '%.*s' registered (have: %s)",
137 (int)nl, name, SvPV_nolen(have));
138 }
139 return NULL; /* not reached */
140 }
141  
142 /* ---- before_render ------------------------------------------------------- */
143  
144 /* Run the application's before_render chain over the data a template is about
145 * to be rendered with. Each hook gets ($c, $template, $data) and whatever it
146 * returns is dropped: the hook's business is the hashref, and a hook that
147 * could replace the response would be a second dispatcher.
148 *
149 * The chain hangs off the app rather than the request state because a render
150 * is reached through a context, which has no route to what the dispatcher is
151 * carrying. Absent unless one was registered, so the common render costs one
152 * hv_fetch that misses.
153 */
154 2172 static void pv_before_render(pTHX_ SV *c, SV *template, SV *data) {
155 SV *app, **slot;
156 AV *chain;
157 SSize_t i, n;
158  
159 /*
160 # uncoverable branch true
161 # uncoverable condition left count:1 class:ignore_covered_err
162 # uncoverable condition left count:2 class:ignore_covered_err */
163 2172 ▶ if (!(c && SvROK(c))) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!(c && SvROK(c))) return; 2172 0 2172
if (!(c && SvROK(c))) return; 0 2172 2172
164 /*
165 # uncoverable branch true
166 # uncoverable condition left count:1 class:ignore_covered_err
167 # uncoverable condition left count:2 class:ignore_covered_err
168 # uncoverable condition left count:3 class:ignore_covered_err */
169 2172 ▶ if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 2172 0 2172
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 2172 0 2172
if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; 0 2172 2172
170 2172 app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP);
171 /*
172 # uncoverable branch true
173 # uncoverable condition left count:1 class:ignore_covered_err
174 # uncoverable condition left count:2 class:ignore_covered_err
175 # uncoverable condition left count:3 class:ignore_covered_err */
176 2172 ▶ if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; 2172 0 2172
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; 2172 0 2172
if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; 0 2172 2172
177 2172 slot = hv_fetchs((HV *)SvRV(app), K_BEFORE_RENDER_C, 0);
178 /*
179 # uncoverable condition left count:2 class:ignore_covered_err
180 # uncoverable condition left count:3 class:ignore_covered_err
181 # uncoverable condition left count:4 class:ignore_covered_err */
182 2172 ▶ if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV))
Branch: 4 branches
BranchTrueFalseTotal
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 11 2161 2172
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 11 0 11
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 11 0 11
if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) 0 11 11
183 2161 return;
184  
185 11 chain = (AV *)SvRV(*slot);
186 11 n = av_len(chain) + 1;
187 24 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 13 11 24
188 13 SV **cv = av_fetch(chain, i, 0);
189 13 dSP;
190 /*
191 # uncoverable branch true
192 # uncoverable condition left count:1 class:ignore_covered_err
193 # uncoverable condition left count:2 class:ignore_covered_err
194 # uncoverable condition left count:3 class:ignore_covered_err */
195 13 ▶ if (!(cv && *cv && SvROK(*cv))) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (!(cv && *cv && SvROK(*cv))) continue; 13 0 13
if (!(cv && *cv && SvROK(*cv))) continue; 13 0 13
if (!(cv && *cv && SvROK(*cv))) continue; 0 13 13
196 13 ENTER; SAVETMPS;
197 13 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 13 13
198 13 ▶ EXTEND(SP, 3);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 3); 0 13 13
199 13 PUSHs(c);
200 13 PUSHs(template);
201 13 PUSHs(data);
202 13 PUTBACK;
203 13 call_sv(*cv, G_VOID | G_DISCARD);
204 13 SPAGAIN;
205 13 PUTBACK;
206 13 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 0 13 13
207 }
208 }
209  
210 #endif /* PUNK_VIEWS_H */
« include/punk/punk_accept.h ↑ Summary include/punk/punk_config.h »