xs/views.xs

filter: Statementstmt 94.7% 108 / 114 Branchbran 70.7% 91 / 130 (1 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.9% 199 / 244 (1 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« include/punk/punk_metrics.h ↑ Summary include/punk/punk_stencil.h »
1 MODULE = Punk PACKAGE = Punk::Views
2  
3 PROTOTYPES: DISABLE
4  
5 # Punk::Views is entirely XS (punk_views.h): the object is a blessed IV-ref to
6 # the engine registry. lib/Punk/Views.pm is documentation only.
7  
8 # new(\@pairs): [ [ Name, \%opts ], ... ]. Resolve, load and instantiate each
9 # engine (the boot-time Perl calls); the first is the default.
10 SV *
11 new(class, pairs)
12 SV *class
13 SV *pairs
14 CODE:
15 {
16 pv_views *v;
17 AV *ps;
18 SSize_t i, n;
19 77 ▶ # uncoverable branch true
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch true 77 0 77
# uncoverable branch true 0 77 77
20 0 # uncoverable condition left count:1 class:ignore_covered_err
21 77 # uncoverable condition left count:2 class:ignore_covered_err
22 77 if (!SvROK(pairs) || SvTYPE(SvRV(pairs)) != SVt_PVAV)
23 77 ▶ croak("Punk::Views::new: pairs must be an arrayref"); /* # uncoverable statement */
Branch: 1 branch
BranchTrueFalseTotal
croak("Punk::Views::new: pairs must be an arrayref"); /* # uncoverable statement */ 0 77 77
24 0 ps = (AV *)SvRV(pairs);
25 77 n = av_len(ps) + 1;
26 152 ▶ # uncoverable branch true
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable branch true 80 72 152
27 80 # uncoverable condition left count:1 class:ignore_covered_err
28 if (n <= 0)
29 croak("Punk::Views: no view engine configured - add a views keyword"); /* # uncoverable statement */
30 80 ▶ v = pv_new(aTHX);
Branch: 4 branches
BranchTrueFalseTotal
v = pv_new(aTHX); 80 0 80
v = pv_new(aTHX); 80 0 80
v = pv_new(aTHX); 80 0 80
v = pv_new(aTHX); 0 80 80
31 0 for (i = 0; i < n; i++) {
32 0 SV **e = av_fetch(ps, i, 0);
33 AV *pair;
34 80 SV **nm, **op;
35 80 # uncoverable branch true
36 80 # uncoverable condition left count:1 class:ignore_covered_err
37 160 ▶ # uncoverable condition left count:2 class:ignore_covered_err
Branch: 3 branches
BranchTrueFalseTotal
# uncoverable condition left count:2 class:ignore_covered_err 80 0 80
# uncoverable condition left count:2 class:ignore_covered_err 80 0 80
# uncoverable condition left count:2 class:ignore_covered_err 80 0 80
38 80 ▶ # uncoverable condition left count:3 class:ignore_covered_err
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable condition left count:3 class:ignore_covered_err 80 0 80
39 # uncoverable condition left count:4 class:ignore_covered_err
40 144 if (!e || !*e || !SvROK(*e) || SvTYPE(SvRV(*e)) != SVt_PVAV) {
41 pv_free(aTHX_ v); /* # uncoverable statement */
42 croak("Punk::Views::new: pair %d is not an arrayref", (int)i); /* # uncoverable statement */
43 }
44 pair = (AV *)SvRV(*e);
45 nm = av_fetch(pair, 0, 0);
46 op = av_fetch(pair, 1, 0);
47 # uncoverable condition right count:1 class:ignore_covered_err
48 # uncoverable condition right count:2 class:ignore_covered_err
49 # uncoverable condition right count:3 class:ignore_covered_err
50 # uncoverable condition right count:4 class:ignore_covered_err
51 pv_load_engine(aTHX_ v, (nm && *nm) ? *nm : &PL_sv_undef,
52 (op && *op) ? *op : NULL);
53 }
54 RETVAL = sv_setref_iv(newSV(0), SvPV_nolen(class), PTR2IV(v));
55 }
56 OUTPUT:
57 RETVAL
58  
59 # The default engine's name.
60 SV *
61 # uncoverable statement
62 # uncoverable subroutine
63 default(self)
64 SV *self
65 CODE:
66 {
67 0 pv_views *v = pv_of(aTHX_ self); /* # uncoverable statement */
68 0 ▶ # uncoverable statement
Branch: 1 branch
BranchTrueFalseTotal
# uncoverable statement 0 0 0
69 # uncoverable condition left count:1
70 # uncoverable condition right count:1
71 RETVAL = v->deflt ? newSVsv(v->deflt) : newSV(0);
72 }
73 OUTPUT:
74 RETVAL
75  
76 # The named (or default) engine object; unknown names croak.
77 SV *
78 # uncoverable statement class:ignore_covered_err
79 # uncoverable subroutine class:ignore_covered_err
80 engine(self, ...)
81 SV *self
82 CODE:
83 1 ▶ RETVAL = newSVsv(pv_engine(aTHX_ pv_of(aTHX_ self), /* # uncoverable statement class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = newSVsv(pv_engine(aTHX_ pv_of(aTHX_ self), /* # uncoverable statement class:ignore_covered_err */ 1 0 1
84 items > 1 ? ST(1) : NULL));
85 OUTPUT:
86 RETVAL
87  
88 # render($c, $template, \%data, %over): call the engine's render and assemble
89 # the PSGI triplet in C. $over: status, type, engine, layout. Status and headers
90 # fold in from $c (a Punk::Context, or undef). The engine's returned bytes are
91 # copied into the body so Template::Stencil's reused render buffer is intact.
92 #
93 # `layout` reaches the engine as a third argument, { wrapper => $name | undef }
94 # - the per-render option Template::Stencil already takes, spelled with the
95 # framework's word on this side and the engine's on that. It is passed only
96 # when given, so an engine written to the two-argument contract sees nothing
97 # new until a caller asks for a layout - and then ignores it, which the
98 # contract in Punk::Views says in so many words.
99 #
100 # An override this does not know croaks. It used to be skipped, which meant
101 # `layout => undef` rendered WITH the layout and told nobody - the option that
102 # looks like it worked is worse than the one that failed.
103 void
104 render(self, c, template, data = &PL_sv_undef, ...)
105 SV *self
106 SV *c
107 SV *template
108 SV *data
109 PPCODE:
110 {
111 2176 pv_views *v = pv_of(aTHX_ self);
112 2176 SV *o_status = NULL, *o_type = NULL, *o_engine = NULL;
113 2176 SV *o_layout = NULL; int have_layout = 0;
114 SV *engine, *bytes, *body, *ct;
115 AV *headers, *bodyav, *resp;
116 2176 IV status = 0;
117 int i;
118 2176 ▶ int have_c = (c && SvOK(c)); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
int have_c = (c && SvOK(c)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2176 0 2176
int have_c = (c && SvOK(c)); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2175 1 2176
119  
120 2176 ▶ if (items > 4 && ((items - 4) & 1))
Branch: 2 branches
BranchTrueFalseTotal
if (items > 4 && ((items - 4) & 1)) 21 2155 2176
if (items > 4 && ((items - 4) & 1)) 1 20 21
121 1 croak("Punk::Views::render: overrides are name => value pairs "
122 "(status, type, engine, layout)");
123 2198 ▶ for (i = 4; i + 1 < items; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 4; i + 1 < items; i += 2) { 24 2174 2198
124 STRLEN kl;
125 24 const char *k = SvPV_const(ST(i), kl);
126 24 ▶ if (kl == 6 && memEQ(k, "status", 6)) o_status = ST(i + 1);
Branch: 2 branches
BranchTrueFalseTotal
if (kl == 6 && memEQ(k, "status", 6)) o_status = ST(i + 1); 23 1 24
if (kl == 6 && memEQ(k, "status", 6)) o_status = ST(i + 1); 2 21 23
127 22 ▶ else if (kl == 4 && memEQ(k, "type", 4)) o_type = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 4 && memEQ(k, "type", 4)) o_type = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 21 22
else if (kl == 4 && memEQ(k, "type", 4)) o_type = ST(i + 1); /* # uncoverable condition right count:2 class:ignore_covered_err */ 1 0 1
128 21 ▶ else if (kl == 6 && memEQ(k, "engine", 6)) o_engine = ST(i + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 6 && memEQ(k, "engine", 6)) o_engine = ST(i + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 21 0 21
else if (kl == 6 && memEQ(k, "engine", 6)) o_engine = ST(i + 1); /* # uncoverable condition right count:1 class:ignore_covered_err */ 5 16 21
129 16 ▶ else if (kl == 6 && memEQ(k, "layout", 6)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
else if (kl == 6 && memEQ(k, "layout", 6)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 16 0 16
else if (kl == 6 && memEQ(k, "layout", 6)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 15 1 16
130 15 o_layout = ST(i + 1); have_layout = 1;
131 }
132 else
133 1 croak("Punk::Views::render: unknown override '%.*s' "
134 "(known: status, type, engine, layout)", (int)kl, k);
135 }
136  
137 2174 engine = pv_engine(aTHX_ v, o_engine);
138  
139 /* engine->render($template, $data || {} [, { wrapper => $layout }]) */
140 {
141 2173 dSP;
142 int count;
143 2173 ▶ SV *d = SvOK(data) ? data : sv_2mortal(newRV_noinc((SV *)newHV()));
Branch: 1 branch
BranchTrueFalseTotal
SV *d = SvOK(data) ? data : sv_2mortal(newRV_noinc((SV *)newHV())); 2112 61 2173
144 2173 SV *eo = NULL;
145 2173 ▶ if (have_layout) {
Branch: 1 branch
BranchTrueFalseTotal
if (have_layout) { 15 2158 2173
146 15 HV *oh = newHV();
147 15 ▶ (void)hv_stores(oh, "wrapper",
Branch: 2 branches
BranchTrueFalseTotal
(void)hv_stores(oh, "wrapper", 15 0 15
(void)hv_stores(oh, "wrapper", 4 11 15
148 (o_layout && SvOK(o_layout)) ? newSVsv(o_layout)
149 : newSV(0));
150 15 eo = sv_2mortal(newRV_noinc((SV *)oh));
151 }
152 /* Punk::Plugin::CSP: this request's nonce, so `{% csp_nonce %}`
153 * resolves without the handler passing anything. A no-op unless
154 * the plugin is registered for this app. */
155 2173 ▶ if (have_c) pcsp_bind_vars(aTHX_ c, d);
Branch: 1 branch
BranchTrueFalseTotal
if (have_c) pcsp_bind_vars(aTHX_ c, d); 2172 1 2173
156 /* Punk::Plugin::I18n: this request's catalogue, so
157 * `{% locale.welcome %}` resolves without the handler passing
158 * anything. A no-op unless the plugin is registered. */
159 2173 ▶ if (have_c) pi_bind_vars(aTHX_ c, d);
Branch: 1 branch
BranchTrueFalseTotal
if (have_c) pi_bind_vars(aTHX_ c, d); 2172 1 2173
160 2173 ENTER; SAVETMPS;
161 /* Named routes: this request's `url` hash, so `{% url.books %}`
162 * resolves without the handler passing anything, and the prefix
163 * the url_for filter reads for the length of this render. Inside
164 * the ENTER because it save_items that prefix - punk_url.h says
165 * why putting it back matters more than setting it. */
166 2173 ▶ if (have_c) pk_url_bind_vars(aTHX_ c, d);
Branch: 1 branch
BranchTrueFalseTotal
if (have_c) pk_url_bind_vars(aTHX_ c, d); 2172 1 2173
167 /* before_render: a plugin's chance to put something into the data
168 * every template gets, after Punk's own binds so an application
169 * that wants the last word still has it by passing the key. The
170 * chain is absent unless somebody registered one, so the ordinary
171 * render pays one hv_fetch that misses. */
172 2173 ▶ if (have_c) pv_before_render(aTHX_ c, template, d);
Branch: 1 branch
BranchTrueFalseTotal
if (have_c) pv_before_render(aTHX_ c, template, d); 2172 1 2173
173 /* A hook is arbitrary Perl, and every call it makes moves the
174 * real stack - so the SP this scope has held since the top of
175 * the function is stale. Pushing the engine call onto it writes
176 * over whatever is there now, which surfaces as "Out of memory
177 * during stack extend" somewhere else entirely. */
178 2173 SPAGAIN;
179 2173 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2173 2173
180 2173 ▶ EXTEND(SP, 4);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 4); 0 2173 2173
181 2173 PUSHs(engine);
182 2173 PUSHs(template);
183 2173 PUSHs(d);
184 2173 ▶ if (eo) PUSHs(eo);
Branch: 1 branch
BranchTrueFalseTotal
if (eo) PUSHs(eo); 15 2158 2173
185 2173 PUTBACK;
186 2173 count = call_method("render", G_SCALAR);
187 2167 SPAGAIN;
188 2167 ▶ bytes = count > 0 ? SvREFCNT_inc(POPs) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
bytes = count > 0 ? SvREFCNT_inc(POPs) : newSV(0); /* # uncoverable condition right count:1 class:ignore_covered_err */ 2167 0 2167
189 2167 PUTBACK;
190 2167 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2167 0 2167
191 }
192  
193 /* Punk::Plugin::CSP's inline-handler check. Development only, and it
194 * never touches `bytes` - a checker with authority over the response
195 * is a checker that gets removed the first time it breaks a page. */
196 2167 ▶ if (have_c) pcsp_scan(aTHX_ c, template, bytes);
Branch: 1 branch
BranchTrueFalseTotal
if (have_c) pcsp_scan(aTHX_ c, template, bytes); 2166 1 2167
197  
198 /* status: override, else the context's pending status, else 200 */
199 2167 ▶ if (o_status && SvOK(o_status)) status = SvIV(o_status); /* # uncoverable condition right count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (o_status && SvOK(o_status)) status = SvIV(o_status); /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 2165 2167
if (o_status && SvOK(o_status)) status = SvIV(o_status); /* # uncoverable condition right count:2 class:ignore_covered_err */ 2 0 2
200 2165 ▶ else if (have_c) {
Branch: 1 branch
BranchTrueFalseTotal
else if (have_c) { 2164 1 2165
201 2164 dSP;
202 int count;
203 2164 ENTER; SAVETMPS;
204 2164 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 2164 2164
205 2164 ▶ XPUSHs(c);
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(c); 0 2164 2164
206 2164 PUTBACK;
207 2164 count = call_method("_status", G_SCALAR);
208 2164 SPAGAIN;
209 2164 ▶ # uncoverable branch false class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false class:ignore_covered_err 2164 0 2164
# uncoverable branch false class:ignore_covered_err 1 2163 2164
210 2164 # uncoverable condition right count:1 class:ignore_covered_err
211 2164 ▶ if (count > 0) { SV *s = POPs; if (SvOK(s)) status = SvIV(s); }
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { SV *s = POPs; if (SvOK(s)) status = SvIV(s); } 2164 0 2164
212 PUTBACK;
213 2167 ▶ FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; LEAVE; 2164 3 2167
214 }
215 1 ▶ if (!status) status = 200;
Branch: 1 branch
BranchTrueFalseTotal
if (!status) status = 200; 1 0 1
216 2167 ▶ 
Branch: 1 branch
BranchTrueFalseTotal
1 2166 2167
217 ct = (o_type && SvOK(o_type)) ? newSVsv(o_type)
218 2167 : newSVpvs("text/html; charset=utf-8");
219 2167  
220 2167 headers = newAV();
221 2167 ▶ av_push(headers, newSVpvs("Content-Type"));
Branch: 1 branch
BranchTrueFalseTotal
av_push(headers, newSVpvs("Content-Type")); 2166 1 2167
222 2166 av_push(headers, ct);
223 if (have_c) { /* fold in $c->_headers pairs */
224 2166 dSP;
225 2166 ▶ int count;
Branch: 1 branch
BranchTrueFalseTotal
int count; 0 2166 2166
226 2166 ▶ ENTER; SAVETMPS;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; 0 2166 2166
227 2166 PUSHMARK(SP);
228 2166 XPUSHs(c);
229 2166 PUTBACK;
230 2166 ▶ count = call_method("_headers", G_SCALAR);
Branch: 1 branch
BranchTrueFalseTotal
count = call_method("_headers", G_SCALAR); 2166 0 2166
231 2166 SPAGAIN;
232 2166 ▶ # uncoverable branch false
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable branch false 2166 0 2166
# uncoverable branch false 2166 0 2166
233 2166 # uncoverable condition right count:1 class:ignore_covered_err
234 2166 if (count > 0) {
235 2172 ▶ SV *hr = POPs;
Branch: 1 branch
BranchTrueFalseTotal
SV *hr = POPs; 6 2166 2172
236 6 # uncoverable branch false
237 6 ▶ # uncoverable condition right count:1 class:ignore_covered_err
Branch: 2 branches
BranchTrueFalseTotal
# uncoverable condition right count:1 class:ignore_covered_err 6 0 6
# uncoverable condition right count:1 class:ignore_covered_err 6 0 6
238 # uncoverable condition right count:2 class:ignore_covered_err
239 if (SvROK(hr) && SvTYPE(SvRV(hr)) == SVt_PVAV) {
240 AV *ch = (AV *)SvRV(hr);
241 2166 SSize_t j, hn = av_len(ch) + 1;
242 2166 ▶ for (j = 0; j < hn; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; j < hn; j++) { 2166 0 2166
243 SV **hp = av_fetch(ch, j, 0);
244 # uncoverable condition right count:1 class:ignore_covered_err
245 # uncoverable condition right count:2 class:ignore_covered_err
246 2167 av_push(headers, hp && *hp ? newSVsv(*hp) : newSV(0));
247 2167 }
248 2167 }
249 2167 }
250 PUTBACK;
251 2167 FREETMPS; LEAVE;
252 2167 }
253 2167  
254 2167 /* copy the engine's bytes so its reused buffer is not shared */
255 2167 body = newSVsv(bytes);
256 2167 SvREFCNT_dec(bytes);
257 2167 av_push(headers, newSVpvs("Content-Length"));
258 av_push(headers, newSViv((IV)SvCUR(body)));
259  
260 bodyav = newAV();
261 av_push(bodyav, body);
262 resp = newAV();
263 av_extend(resp, 2);
264 av_push(resp, newSViv(status));
265 av_push(resp, newRV_noinc((SV *)headers));
266 av_push(resp, newRV_noinc((SV *)bodyav));
267  
268 /* SPAGAIN before the push, and it is not ceremony.
269 *
270 * Everything between here and the top of this XSUB - the engine's
271 * render, _status, _headers, and any before_render hook - is Perl,
272 * and Perl that grows the value stack REALLOCATES it. `sp` has been
273 2167 * a C pointer into the old buffer since the PPCODE prologue, so
274 2167 ▶ * pushing on it writes into freed memory and the caller gets an
Branch: 1 branch
BranchTrueFalseTotal
* pushing on it writes into freed memory and the caller gets an 0 2167 2167
275 * empty list back: a handler returning nothing, which the dispatcher
276 * serialises as `null` with a 200.
277 *
278 * It only bites when something in the middle grows the stack far
279 * enough to realloc - a first render that opens a database will,
280 * a second one will not - which is why it looks intermittent and is
281 * not. */
282 SPAGAIN;
283 mXPUSHs(newRV_noinc((SV *)resp));
284 }
285  
286 void
287 DESTROY(self)
288 SV *self
289 CODE:
290 {
291 73 ▶ if (SvROK(self) && SvIOK(SvRV(self))) /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (SvROK(self) && SvIOK(SvRV(self))) /* # uncoverable condition right count:1 class:ignore_covered_err */ 73 0 73
if (SvROK(self) && SvIOK(SvRV(self))) /* # uncoverable condition right count:1 class:ignore_covered_err */ 72 1 73
292 72 pv_free(aTHX_ pv_of(aTHX_ self));
293 }
« include/punk/punk_metrics.h ↑ Summary include/punk/punk_stencil.h »