include/punk/punk_stencil.h

filter: Statementstmt 95.3% 61 / 65 (1 uncoverable) Branchbran 70.0% 43 / 70 (8 uncoverable) Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.2% 104 / 135 (9 uncoverable) CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« xs/views.xs ↑ Summary include/punk/punk_oamount.h »
1 /* punk_stencil.h - Punk::View::Stencil, the shipped view engine, in C.
2 *
3 * The adapter over Template::Stencil is two array slots and no Perl: the
4 * options as registered, and the Template::Stencil object built from them.
5 * Rendering goes through Template::Stencil's C ABI (st_abi.h, reached through
6 * ExtUtils::Depends), so the path from the dispatcher to the template VM is
7 *
8 * punk_views.h -> this XSUB -> abi->render
9 *
10 * with no Perl frame anywhere in it. The engine object is built once, at boot,
11 * through Template::Stencil->new - the one Perl call, in the one place where a
12 * bad option should still fail with the rest of the configuration.
13 *
14 * Perl headers and punk_static.h (punk_closure is not used here, but the file
15 * ordering in Punk.xs puts this after it) must be in scope first.
16 */
17  
18 #ifndef PUNK_STENCIL_H
19 #define PUNK_STENCIL_H
20  
21 #include "st_abi.h"
22  
23 enum { PVS_OPTS = 0, PVS_ENGINE = 1 }; /* the object's slots */
24  
25 static const st_abi *PUNK_ST = NULL;
26 static int PUNK_ST_TRIED = 0;
27  
28 /* Resolve (once) Template::Stencil's ABI table, or NULL. PUNK_FAKE_ST_BAD
29 * simulates a version mismatch for the guard test. */
30 2922 static const st_abi *punk_st_try(pTHX) {
31 2922 ▶ if (!PUNK_ST_TRIED) {
Branch: 1 branch
BranchTrueFalseTotal
if (!PUNK_ST_TRIED) { 58 2864 2922
32 58 dSP; int count, ok; IV p = 0;
33 58 PUNK_ST_TRIED = 1;
34 58 ok = pk_require_once(aTHX_ "Template::Stencil", FALSE);
35 58 SPAGAIN; /* the require may have reallocated the value stack */
36 /*
37 # uncoverable branch false
38 # uncoverable condition right count:1 class:ignore_covered_err */
39 58 ▶ if (ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (ok) { 58 0 58
40 58 ▶ ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK;
Branch: 1 branch
BranchTrueFalseTotal
ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; 0 58 58
41 58 count = call_pv("Template::Stencil::_abi_ptr", G_SCALAR | G_EVAL);
42 58 SPAGAIN;
43 /*
44 # uncoverable branch false
45 # uncoverable condition right count:1 class:ignore_covered_err
46 # uncoverable condition right count:2 class:ignore_covered_err */
47 58 ▶ if (!SvTRUE(ERRSV) && count > 0) p = POPi;
Branch: 3 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 58 0 58
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 58 0 58
if (!SvTRUE(ERRSV) && count > 0) p = POPi; 58 0 58
48 /*
49 # uncoverable statement
50 # uncoverable branch true
51 # uncoverable branch false
52 # uncoverable condition left count:1
53 # uncoverable condition right count:1 */
54 0 ▶ else if (count > 0) (void)POPs;
Branch: 1 branch
BranchTrueFalseTotal
else if (count > 0) (void)POPs; 0 0 0
55 58 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 58 0 58
56 /*
57 # uncoverable branch false
58 # uncoverable condition right count:1 class:ignore_covered_err */
59 58 ▶ if (p) {
Branch: 1 branch
BranchTrueFalseTotal
if (p) { 58 0 58
60 58 const st_abi *a = INT2PTR(const st_abi *, p);
61 /*
62 # uncoverable condition right count:1 class:ignore_covered_err
63 # uncoverable condition right count:3 class:ignore_covered_err */
64 58 ▶ if (a && !getenv("PUNK_FAKE_ST_BAD")
Branch: 2 branches
BranchTrueFalseTotal
if (a && !getenv("PUNK_FAKE_ST_BAD") 58 0 58
if (a && !getenv("PUNK_FAKE_ST_BAD") 57 1 58
65 57 ▶ && a->abi_version >= ST_ABI_VERSION)
Branch: 1 branch
BranchTrueFalseTotal
&& a->abi_version >= ST_ABI_VERSION) 57 0 57
66 57 PUNK_ST = a;
67 }
68 }
69 }
70 2922 return PUNK_ST;
71 }
72  
73 /* The table, croaking if it is missing or too old. Template::Stencil 0.02+ is
74 * a hard prerequisite, so this is a boot-environment error rather than a
75 * per-request fallback - there is no Perl render path to fall back to. */
76 2915 static const st_abi *punk_st(pTHX) {
77 2915 const st_abi *a = punk_st_try(aTHX);
78 2915 ▶ if (!a)
Branch: 1 branch
BranchTrueFalseTotal
if (!a) 1 2914 2915
79 1 croak("Punk::View::Stencil: needs Template::Stencil with a compatible "
80 "C ABI (ST_ABI_VERSION %d); upgrade Template::Stencil to 0.02+",
81 ST_ABI_VERSION);
82 2914 return a;
83 }
84  
85 2177 static AV *pvs_av(pTHX_ SV *self) {
86 /*
87 # uncoverable branch true
88 # uncoverable condition left count:1 class:ignore_covered_err
89 # uncoverable condition left count:2 class:ignore_covered_err */
90 2177 ▶ if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 2177 0 2177
if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) 0 2177 2177
91 0 croak("Punk::View::Stencil: not a view object"); /* # uncoverable statement */
92 2177 return (AV *)SvRV(self);
93 }
94  
95 2175 static SV *pvs_slot(pTHX_ SV *self, I32 slot) {
96 2175 SV **e = av_fetch(pvs_av(aTHX_ self), slot, 0);
97 /*
98 # uncoverable condition right count:2 class:ignore_covered_err
99 # uncoverable condition right count:3 class:ignore_covered_err */
100 2175 ▶ return (e && *e && SvOK(*e)) ? *e : NULL;
Branch: 3 branches
BranchTrueFalseTotal
return (e && *e && SvOK(*e)) ? *e : NULL; 2174 1 2175
return (e && *e && SvOK(*e)) ? *e : NULL; 2174 0 2174
return (e && *e && SvOK(*e)) ? *e : NULL; 2174 0 2174
101 }
102  
103 /* Template::Stencil->new(\%opts) - the single Perl call, at boot. Building the
104 * engine here rather than on first render is the point: an option Stencil will
105 * not accept fails with the rest of the configuration, not on a request. */
106 68 static SV *pvs_build_engine(pTHX_ SV *opts) {
107 68 dSP;
108 SV *obj;
109 int count;
110 68 ENTER; SAVETMPS;
111 68 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 68 68
112 68 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 68 68
113 68 mPUSHs(newSVpvs("Template::Stencil"));
114 /*
115 # uncoverable condition left count:1 class:ignore_covered_err
116 # uncoverable condition left count:2 class:ignore_covered_err */
117 68 ▶ PUSHs(opts && SvOK(opts) ? opts
Branch: 2 branches
BranchTrueFalseTotal
PUSHs(opts && SvOK(opts) ? opts 68 0 68
PUSHs(opts && SvOK(opts) ? opts 0 68 68
118 : sv_2mortal(newRV_noinc((SV *)newHV())));
119 68 PUTBACK;
120 68 count = call_method("new", G_SCALAR);
121 67 SPAGAIN;
122 67 ▶ obj = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
obj = count > 0 ? newSVsv(POPs) : &PL_sv_undef; /* # uncoverable condition right count:1 class:ignore_covered_err */ 67 0 67
123 67 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 67 0 67
124 /*
125 # uncoverable branch true
126 # uncoverable condition left count:1 class:ignore_covered_err */
127 67 ▶ if (!SvROK(obj))
Branch: 1 branch
BranchTrueFalseTotal
if (!SvROK(obj)) 0 67 67
128 0 croak("Punk::View::Stencil: Template::Stencil->new returned no object"); /* # uncoverable statement */
129 67 return obj;
130 }
131  
132 /* One render, entirely in C. Returns the bytes SV (caller owns) or croaks with
133 * the template error, which is what the render method did and what Punk::Views
134 * turns into a 500 further up. */
135 2175 static SV *pvs_render(pTHX_ SV *self, SV *tmpl, SV *data, SV *opts) {
136 2175 const st_abi *abi = punk_st(aTHX);
137 2175 SV *engine_sv = pvs_slot(aTHX_ self, PVS_ENGINE);
138 void *engine;
139 2175 SV *err = NULL, *out;
140 2175 HV *dhv = NULL;
141  
142 2175 ▶ if (!engine_sv)
Branch: 1 branch
BranchTrueFalseTotal
if (!engine_sv) 1 2174 2175
143 1 croak("Punk::View::Stencil: this view has no engine");
144 /* engine_of on every render, not a cached pointer: an ithread clone drops
145 * its engine and rebuilds lazily, so a cached one would dangle. The lookup
146 * is a walk of the object's magic chain. */
147 2174 engine = abi->engine_of(aTHX_ engine_sv);
148 /*
149 # uncoverable branch true
150 # uncoverable condition left count:1 class:ignore_covered_err */
151 2174 ▶ if (!engine)
Branch: 1 branch
BranchTrueFalseTotal
if (!engine) 0 2174 2174
152 0 croak("Punk::View::Stencil: the engine is not a Template::Stencil " /* # uncoverable statement */
153 "object");
154  
155 2174 ▶ if (data && SvOK(data)) { /* # uncoverable condition right count:1 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (data && SvOK(data)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2174 0 2174
if (data && SvOK(data)) { /* # uncoverable condition right count:1 class:ignore_covered_err */ 2172 2 2174
156 2172 ▶ if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 2171 1 2172
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) /* # uncoverable condition left count:2 class:ignore_covered_err */ 0 2171 2171
157 1 croak("Punk::View::Stencil: render data must be a hashref");
158 2171 dhv = (HV *)SvRV(data);
159 }
160 4346 ▶ out = abi->render(aTHX_ engine, tmpl, dhv,
Branch: 1 branch
BranchTrueFalseTotal
out = abi->render(aTHX_ engine, tmpl, dhv, 2173 0 2173
161 2173 ▶ (opts && SvOK(opts)) ? opts : NULL, &err);
Branch: 1 branch
BranchTrueFalseTotal
(opts && SvOK(opts)) ? opts : NULL, &err); 14 2159 2173
162 2170 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 4 2166 2170
163 4 ▶ croak_sv(err ? err : sv_2mortal(newSVpvs( /* # uncoverable condition left count:1 class:ignore_covered_err */
Branch: 1 branch
BranchTrueFalseTotal
croak_sv(err ? err : sv_2mortal(newSVpvs( /* # uncoverable condition left count:1 class:ignore_covered_err */ 0 4 4
164 "Punk::View::Stencil: render failed")));
165 2166 return out;
166 }
167  
168 #endif /* PUNK_STENCIL_H */
« xs/views.xs ↑ Summary include/punk/punk_oamount.h »