xs/views.xs

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