Stencil.xs

filter: Statementstmt 94.8% 275 / 290 Branchbran 71.2% 225 / 316 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 82.5% 500 / 606 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_engine.c ↑ Summary src/stencil_render.c »
1 /* mg_findext is 5.14; ask ppport.h (included from stencil.h) to emit its
2 * back-compat implementation into this TU, the only one that needs it. */
3 #define NEED_mg_findext
4  
5 #include "stencil.h"
6  
7 /* ====================================================================
8 * Object lifecycle: a Template::Stencil object is a blessed hashref of
9 * normalised options; the C engine hangs off it via ext magic. The
10 * magic free callback releases the engine (no DESTROY needed), and
11 * under ithreads the dup callback nulls the pointer so a cloned object
12 * lazily rebuilds a fresh engine (with an empty cache) from its own
13 * cloned options on first use.
14 * ==================================================================== */
15  
16 static UV stencil_stat_checker = 0;
17  
18 264 static int stencil_mg_free(pTHX_ SV *sv, MAGIC *mg)
19 {
20 PERL_UNUSED_ARG(sv);
21 264 ▶ if (mg->mg_ptr) {
Branch: 1 branch
BranchTrueFalseTotal
if (mg->mg_ptr) { 264 0 264
22 264 stencil_engine_free(aTHX_ (stencil_engine *)mg->mg_ptr);
23 264 mg->mg_ptr = NULL;
24 }
25 264 return 0;
26 }
27  
28 #ifdef USE_ITHREADS
29 static int stencil_mg_dup(pTHX_ MAGIC *mg, CLONE_PARAMS *param)
30 {
31 PERL_UNUSED_ARG(param);
32 mg->mg_ptr = NULL; /* clone rebuilds its own engine lazily */
33 return 0;
34 }
35 #endif
36  
37 static MGVTBL stencil_obj_vtbl = {
38 NULL, NULL, NULL, NULL,
39 stencil_mg_free,
40 NULL,
41 #ifdef USE_ITHREADS
42 stencil_mg_dup,
43 #else
44 NULL,
45 #endif
46 NULL
47 };
48  
49 264 static stencil_engine *stencil_build_engine(pTHX_ HV *o)
50 {
51 SV **v;
52 264 const char *dir = NULL, *wrap = NULL;
53 264 uint32_t flags = 0;
54 264 double ttl = 1.0;
55 264 UV csize = 256;
56 264 HV *filters = NULL;
57 stencil_engine *e;
58  
59 264 ▶ if ((v = hv_fetchs(o, "template_dir", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "template_dir", 0)) && SvOK(*v)) 20 244 264
if ((v = hv_fetchs(o, "template_dir", 0)) && SvOK(*v)) 20 0 20
60 40 dir = SvPV_nolen(*v);
61 264 ▶ if ((v = hv_fetchs(o, "wrapper", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "wrapper", 0)) && SvOK(*v)) 9 255 264
if ((v = hv_fetchs(o, "wrapper", 0)) && SvOK(*v)) 9 0 9
62 18 wrap = SvPV_nolen(*v);
63 264 ▶ if ((v = hv_fetchs(o, "filters", 0)) && SvROK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "filters", 0)) && SvROK(*v)) 5 259 264
if ((v = hv_fetchs(o, "filters", 0)) && SvROK(*v)) 5 0 5
64 5 filters = (HV *)SvRV(*v);
65 264 ▶ if ((v = hv_fetchs(o, "strict", 0)) && SvTRUE(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "strict", 0)) && SvTRUE(*v)) 264 0 264
if ((v = hv_fetchs(o, "strict", 0)) && SvTRUE(*v)) 4 260 264
66 4 flags |= STENCIL_RF_STRICT;
67 264 ▶ if ((v = hv_fetchs(o, "sort_keys", 0)) && !SvTRUE(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "sort_keys", 0)) && !SvTRUE(*v)) 264 0 264
if ((v = hv_fetchs(o, "sort_keys", 0)) && !SvTRUE(*v)) 1 263 264
68 1 flags |= STENCIL_RF_NO_SORT_KEYS;
69 264 ▶ if ((v = hv_fetchs(o, "auto_escape", 0)) && !SvTRUE(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "auto_escape", 0)) && !SvTRUE(*v)) 264 0 264
if ((v = hv_fetchs(o, "auto_escape", 0)) && !SvTRUE(*v)) 2 262 264
70 2 flags |= STENCIL_RF_NO_ESCAPE;
71 264 ▶ if ((v = hv_fetchs(o, "chars", 0)) && SvTRUE(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "chars", 0)) && SvTRUE(*v)) 264 0 264
if ((v = hv_fetchs(o, "chars", 0)) && SvTRUE(*v)) 3 261 264
72 3 flags |= STENCIL_RF_CHARS;
73 264 ▶ if ((v = hv_fetchs(o, "pretty", 0)) && SvTRUE(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "pretty", 0)) && SvTRUE(*v)) 264 0 264
if ((v = hv_fetchs(o, "pretty", 0)) && SvTRUE(*v)) 5 259 264
74 5 flags |= STENCIL_RF_PRETTY;
75 264 ▶ if ((v = hv_fetchs(o, "cache", 0)) && !SvTRUE(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "cache", 0)) && !SvTRUE(*v)) 264 0 264
if ((v = hv_fetchs(o, "cache", 0)) && !SvTRUE(*v)) 0 264 264
76 0 flags |= STENCIL_EF_NO_CACHE;
77 264 ▶ if ((v = hv_fetchs(o, "stat_ttl", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "stat_ttl", 0)) && SvOK(*v)) 264 0 264
if ((v = hv_fetchs(o, "stat_ttl", 0)) && SvOK(*v)) 264 0 264
78 264 ttl = SvNV(*v);
79 264 ▶ if ((v = hv_fetchs(o, "cache_size", 0)) && SvOK(*v))
Branch: 2 branches
BranchTrueFalseTotal
if ((v = hv_fetchs(o, "cache_size", 0)) && SvOK(*v)) 264 0 264
if ((v = hv_fetchs(o, "cache_size", 0)) && SvOK(*v)) 264 0 264
80 264 csize = SvUV(*v);
81  
82 264 e = stencil_engine_new(aTHX_ dir, wrap, flags, ttl, (uint32_t)csize,
83 filters);
84 264 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 0 264 264
85 0 croak("Template::Stencil: engine allocation failed");
86 264 return e;
87 }
88  
89 1896 static stencil_engine *stencil_engine_of(pTHX_ SV *self)
90 {
91 SV *rv;
92 MAGIC *mg;
93 1896 ▶ if (!SvROK(self) || SvTYPE(rv = SvRV(self)) != SVt_PVHV
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(self) || SvTYPE(rv = SvRV(self)) != SVt_PVHV 1894 2 1896
if (!SvROK(self) || SvTYPE(rv = SvRV(self)) != SVt_PVHV 1894 0 1894
94 1894 ▶ || !sv_derived_from(self, "Template::Stencil"))
Branch: 1 branch
BranchTrueFalseTotal
|| !sv_derived_from(self, "Template::Stencil")) 0 1894 1894
95 2 croak("Template::Stencil::render: not a Template::Stencil "
96 "object");
97 1894 mg = mg_findext(rv, PERL_MAGIC_ext, &stencil_obj_vtbl);
98 1894 ▶ if (!mg)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg) 0 1894 1894
99 0 croak("Template::Stencil::render: object has no engine magic");
100 1894 ▶ if (!mg->mg_ptr)
Branch: 1 branch
BranchTrueFalseTotal
if (!mg->mg_ptr) 0 1894 1894
101 0 mg->mg_ptr = (char *)stencil_build_engine(aTHX_ (HV *)rv);
102 1894 return (stencil_engine *)mg->mg_ptr;
103 }
104  
105 1896 static SV *stencil_obj_render(pTHX_ SV *self, SV *tmpl, SV *data,
106 SV *opts)
107 {
108 1896 stencil_engine *e = stencil_engine_of(aTHX_ self);
109 HV *dhv;
110 1894 SV *err = NULL;
111 SV *out;
112  
113 1894 ▶ if (!data || !SvOK(data))
Branch: 2 branches
BranchTrueFalseTotal
if (!data || !SvOK(data)) 1892 2 1894
if (!data || !SvOK(data)) 1 1891 1892
114 3 dhv = (HV *)sv_2mortal((SV *)newHV());
115 1891 ▶ else if (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
else if (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 1890 1 1891
else if (SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 1889 1 1890
116 1889 dhv = (HV *)SvRV(data);
117 else
118 2 croak("Template::Stencil::render: data must be a hashref");
119 1892 out = stencil_engine_render(aTHX_ e, tmpl, dhv, opts, &err);
120 1892 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 283 1609 1892
121 283 croak_sv(err);
122 1609 return out;
123 }
124  
125 #ifdef STENCIL_HAVE_CALL_CHECKER
126 /* Fast path installed by the call checker for statically resolved
127 * calls: the entersub's op_ppaddr is swapped for this pp, which reads
128 * the args directly (the trailing stack item is the CV, skipped) and
129 * calls the render core without XS prologue overhead. */
130 9 static OP *stencil_pp_render(pTHX)
131 {
132 9 dSP;
133 9 dMARK;
134 9 SSize_t n = SP - MARK - 1; /* last item is the CV */
135 9 SV **args = MARK + 1;
136 SV *out;
137 9 stencil_stat_checker++;
138 9 ▶ if (n < 2 || n > 4)
Branch: 2 branches
BranchTrueFalseTotal
if (n < 2 || n > 4) 8 1 9
if (n < 2 || n > 4) 0 8 8
139 1 croak("Usage: $stencil->render($template, \\%%data, \\%%opts)");
140 8 ▶ out = stencil_obj_render(aTHX_ args[0], args[1],
Branch: 2 branches
BranchTrueFalseTotal
out = stencil_obj_render(aTHX_ args[0], args[1], 1 7 8
out = stencil_obj_render(aTHX_ args[0], args[1], 7 1 8
141 n >= 3 ? args[2] : NULL,
142 n >= 4 ? args[3] : NULL);
143 5 SP = MARK;
144 5 ▶ XPUSHs(sv_2mortal(out));
Branch: 1 branch
BranchTrueFalseTotal
XPUSHs(sv_2mortal(out)); 0 5 5
145 5 RETURN;
146 }
147  
148 9 static OP *stencil_render_checker(pTHX_ OP *entersubop, GV *namegv,
149 SV *ckobj)
150 {
151 PERL_UNUSED_ARG(namegv);
152 PERL_UNUSED_ARG(ckobj);
153 9 entersubop = ck_entersub_args_list(entersubop);
154 9 entersubop->op_ppaddr = stencil_pp_render;
155 9 return entersubop;
156 }
157 #endif /* STENCIL_HAVE_CALL_CHECKER */
158  
159 /* The shared C ABI, after the object magic and the engine builder it wraps. */
160 #include "st_abi_impl.h"
161  
162 MODULE = Template::Stencil PACKAGE = Template::Stencil
163  
164 PROTOTYPES: DISABLE
165  
166 BOOT:
167 54 stencil_boot(aTHX);
168 #ifdef STENCIL_HAVE_CALL_CHECKER
169 {
170 54 CV *rcv = get_cv("Template::Stencil::render", 0);
171 54 ▶ if (rcv)
Branch: 1 branch
BranchTrueFalseTotal
if (rcv) 54 0 54
172 54 cv_set_call_checker(rcv, stencil_render_checker,
173 (SV *)rcv);
174 }
175 #endif
176  
177 UV
178 _stencil_built()
179 CODE:
180 1 ▶ RETVAL = (UV)stencil_dispatch.caps;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)stencil_dispatch.caps; 0 1 1
181 OUTPUT:
182 RETVAL
183  
184 SV *
185 new(class, ...)
186 SV *class
187 CODE:
188 {
189 0 ▶ const char *cls = (SvROK(class) && SvOBJECT(SvRV(class)))
Branch: 1 branch
BranchTrueFalseTotal
const char *cls = (SvROK(class) && SvOBJECT(SvRV(class))) 0 0 0
190 0 ▶ ? HvNAME(SvSTASH(SvRV(class)))
Branch: 4 branches
BranchTrueFalseTotal
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
? HvNAME(SvSTASH(SvRV(class))) 0 0 0
191 542 ▶ : SvPV_nolen(class);
Branch: 3 branches
BranchTrueFalseTotal
: SvPV_nolen(class); 0 271 271
: SvPV_nolen(class); 0 0 0
: SvPV_nolen(class); 0 0 0
192 271 HV *o = newHV();
193 271 AV *pairs = newAV();
194 SSize_t np, i;
195 stencil_engine *e;
196 MAGIC *mg;
197  
198 271 sv_2mortal((SV *)o);
199 271 sv_2mortal((SV *)pairs);
200 271 ▶ if (items == 2 && SvROK(ST(1))
Branch: 2 branches
BranchTrueFalseTotal
if (items == 2 && SvROK(ST(1)) 2 269 271
if (items == 2 && SvROK(ST(1)) 1 1 2
201 2 ▶ && SvTYPE(SvRV(ST(1))) == SVt_PVHV) {
Branch: 1 branch
BranchTrueFalseTotal
&& SvTYPE(SvRV(ST(1))) == SVt_PVHV) { 1 0 1
202 1 HV *src = (HV *)SvRV(ST(1));
203 HE *he;
204 1 hv_iterinit(src);
205 2 ▶ while ((he = hv_iternext(src))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(src))) { 1 1 2
206 STRLEN klen;
207 1 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 1 1
208 1 av_push(pairs, newSVpvn(k, klen));
209 1 av_push(pairs, SvREFCNT_inc(HeVAL(he)));
210 }
211 } else {
212 270 ▶ if (!(items % 2))
Branch: 1 branch
BranchTrueFalseTotal
if (!(items % 2)) 1 269 270
213 1 croak("Template::Stencil->new: odd number of options");
214 795 ▶ for (i = 1; i < items; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < items; i++) 526 269 795
215 526 av_push(pairs, SvREFCNT_inc(ST(i)));
216 }
217  
218 270 np = av_top_index(pairs) + 1;
219 528 ▶ for (i = 0; i + 1 < np; i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i + 1 < np; i += 2) { 264 264 528
220 STRLEN klen;
221 264 const char *k = SvPV(*av_fetch(pairs, i, 0), klen);
222 264 SV *v = *av_fetch(pairs, i + 1, 0);
223 264 ▶ if (klen == 12 && memEQ(k, "template_dir", 12)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 12 && memEQ(k, "template_dir", 12)) { 21 243 264
if (klen == 12 && memEQ(k, "template_dir", 12)) { 21 0 21
224 21 ▶ if (SvOK(v)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) { 21 0 21
225 struct stat st;
226 21 ▶ if (stat(SvPV_nolen(v), &st) != 0
Branch: 1 branch
BranchTrueFalseTotal
if (stat(SvPV_nolen(v), &st) != 0 20 1 21
227 20 ▶ || !S_ISDIR(st.st_mode))
Branch: 1 branch
BranchTrueFalseTotal
|| !S_ISDIR(st.st_mode)) 0 20 20
228 1 croak("Template::Stencil->new: template_dir "
229 "'%s' is not a directory",
230 SvPV_nolen(v));
231 20 (void)hv_stores(o, "template_dir", newSVsv(v));
232 }
233 243 ▶ } else if (klen == 7 && memEQ(k, "wrapper", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 7 && memEQ(k, "wrapper", 7)) { 16 227 243
} else if (klen == 7 && memEQ(k, "wrapper", 7)) { 9 7 16
234 9 ▶ if (SvOK(v))
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(v)) 9 0 9
235 9 (void)hv_stores(o, "wrapper", newSVsv(v));
236 239 ▶ } else if (klen == 7 && memEQ(k, "filters", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 7 && memEQ(k, "filters", 7)) { 7 227 234
} else if (klen == 7 && memEQ(k, "filters", 7)) { 7 0 7
237 HE *fe;
238 HV *fh;
239 7 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) 6 1 7
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVHV) 0 6 6
240 1 croak("Template::Stencil->new: filters must be a "
241 "hashref");
242 6 fh = (HV *)SvRV(v);
243 6 hv_iterinit(fh);
244 11 ▶ while ((fe = hv_iternext(fh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((fe = hv_iternext(fh))) { 6 5 11
245 6 SV *fv = HeVAL(fe);
246 6 ▶ if (!SvROK(fv) || SvTYPE(SvRV(fv)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(fv) || SvTYPE(SvRV(fv)) != SVt_PVCV) { 5 1 6
if (!SvROK(fv) || SvTYPE(SvRV(fv)) != SVt_PVCV) { 0 5 5
247 STRLEN fklen;
248 1 ▶ const char *fk = HePV(fe, fklen);
Branch: 1 branch
BranchTrueFalseTotal
const char *fk = HePV(fe, fklen); 0 1 1
249 1 croak("Template::Stencil->new: filter '%.*s' "
250 "is not a coderef", (int)fklen, fk);
251 }
252 }
253 5 (void)hv_stores(o, "filters", newSVsv(v));
254 227 ▶ } else if ((klen == 11 && memEQ(k, "auto_escape", 11))
Branch: 2 branches
BranchTrueFalseTotal
} else if ((klen == 11 && memEQ(k, "auto_escape", 11)) 3 224 227
} else if ((klen == 11 && memEQ(k, "auto_escape", 11)) 0 3 3
255 224 ▶ || (klen == 6 && memEQ(k, "strict", 6))
Branch: 2 branches
BranchTrueFalseTotal
|| (klen == 6 && memEQ(k, "strict", 6)) 10 214 224
|| (klen == 6 && memEQ(k, "strict", 6)) 5 5 10
256 219 ▶ || (klen == 5 && memEQ(k, "cache", 5))
Branch: 2 branches
BranchTrueFalseTotal
|| (klen == 5 && memEQ(k, "cache", 5)) 5 214 219
|| (klen == 5 && memEQ(k, "cache", 5)) 4 1 5
257 218 ▶ || (klen == 5 && memEQ(k, "chars", 5))
Branch: 2 branches
BranchTrueFalseTotal
|| (klen == 5 && memEQ(k, "chars", 5)) 4 214 218
|| (klen == 5 && memEQ(k, "chars", 5)) 1 3 4
258 215 ▶ || (klen == 6 && memEQ(k, "pretty", 6))
Branch: 2 branches
BranchTrueFalseTotal
|| (klen == 6 && memEQ(k, "pretty", 6)) 5 210 215
|| (klen == 6 && memEQ(k, "pretty", 6)) 0 5 5
259 210 ▶ || (klen == 9 && memEQ(k, "sort_keys", 9))) {
Branch: 2 branches
BranchTrueFalseTotal
|| (klen == 9 && memEQ(k, "sort_keys", 9))) { 3 207 210
|| (klen == 9 && memEQ(k, "sort_keys", 9))) { 3 0 3
260 20 (void)hv_store(o, k, (I32)klen,
261 newSViv(SvTRUE(v) ? 1 : 0), 0);
262 207 ▶ } else if (klen == 8 && memEQ(k, "stat_ttl", 8)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 8 && memEQ(k, "stat_ttl", 8)) { 4 203 207
} else if (klen == 8 && memEQ(k, "stat_ttl", 8)) { 4 0 4
263 4 ▶ if (!SvOK(v) || !looks_like_number(v))
Branch: 2 branches
BranchTrueFalseTotal
if (!SvOK(v) || !looks_like_number(v)) 4 0 4
if (!SvOK(v) || !looks_like_number(v)) 1 3 4
264 1 croak("Template::Stencil->new: stat_ttl must be a "
265 "number");
266 3 (void)hv_stores(o, "stat_ttl", newSVnv(SvNV(v)));
267 203 ▶ } else if (klen == 10 && memEQ(k, "cache_size", 10)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 10 && memEQ(k, "cache_size", 10)) { 202 1 203
} else if (klen == 10 && memEQ(k, "cache_size", 10)) { 202 0 202
268 202 ▶ if (!SvOK(v) || !looks_like_number(v) || SvNV(v) < 1)
Branch: 3 branches
BranchTrueFalseTotal
if (!SvOK(v) || !looks_like_number(v) || SvNV(v) < 1) 202 0 202
if (!SvOK(v) || !looks_like_number(v) || SvNV(v) < 1) 202 0 202
if (!SvOK(v) || !looks_like_number(v) || SvNV(v) < 1) 1 201 202
269 1 croak("Template::Stencil->new: cache_size must be "
270 "a positive integer");
271 201 (void)hv_stores(o, "cache_size", newSVuv(SvUV(v)));
272 } else {
273 1 croak("Template::Stencil->new: unknown option '%.*s'",
274 (int)klen, k);
275 }
276 }
277  
278 /* defaults, stored so a thread-cloned object can rebuild */
279 264 ▶ if (!hv_fetchs(o, "auto_escape", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "auto_escape", 0)) 261 3 264
280 261 (void)hv_stores(o, "auto_escape", newSViv(1));
281 264 ▶ if (!hv_fetchs(o, "strict", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "strict", 0)) 259 5 264
282 259 (void)hv_stores(o, "strict", newSViv(0));
283 264 ▶ if (!hv_fetchs(o, "cache", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "cache", 0)) 263 1 264
284 263 (void)hv_stores(o, "cache", newSViv(1));
285 264 ▶ if (!hv_fetchs(o, "sort_keys", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "sort_keys", 0)) 261 3 264
286 261 (void)hv_stores(o, "sort_keys", newSViv(1));
287 264 ▶ if (!hv_fetchs(o, "chars", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "chars", 0)) 261 3 264
288 261 (void)hv_stores(o, "chars", newSViv(0));
289 264 ▶ if (!hv_fetchs(o, "pretty", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "pretty", 0)) 259 5 264
290 259 (void)hv_stores(o, "pretty", newSViv(0));
291 264 ▶ if (!hv_fetchs(o, "stat_ttl", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "stat_ttl", 0)) 261 3 264
292 261 (void)hv_stores(o, "stat_ttl", newSVnv(1.0));
293 264 ▶ if (!hv_fetchs(o, "cache_size", 0))
Branch: 1 branch
BranchTrueFalseTotal
if (!hv_fetchs(o, "cache_size", 0)) 63 201 264
294 63 (void)hv_stores(o, "cache_size", newSVuv(256));
295  
296 264 e = stencil_build_engine(aTHX_ o);
297 264 mg = sv_magicext((SV *)o, NULL, PERL_MAGIC_ext,
298 &stencil_obj_vtbl, (char *)e, 0);
299 #ifdef USE_ITHREADS
300 mg->mg_flags |= MGf_DUP;
301 #else
302 PERL_UNUSED_VAR(mg);
303 #endif
304 264 RETVAL = sv_bless(newRV_inc((SV *)o),
305 gv_stashpv(cls, GV_ADD));
306 }
307 OUTPUT:
308 RETVAL
309  
310 SV *
311 render(self, tmpl, ...)
312 SV *self
313 SV *tmpl
314 CODE:
315 4407 ▶ RETVAL = stencil_obj_render(aTHX_ self, tmpl,
Branch: 2 branches
BranchTrueFalseTotal
RETVAL = stencil_obj_render(aTHX_ self, tmpl, 632 1256 1888
RETVAL = stencil_obj_render(aTHX_ self, tmpl, 1887 1 1888
316 1887 items > 2 ? ST(2) : NULL,
317 632 items > 3 ? ST(3) : NULL);
318 OUTPUT:
319 RETVAL
320  
321 SV *
322 _arena_selftest()
323 CODE:
324 {
325 1 const char *err = stencil_arena_selftest();
326 1 ▶ RETVAL = err ? newSVpv(err, 0) : &PL_sv_undef;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = err ? newSVpv(err, 0) : &PL_sv_undef; 0 1 1
327 }
328 OUTPUT:
329 RETVAL
330  
331 SV *
332 _escape(sv)
333 SV *sv
334 CODE:
335 {
336 STRLEN len;
337 522 const char *p = SvPV_const(sv, len);
338 stencil_buf b;
339 522 stencil_buf_init(aTHX_ &b, len + 16);
340 522 stencil_dispatch.escape(&b, p, len);
341 522 ▶ if (SvUTF8(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) 2 520 522
342 2 b.utf8 = 1;
343 522 RETVAL = stencil_buf_done(&b);
344 }
345 OUTPUT:
346 RETVAL
347  
348 SV *
349 _escape_off(sv, off)
350 SV *sv
351 UV off
352 CODE:
353 {
354 STRLEN len;
355 68 const char *p = SvPV_const(sv, len);
356 stencil_buf b;
357 68 ▶ if (off > len)
Branch: 1 branch
BranchTrueFalseTotal
if (off > len) 0 68 68
358 0 croak("_escape_off: offset %" UVuf " past end", off);
359 68 stencil_buf_init(aTHX_ &b, len - (STRLEN)off + 16);
360 68 stencil_dispatch.escape(&b, p + (STRLEN)off, len - (STRLEN)off);
361 68 ▶ if (SvUTF8(sv))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(sv)) 0 68 68
362 0 b.utf8 = 1;
363 68 RETVAL = stencil_buf_done(&b);
364 }
365 OUTPUT:
366 RETVAL
367  
368 SV *
369 _inspect(src)
370 SV *src
371 CODE:
372 {
373 STRLEN len;
374 85 const char *p = SvPV_const(src, len);
375 85 SV *err = NULL;
376 stencil_program *prog =
377 85 stencil_compile(aTHX_ p, len, "<string>",
378 85 SvUTF8(src) ? STENCIL_PROG_SRC_UTF8 : 0,
379 &err);
380 85 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 32 53 85
381 32 croak_sv(err);
382 53 RETVAL = stencil_program_inspect(aTHX_ prog);
383 53 stencil_program_free(prog);
384 }
385 OUTPUT:
386 RETVAL
387  
388 SV *
389 _render(src, data, ...)
390 SV *src
391 SV *data
392 CODE:
393 {
394 STRLEN len;
395 260 const char *p = SvPV_const(src, len);
396 260 SV *err = NULL;
397 260 ▶ UV flags = items > 2 ? SvUV(ST(2)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV flags = items > 2 ? SvUV(ST(2)) : 0; 36 224 260
398 SV *out;
399 stencil_program *prog;
400 260 ▶ if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) 260 0 260
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) 0 260 260
401 0 croak("Template::Stencil: render data must be a hashref");
402 260 prog = stencil_compile(aTHX_ p, len, "<string>",
403 260 SvUTF8(src) ? STENCIL_PROG_SRC_UTF8 : 0,
404 &err);
405 260 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 12 248 260
406 12 croak_sv(err);
407 248 out = stencil_render_run(aTHX_ prog, (HV *)SvRV(data),
408 (uint32_t)flags, "<string>", &err);
409 248 stencil_program_free(prog);
410 248 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 26 222 248
411 26 croak_sv(err);
412 222 RETVAL = out;
413 }
414 OUTPUT:
415 RETVAL
416  
417 IV
418 _compile_handle(src)
419 SV *src
420 CODE:
421 {
422 STRLEN len;
423 1321 const char *p = SvPV_const(src, len);
424 1321 SV *err = NULL;
425 stencil_program *prog =
426 1321 stencil_compile(aTHX_ p, len, "<string>",
427 1321 SvUTF8(src) ? STENCIL_PROG_SRC_UTF8 : 0,
428 &err);
429 1321 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 0 1321 1321
430 0 croak_sv(err);
431 1321 ▶ RETVAL = PTR2IV(prog);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2IV(prog); 1316 5 1321
432 }
433 OUTPUT:
434 RETVAL
435  
436 SV *
437 _run_handle(handle, data, ...)
438 IV handle
439 SV *data
440 CODE:
441 {
442 100310 stencil_program *prog = INT2PTR(stencil_program *, handle);
443 100310 SV *err = NULL;
444 100310 ▶ UV flags = items > 2 ? SvUV(ST(2)) : 0;
Branch: 1 branch
BranchTrueFalseTotal
UV flags = items > 2 ? SvUV(ST(2)) : 0; 0 100310 100310
445 SV *out;
446 100310 ▶ if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) 100310 0 100310
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) 0 100310 100310
447 0 croak("Template::Stencil: render data must be a hashref");
448 100310 out = stencil_render_run(aTHX_ prog, (HV *)SvRV(data),
449 (uint32_t)flags, "<string>", &err);
450 100310 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 0 100310 100310
451 0 croak_sv(err);
452 100310 RETVAL = out;
453 }
454 OUTPUT:
455 RETVAL
456  
457 void
458 _free_handle(handle)
459 IV handle
460 CODE:
461 1321 stencil_program_free(INT2PTR(stencil_program *, handle));
462  
463 SV *
464 _stencil_stats()
465 CODE:
466 {
467 43 HV *hv = newHV();
468 43 (void)hv_stores(hv, "buf_grows", newSVuv(stencil_stat_buf_grows));
469 43 (void)hv_stores(hv, "scratch_allocs",
470 newSVuv(stencil_stat_scratch_allocs));
471 43 (void)hv_stores(hv, "compiles", newSVuv(stencil_stat_compiles));
472 43 (void)hv_stores(hv, "cache_hits",
473 newSVuv(stencil_stat_cache_hits));
474 43 (void)hv_stores(hv, "stats", newSVuv(stencil_stat_stats));
475 43 (void)hv_stores(hv, "checker", newSVuv(stencil_stat_checker));
476 43 (void)hv_stores(hv, "engines", newSVuv(stencil_stat_engines));
477 43 RETVAL = newRV_noinc((SV *)hv);
478 }
479 OUTPUT:
480 RETVAL
481  
482 IV
483 _engine_new(template_dir, wrapper, flags, stat_ttl, cache_size, ...)
484 SV *template_dir
485 SV *wrapper
486 UV flags
487 NV stat_ttl
488 UV cache_size
489 CODE:
490 {
491 33 HV *filters = NULL;
492 stencil_engine *e;
493 33 ▶ if (items > 5 && SvOK(ST(5))) {
Branch: 2 branches
BranchTrueFalseTotal
if (items > 5 && SvOK(ST(5))) { 7 26 33
if (items > 5 && SvOK(ST(5))) { 7 0 7
494 HE *he;
495 7 ▶ if (!SvROK(ST(5)) || SvTYPE(SvRV(ST(5))) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(ST(5)) || SvTYPE(SvRV(ST(5))) != SVt_PVHV) 7 0 7
if (!SvROK(ST(5)) || SvTYPE(SvRV(ST(5))) != SVt_PVHV) 0 7 7
496 0 croak("Template::Stencil: filters must be a hashref");
497 7 filters = (HV *)SvRV(ST(5));
498 7 hv_iterinit(filters);
499 31 ▶ while ((he = hv_iternext(filters))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(filters))) { 25 6 31
500 25 SV *v = HeVAL(he);
501 25 ▶ if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVCV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVCV) { 24 1 25
if (!SvROK(v) || SvTYPE(SvRV(v)) != SVt_PVCV) { 0 24 24
502 STRLEN klen;
503 1 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 1 1
504 1 croak("Template::Stencil: filter '%.*s' is not a "
505 "coderef", (int)klen, k);
506 }
507 }
508 }
509 58 e = stencil_engine_new(aTHX_
510 58 ▶ SvOK(template_dir) ? SvPV_nolen(template_dir) : NULL,
Branch: 1 branch
BranchTrueFalseTotal
SvOK(template_dir) ? SvPV_nolen(template_dir) : NULL, 26 6 32
511 38 ▶ SvOK(wrapper) ? SvPV_nolen(wrapper) : NULL,
Branch: 1 branch
BranchTrueFalseTotal
SvOK(wrapper) ? SvPV_nolen(wrapper) : NULL, 6 26 32
512 (uint32_t)flags, (double)stat_ttl, (uint32_t)cache_size,
513 filters);
514 32 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 0 32 32
515 0 croak("Template::Stencil: engine allocation failed");
516 32 ▶ RETVAL = PTR2IV(e);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2IV(e); 17 15 32
517 }
518 OUTPUT:
519 RETVAL
520  
521 void
522 _engine_free(handle)
523 IV handle
524 CODE:
525 32 stencil_engine_free(aTHX_ INT2PTR(stencil_engine *, handle));
526  
527 SV *
528 _engine_render(handle, tmpl, data, ...)
529 IV handle
530 SV *tmpl
531 SV *data
532 CODE:
533 {
534 68 stencil_engine *e = INT2PTR(stencil_engine *, handle);
535 68 ▶ SV *opts = items > 3 ? ST(3) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
SV *opts = items > 3 ? ST(3) : NULL; 3 65 68
536 68 SV *err = NULL;
537 SV *out;
538 68 ▶ if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) 68 0 68
if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) 0 68 68
539 0 croak("Template::Stencil: render data must be a hashref");
540 68 out = stencil_engine_render(aTHX_ e, tmpl, (HV *)SvRV(data),
541 opts, &err);
542 68 ▶ if (!out)
Branch: 1 branch
BranchTrueFalseTotal
if (!out) 15 53 68
543 15 croak_sv(err);
544 53 RETVAL = out;
545 }
546 OUTPUT:
547 RETVAL
548  
549 UV
550 _perl_hash(sv)
551 SV *sv
552 CODE:
553 {
554 STRLEN len;
555 2 const char *p = SvPV_const(sv, len);
556 U32 hash;
557 2 ▶ PERL_HASH(hash, p, len);
Branch: 1 branch
BranchTrueFalseTotal
PERL_HASH(hash, p, len); 2 0 2
558 2 ▶ RETVAL = (UV)hash;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)hash; 1 1 2
559 }
560 OUTPUT:
561 RETVAL
562  
563 SV *
564 _buf_selftest()
565 CODE:
566 {
567 stencil_buf b;
568 int i;
569 1 stencil_buf_init(aTHX_ &b, 4);
570 1001 ▶ for (i = 0; i < 1000; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 1000; i++) 1000 1 1001
571 1000 stencil_buf_write(&b, "abc", 3);
572 1 stencil_buf_write8(&b, "12345678", 4);
573 1 RETVAL = stencil_buf_done(&b);
574 }
575 OUTPUT:
576 RETVAL
577  
578 # Address of Template::Stencil's own C ABI table (st_abi.h). A consumer XS
579 # module (Punk's view tier) fetches this once at boot, INT2PTRs it to a
580 # `const st_abi *`, and checks ->abi_version before using it. Not part of the
581 # public Perl API.
582 #
583 # Unsigned, not IV: where the loader maps this object decides the sign bit.
584 # A 32-bit perl above 0x7fffffff, or illumos putting shared objects up at
585 # 0xfffffd7f..., would hand back a negative number from PTR2IV.
586 UV
587 _abi_ptr()
588 CODE:
589 72 ▶ RETVAL = PTR2UV(&ST_ABI);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(&ST_ABI); 0 72 72
590 OUTPUT:
591 RETVAL
592  
593 # Exercise the whole st_abi table the way a C consumer would: resolve it from
594 # the UV _abi_ptr hands back, gate on abi_version, then drive engine_of ->
595 # render through the function pointers rather than calling the C directly.
596 # Returns the rendered bytes; (undef, $error) when the template fails; and an
597 # empty list when the gate rejects the table or the invocant is not a Stencil
598 # object - which is where a consumer would fall back to the render method.
599 void
600 _abi_selftest(self, tmpl, data = &PL_sv_undef, opts = &PL_sv_undef)
601 SV *self
602 SV *tmpl
603 SV *data
604 SV *opts
605 PPCODE:
606 {
607 70 const st_abi *abi = NULL;
608 void *engine;
609 70 SV *err = NULL, *out;
610 HV *dhv;
611 {
612 70 dSP;
613 70 UV ptr = 0;
614 int count;
615 70 ENTER; SAVETMPS;
616 70 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 70 70
617 70 PUTBACK;
618 70 count = call_pv("Template::Stencil::_abi_ptr", G_SCALAR | G_EVAL);
619 70 SPAGAIN;
620 70 ▶ if (count > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 0) { 70 0 70
621 /* Pop into a local first: before 5.30 SvUV was a macro that
622 * evaluated its argument twice (SvIOK(sv) ? SvUVX(sv) :
623 * sv_2uv(sv)), so SvUV(POPs) popped twice and read the value
624 * off whatever sat one slot lower. Modern perls make it an
625 * inline function, which is why this only ever bit on old
626 * ones.
627 *
628 * SvUV, not SvIV, to match the UV _abi_ptr returns: an
629 * address with the top bit set is above IV_MAX, and SvIV
630 * would clamp it rather than hand back the address. */
631 70 SV *rv = POPs;
632 70 ▶ if (!SvTRUE(ERRSV)) ptr = SvUV(rv);
Branch: 2 branches
BranchTrueFalseTotal
if (!SvTRUE(ERRSV)) ptr = SvUV(rv); 70 0 70
if (!SvTRUE(ERRSV)) ptr = SvUV(rv); 70 0 70
633 }
634 70 ▶ PUTBACK; FREETMPS; LEAVE;
Branch: 1 branch
BranchTrueFalseTotal
PUTBACK; FREETMPS; LEAVE; 70 0 70
635 70 ▶ if (ptr) abi = INT2PTR(const st_abi *, ptr);
Branch: 1 branch
BranchTrueFalseTotal
if (ptr) abi = INT2PTR(const st_abi *, ptr); 70 0 70
636 }
637 70 ▶ if (!abi || abi->abi_version < ST_ABI_VERSION) XSRETURN_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
if (!abi || abi->abi_version < ST_ABI_VERSION) XSRETURN_EMPTY; 70 0 70
if (!abi || abi->abi_version < ST_ABI_VERSION) XSRETURN_EMPTY; 0 70 70
638 70 engine = abi->engine_of(aTHX_ self);
639 70 ▶ if (!engine) XSRETURN_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
if (!engine) XSRETURN_EMPTY; 5 65 70
640 65 ▶ dhv = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
dhv = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 61 4 65
dhv = (data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV) 61 0 61
641 130 ▶ ? (HV *)SvRV(data) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
? (HV *)SvRV(data) : NULL; 65 0 65
642 130 ▶ out = abi->render(aTHX_ engine, tmpl, dhv,
Branch: 1 branch
BranchTrueFalseTotal
out = abi->render(aTHX_ engine, tmpl, dhv, 65 0 65
643 65 ▶ (opts && SvOK(opts)) ? opts : NULL, &err);
Branch: 1 branch
BranchTrueFalseTotal
(opts && SvOK(opts)) ? opts : NULL, &err); 2 63 65
644 65 ▶ if (!out) {
Branch: 1 branch
BranchTrueFalseTotal
if (!out) { 2 63 65
645 2 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 2 2
646 2 mPUSHs(newSV(0));
647 2 ▶ PUSHs(err ? err : sv_2mortal(newSVpvs("unknown render error")));
Branch: 1 branch
BranchTrueFalseTotal
PUSHs(err ? err : sv_2mortal(newSVpvs("unknown render error"))); 0 2 2
648 2 XSRETURN(2);
649 }
650 63 ▶ EXTEND(SP, 1);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 1); 0 63 63
651 63 mPUSHs(out);
652 63 XSRETURN(1);
653 }
« src/stencil_engine.c ↑ Summary src/stencil_render.c »