src/stencil_engine.c

filter: Statementstmt 87.9% 510 / 580 Branchbran 73.0% 336 / 460 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 81.3% 846 / 1040 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« src/stencil_arena.c ↑ Summary Stencil.xs »
1 #include "stencil.h"
2  
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <time.h>
7  
8 #ifdef _WIN32
9 # include <io.h> /* open/read/close live here, not in unistd.h */
10 #else
11 # include <unistd.h>
12 #endif
13  
14 /* Templates are read as bytes: without O_BINARY the Windows CRT eats the
15 * CR of every CRLF, so the read returns fewer bytes than the stat said and
16 * the tail of the buffer is garbage. Everywhere else it is a no-op. */
17 #ifndef O_BINARY
18 # define O_BINARY 0
19 #endif
20  
21 /* MSVC ships neither S_ISREG nor S_ISDIR. */
22 #ifndef S_ISREG
23 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
24 #endif
25 #ifndef S_ISDIR
26 # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
27 #endif
28  
29 /* Path separator set: on Windows a backslash separates too, so both the
30 * traversal guard and the extension sniff have to honour it. */
31 #ifdef _WIN32
32 # define STENCIL_IS_SEP(c) ((c) == '/' || (c) == '\\')
33 #else
34 # define STENCIL_IS_SEP(c) ((c) == '/')
35 #endif
36  
37 UV stencil_stat_compiles = 0;
38 UV stencil_stat_engines = 0;
39 UV stencil_stat_cache_hits = 0;
40 UV stencil_stat_stats = 0;
41  
42 #define STENCIL_PATH_MAX 4096
43 #define STENCIL_N_BUCKETS 128u /* fixed; chains handle the rest */
44  
45 4342 static const char *ent_name(const stencil_cache_ent *ent)
46 {
47 4342 ▶ return ent->abs_path ? ent->abs_path : "<string>";
Branch: 1 branch
BranchTrueFalseTotal
return ent->abs_path ? ent->abs_path : "<string>"; 268 4074 4342
48 }
49  
50 static int looks_like_source(const char *p, STRLEN len);
51  
52 /* Pretty preprocessing: drop whitespace-only lines (the artifacts of
53 * block-tag indentation in templates) so the reformatter starts from
54 * dense markup. */
55 9 static SV *pretty_strip_blank_lines(pTHX_ SV *html)
56 {
57 STRLEN len;
58 9 const char *p = SvPV(html, len);
59 9 const char *end = p + len;
60 9 ▶ SV *out = newSV(len ? len : 1);
Branch: 1 branch
BranchTrueFalseTotal
SV *out = newSV(len ? len : 1); 9 0 9
61 char *w;
62 9 SvPOK_only(out);
63 9 w = SvPVX(out);
64 154 ▶ while (p < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) { 145 9 154
65 145 const char *eol = (const char *)memchr(p, '\n',
66 145 (size_t)(end - p));
67 145 ▶ const char *stop = eol ? eol : end;
Branch: 1 branch
BranchTrueFalseTotal
const char *stop = eol ? eol : end; 139 6 145
68 145 const char *q = p;
69 261 ▶ while (q < stop && (*q == ' ' || *q == '\t' || *q == '\r'))
Branch: 4 branches
BranchTrueFalseTotal
while (q < stop && (*q == ' ' || *q == '\t' || *q == '\r')) 199 62 261
while (q < stop && (*q == ' ' || *q == '\t' || *q == '\r')) 39 160 199
while (q < stop && (*q == ' ' || *q == '\t' || *q == '\r')) 77 83 160
while (q < stop && (*q == ' ' || *q == '\t' || *q == '\r')) 0 83 83
70 116 q++;
71 145 ▶ if (q != stop) { /* line has content */
Branch: 1 branch
BranchTrueFalseTotal
if (q != stop) { /* line has content */ 83 62 145
72 83 memcpy(w, p, (size_t)(stop - p));
73 83 w += stop - p;
74 83 ▶ if (eol)
Branch: 1 branch
BranchTrueFalseTotal
if (eol) 77 6 83
75 77 *w++ = '\n';
76 }
77 145 ▶ p = eol ? eol + 1 : end;
Branch: 1 branch
BranchTrueFalseTotal
p = eol ? eol + 1 : end; 139 6 145
78 }
79 9 SvCUR_set(out, (STRLEN)(w - SvPVX(out)));
80 9 SvPVX(out)[SvCUR(out)] = '\0';
81 9 ▶ if (SvUTF8(html))
Branch: 1 branch
BranchTrueFalseTotal
if (SvUTF8(html)) 0 9 9
82 0 SvUTF8_on(out);
83 9 return out;
84 }
85  
86 /* pretty => 1: hand the rendered HTML to Eshu->indent_html. Eshu is an
87 * optional dependency, loaded lazily on first use; requesting pretty
88 * without it is an error, not having it and never asking costs
89 * nothing. Returns a fresh SV or NULL with *err set. */
90 9 static SV *eshu_prettify(pTHX_ SV *html, SV **err)
91 {
92 static int eshu_state = 0; /* 0 unknown, 1 ok, -1 unavailable */
93 9 SV *ret = NULL;
94  
95 9 ▶ if (eshu_state == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_state == 0) { 3 6 9
96 3 SV *ok = eval_pv("require Eshu; 1", FALSE);
97 3 ▶ eshu_state = (ok && SvTRUE(ok)) ? 1 : -1;
Branch: 2 branches
BranchTrueFalseTotal
eshu_state = (ok && SvTRUE(ok)) ? 1 : -1; 3 0 3
eshu_state = (ok && SvTRUE(ok)) ? 1 : -1; 3 0 3
98 }
99 9 ▶ if (eshu_state < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (eshu_state < 0) { 0 9 9
100 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
101 0 *err = sv_2mortal(newSVpvs(
102 "Template::Stencil: pretty => 1 requires the Eshu "
103 "module"));
104 0 return NULL;
105 }
106 {
107 9 dSP;
108 int count;
109 9 ENTER;
110 9 SAVETMPS;
111 9 ▶ PUSHMARK(SP);
Branch: 1 branch
BranchTrueFalseTotal
PUSHMARK(SP); 0 9 9
112 9 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 9 9
113 9 mPUSHp("Eshu", 4);
114 9 PUSHs(html);
115 9 PUTBACK;
116 9 count = call_method("indent_html", G_SCALAR | G_EVAL);
117 9 SPAGAIN;
118 9 ▶ if (SvTRUE(ERRSV)) {
Branch: 2 branches
BranchTrueFalseTotal
if (SvTRUE(ERRSV)) { 9 0 9
if (SvTRUE(ERRSV)) { 0 9 9
119 0 ▶ if (count)
Branch: 1 branch
BranchTrueFalseTotal
if (count) 0 0 0
120 0 (void)POPs;
121 0 PUTBACK;
122 0 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 0 0 0
123 0 LEAVE;
124 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
125 0 ▶ *err = sv_2mortal(newSVpvf(
Branch: 1 branch
BranchTrueFalseTotal
*err = sv_2mortal(newSVpvf( 0 0 0
126 "Template::Stencil: Eshu->indent_html died: %s",
127 SvPV_nolen(ERRSV)));
128 0 return NULL;
129 }
130 9 ▶ ret = count ? newSVsv(POPs) : newSVpvs("");
Branch: 1 branch
BranchTrueFalseTotal
ret = count ? newSVsv(POPs) : newSVpvs(""); 9 0 9
131 9 PUTBACK;
132 9 ▶ FREETMPS;
Branch: 1 branch
BranchTrueFalseTotal
FREETMPS; 9 0 9
133 9 LEAVE;
134 }
135 9 return ret;
136 }
137  
138 /* ---- path safety and resolution -------------------------------------- */
139  
140 /* Templates never escape template_dir: no absolute paths, no '..'
141 * segments anywhere. Template names are always '/'-joined logical paths,
142 * so a backslash in one is never legitimate - the traversal scan splits on
143 * it too, on every platform, rather than letting 'a\..\..\etc' through on
144 * the one OS where the kernel would honour it. */
145 413 static int name_unsafe(const char *n, size_t len)
146 {
147 413 size_t i = 0;
148 413 ▶ if (!len || n[0] == '/' || n[0] == '\\')
Branch: 3 branches
BranchTrueFalseTotal
if (!len || n[0] == '/' || n[0] == '\\') 411 2 413
if (!len || n[0] == '/' || n[0] == '\\') 408 3 411
if (!len || n[0] == '/' || n[0] == '\\') 3 405 408
149 8 return 1;
150 #ifdef _WIN32
151 /* 'C:foo' and 'C:/foo' both leave template_dir behind. */
152 if (len >= 2 && n[1] == ':')
153 return 1;
154 #endif
155 964 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 564 400 964
156 564 size_t j = i;
157 129304 ▶ while (j < len && n[j] != '/' && n[j] != '\\')
Branch: 3 branches
BranchTrueFalseTotal
while (j < len && n[j] != '/' && n[j] != '\\') 128908 396 129304
while (j < len && n[j] != '/' && n[j] != '\\') 128816 92 128908
while (j < len && n[j] != '/' && n[j] != '\\') 128740 76 128816
158 128740 j++;
159 564 ▶ if (j - i == 2 && n[i] == '.' && n[i + 1] == '.')
Branch: 3 branches
BranchTrueFalseTotal
if (j - i == 2 && n[i] == '.' && n[i + 1] == '.') 22 542 564
if (j - i == 2 && n[i] == '.' && n[i + 1] == '.') 5 17 22
if (j - i == 2 && n[i] == '.' && n[i + 1] == '.') 5 0 5
160 5 return 1;
161 559 i = j + 1;
162 }
163 400 return 0;
164 }
165  
166 413 static int final_seg_has_dot(const char *n, size_t len)
167 {
168 413 size_t i = len;
169 118590 ▶ while (i > 0 && !STENCIL_IS_SEP(n[i - 1])) {
Branch: 2 branches
BranchTrueFalseTotal
while (i > 0 && !STENCIL_IS_SEP(n[i - 1])) { 118403 187 118590
while (i > 0 && !STENCIL_IS_SEP(n[i - 1])) { 118346 57 118403
170 118346 ▶ if (n[i - 1] == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (n[i - 1] == '.') 169 118177 118346
171 169 return 1;
172 118177 i--;
173 }
174 244 return 0;
175 }
176  
177 668 static int try_stat(const char *path, struct stat *st)
178 {
179 668 stencil_stat_stats++;
180 668 ▶ return stat(path, st) == 0 && S_ISREG(st->st_mode);
Branch: 2 branches
BranchTrueFalseTotal
return stat(path, st) == 0 && S_ISREG(st->st_mode); 151 517 668
return stat(path, st) == 0 && S_ISREG(st->st_mode); 151 0 151
181 }
182  
183 /* Try template_dir/name[.tmpl], then cwd-relative name[.tmpl] (the
184 * draft test renders 't/template/loops' with template_dir already set
185 * to t/template). Returns 1 with out+st filled.
186 *
187 * Each snprintf is followed by an explicit terminator: perl's win32.h maps
188 * snprintf onto _snprintf for the older Windows toolchains, and that one
189 * leaves the buffer unterminated when it truncates. On a C99 snprintf the
190 * store is a no-op. */
191 413 static int resolve_file(const stencil_engine *e, const char *name,
192 size_t nlen, char *out, size_t outsz,
193 struct stat *st)
194 {
195 413 int has_dot = final_seg_has_dot(name, nlen);
196 413 ▶ if (name_unsafe(name, nlen))
Branch: 1 branch
BranchTrueFalseTotal
if (name_unsafe(name, nlen)) 13 400 413
197 13 return 0;
198 400 ▶ if (e->template_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->template_dir) { 160 240 400
199 160 snprintf(out, outsz, "%s/%.*s", e->template_dir, (int)nlen, name);
200 160 out[outsz - 1] = '\0';
201 160 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 95 65 160
202 95 return 1;
203 65 ▶ if (!has_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_dot) { 62 3 65
204 62 snprintf(out, outsz, "%s/%.*s.tmpl", e->template_dir,
205 (int)nlen, name);
206 62 out[outsz - 1] = '\0';
207 62 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 49 13 62
208 49 return 1;
209 }
210 }
211 256 snprintf(out, outsz, "%.*s", (int)nlen, name);
212 256 out[outsz - 1] = '\0';
213 256 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 3 253 256
214 3 return 1;
215 253 ▶ if (!has_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_dot) { 190 63 253
216 190 snprintf(out, outsz, "%.*s.tmpl", (int)nlen, name);
217 190 out[outsz - 1] = '\0';
218 190 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 4 186 190
219 4 return 1;
220 }
221 249 return 0;
222 }
223  
224 /* Read a file: the (mtime,size) snapshot is taken before the read so a
225 * mid-read write is caught by the next revalidation. */
226 96 static char *slurp(pTHX_ const char *path, STRLEN *lenp, time_t *mtime,
227 Off_t *fsize, SV **err)
228 {
229 struct stat st;
230 96 char *buf = NULL;
231 SSize_t got;
232 96 size_t have = 0;
233 96 int fd = open(path, O_RDONLY | O_BINARY);
234 96 ▶ if (fd < 0 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) {
Branch: 3 branches
BranchTrueFalseTotal
if (fd < 0 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { 96 0 96
if (fd < 0 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { 96 0 96
if (fd < 0 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { 0 96 96
235 0 ▶ if (fd >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (fd >= 0) 0 0 0
236 0 close(fd);
237 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
238 0 *err = sv_2mortal(newSVpvf(
239 "Template::Stencil: cannot read template '%s'", path));
240 0 return NULL;
241 }
242 96 *mtime = st.st_mtime;
243 96 *fsize = st.st_size;
244 96 buf = (char *)malloc((size_t)st.st_size + 1);
245 96 ▶ if (!buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) { 0 96 96
246 0 close(fd);
247 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
248 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
249 0 return NULL;
250 }
251 192 ▶ while (have < (size_t)st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (have < (size_t)st.st_size) { 96 96 192
252 /* The Windows CRT read() counts in unsigned int and read(2) itself
253 * refuses more than ~2GB, so cap the chunk: after this the cast is
254 * lossless on every platform. */
255 96 size_t want = (size_t)st.st_size - have;
256 96 ▶ if (want > 0x7ffff000u)
Branch: 1 branch
BranchTrueFalseTotal
if (want > 0x7ffff000u) 0 96 96
257 0 want = 0x7ffff000u;
258 96 got = read(fd, buf + have, (unsigned int)want);
259 96 ▶ if (got <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) 0 96 96
260 0 break;
261 96 have += (size_t)got;
262 }
263 96 close(fd);
264 96 buf[have] = '\0';
265 96 *lenp = (STRLEN)have;
266 96 return buf;
267 }
268  
269 /* ---- cache table ------------------------------------------------------ */
270  
271 9817 static stencil_cache_ent **bucket_for(stencil_engine *e, uint64_t h)
272 {
273 9817 return &e->buckets[(uint32_t)h & (e->n_buckets - 1)];
274 }
275  
276 3146 static void lru_unlink(stencil_engine *e, stencil_cache_ent *ent)
277 {
278 3146 ▶ if (ent->lru_prev)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->lru_prev) 449 2697 3146
279 449 ent->lru_prev->lru_next = ent->lru_next;
280 2697 ▶ else if (e->lru_head == ent)
Branch: 1 branch
BranchTrueFalseTotal
else if (e->lru_head == ent) 2697 0 2697
281 2697 e->lru_head = ent->lru_next;
282 3146 ▶ if (ent->lru_next)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->lru_next) 2517 629 3146
283 2517 ent->lru_next->lru_prev = ent->lru_prev;
284 629 ▶ else if (e->lru_tail == ent)
Branch: 1 branch
BranchTrueFalseTotal
else if (e->lru_tail == ent) 629 0 629
285 629 e->lru_tail = ent->lru_prev;
286 3146 ent->lru_next = ent->lru_prev = NULL;
287 3146 }
288  
289 4336 static void lru_push_front(stencil_engine *e, stencil_cache_ent *ent)
290 {
291 4336 ent->lru_prev = NULL;
292 4336 ent->lru_next = e->lru_head;
293 4336 ▶ if (e->lru_head)
Branch: 1 branch
BranchTrueFalseTotal
if (e->lru_head) 3595 741 4336
294 3595 e->lru_head->lru_prev = ent;
295 4336 e->lru_head = ent;
296 4336 ▶ if (!e->lru_tail)
Branch: 1 branch
BranchTrueFalseTotal
if (!e->lru_tail) 741 3595 4336
297 741 e->lru_tail = ent;
298 4336 }
299  
300 1458 static void ent_free(pTHX_ stencil_cache_ent *ent)
301 {
302 1458 free(ent->abs_path);
303 1458 free(ent->src);
304 1458 ▶ if (ent->incs)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->incs) 41 1417 1458
305 41 Safefree(ent->incs);
306 1458 stencil_program_free(ent->prog);
307 1458 free(ent);
308 1458 }
309  
310 175 static void bucket_unlink(stencil_engine *e, stencil_cache_ent *ent,
311 uint64_t h)
312 {
313 175 stencil_cache_ent **pp = bucket_for(e, h);
314 509 ▶ while (*pp && *pp != ent)
Branch: 2 branches
BranchTrueFalseTotal
while (*pp && *pp != ent) 509 0 509
while (*pp && *pp != ent) 334 175 509
315 334 pp = &(*pp)->next;
316 175 ▶ if (*pp)
Branch: 1 branch
BranchTrueFalseTotal
if (*pp) 175 0 175
317 175 *pp = ent->next;
318 175 }
319  
320 329 static void engine_cache_clear(pTHX_ stencil_engine *e)
321 {
322 uint32_t i;
323 42441 ▶ for (i = 0; i < e->n_buckets; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->n_buckets; i++) { 42112 329 42441
324 42112 stencil_cache_ent *ent = e->buckets[i];
325 43395 ▶ while (ent) {
Branch: 1 branch
BranchTrueFalseTotal
while (ent) { 1283 42112 43395
326 1283 stencil_cache_ent *next = ent->next;
327 1283 ent_free(aTHX_ ent);
328 1283 ent = next;
329 }
330 42112 e->buckets[i] = NULL;
331 }
332 329 e->lru_head = e->lru_tail = NULL;
333 329 e->n_ents = e->n_string = 0;
334 329 e->generation++;
335 329 }
336  
337 /* ---- compile + link --------------------------------------------------- */
338  
339 static stencil_cache_ent *get_file_ent(pTHX_ stencil_engine *e,
340 const char *name, size_t nlen,
341 int *notfound, SV **err);
342  
343 1395 static int link_ent(pTHX_ stencil_engine *e, stencil_cache_ent *ent,
344 SV **err)
345 {
346 1395 stencil_program *prog = ent->prog;
347 1395 const stencil_cinc *incs = stencil_prog_incs(prog);
348 1395 const char *pool = stencil_prog_pool(prog);
349 1395 uint32_t n = prog->n_incs, i;
350  
351 1395 ent->linked = 0;
352 1395 ▶ if (ent->incs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ent->incs) { 0 1395 1395
353 0 Safefree(ent->incs);
354 0 ent->incs = NULL;
355 }
356 1395 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 1354 41 1395
357 1354 ent->linked = 1;
358 1354 return 1;
359 }
360 41 Newxz(ent->incs, n, stencil_cache_ent *);
361 41 ent->linking = 1;
362 71 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 41 30 71
363 41 int notfound = 0;
364 stencil_cache_ent *child =
365 41 get_file_ent(aTHX_ e, pool + incs[i].name_off,
366 41 incs[i].name_len, &notfound, err);
367 41 ▶ if (!child) {
Branch: 1 branch
BranchTrueFalseTotal
if (!child) { 9 32 41
368 /* resolve failures get the include-site context; deeper
369 * errors (cycles, compile errors) pass through untouched */
370 9 ▶ if (notfound && err)
Branch: 2 branches
BranchTrueFalseTotal
if (notfound && err) 8 1 9
if (notfound && err) 8 0 8
371 8 *err = sv_2mortal(newSVpvf(
372 "Template::Stencil: %s:%u:%u: cannot find include "
373 "'%.*s'", ent_name(ent), (unsigned)incs[i].line,
374 (unsigned)incs[i].col, (int)incs[i].name_len,
375 pool + incs[i].name_off));
376 9 ent->linking = 0;
377 11 return 0;
378 }
379 32 ▶ if (child->linking) {
Branch: 1 branch
BranchTrueFalseTotal
if (child->linking) { 2 30 32
380 2 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 2 0 2
381 2 *err = sv_2mortal(newSVpvf(
382 "Template::Stencil: %s:%u:%u: include cycle: "
383 "%s -> %s", ent_name(ent), (unsigned)incs[i].line,
384 (unsigned)incs[i].col, ent_name(ent),
385 ent_name(child)));
386 2 ent->linking = 0;
387 2 return 0;
388 }
389 30 ent->incs[i] = child;
390 }
391 30 ent->linking = 0;
392 30 ent->linked = 1;
393 30 return 1;
394 }
395  
396 /* A cached entry whose earlier link failed (cycle, missing include)
397 * retries on the next use - it may have been fixed on disk. Never
398 * relink mid-DFS: the caller's cycle check handles that. */
399 2980 static int ent_ready(pTHX_ stencil_engine *e, stencil_cache_ent *ent,
400 SV **err)
401 {
402 2980 ▶ if (ent->linking || ent->linked)
Branch: 2 branches
BranchTrueFalseTotal
if (ent->linking || ent->linked) 2978 2 2980
if (ent->linking || ent->linked) 2978 0 2978
403 2980 return 1;
404 0 return link_ent(aTHX_ e, ent, err);
405 }
406  
407 /* User (non-builtin) filter names must be registered on the engine;
408 * unknown names are compile-time errors listing what is registered. */
409 1398 static int validate_filters(pTHX_ const stencil_engine *e,
410 const stencil_program *prog,
411 const char *name, SV **err)
412 {
413 1398 const stencil_cfilt *ft = stencil_prog_filts(prog);
414 1398 const char *pool = stencil_prog_pool(prog);
415 uint32_t i;
416 1500 ▶ for (i = 0; i < prog->n_filts; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < prog->n_filts; i++) { 105 1395 1500
417 SV **cvp;
418 105 ▶ if (ft[i].builtin_id >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ft[i].builtin_id >= 0) 71 34 105
419 71 continue;
420 68 cvp = e->filters
421 33 ? hv_fetch(e->filters, pool + ft[i].name_off,
422 (I32)ft[i].name_len, 0)
423 34 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 33 1 34
424 34 ▶ if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV)
Branch: 3 branches
BranchTrueFalseTotal
if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV) 31 3 34
if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV) 31 0 31
if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV) 31 0 31
425 31 continue;
426 3 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 3 0 3
427 3 SV *msg = newSVpvf(
428 "Template::Stencil: %s: unknown filter '%.*s'"
429 " (registered:", name, (int)ft[i].name_len,
430 pool + ft[i].name_off);
431 5 ▶ if (e->filters && HvKEYS(e->filters)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e->filters && HvKEYS(e->filters)) { 2 1 3
if (e->filters && HvKEYS(e->filters)) { 0 2 2
if (e->filters && HvKEYS(e->filters)) { 2 0 2
432 HE *he;
433 2 int first = 1;
434 2 hv_iterinit(e->filters);
435 7 ▶ while ((he = hv_iternext(e->filters))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(e->filters))) { 5 2 7
436 STRLEN klen;
437 5 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 5 5
438 5 ▶ sv_catpvf(msg, "%s %.*s", first ? "" : ",",
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(msg, "%s %.*s", first ? "" : ",", 2 3 5
439 (int)klen, k);
440 5 first = 0;
441 }
442 } else {
443 1 sv_catpvs(msg, " none");
444 }
445 3 sv_catpvs(msg, ")");
446 3 *err = sv_2mortal(msg);
447 }
448 3 return 0;
449 }
450 1395 return 1;
451 }
452  
453 /* Compile flags for a template read from a file. Files are UTF-8 on disk
454 * (ENCODING in the POD), so a file with non-ASCII text compiles as UTF-8
455 * source, exactly as the same text does when passed as a string; a file
456 * that is not valid UTF-8 keeps its byte-for-byte meaning. Pure ASCII
457 * needs no flag. */
458 96 static uint32_t file_compile_flags(pTHX_ const char *src, STRLEN len)
459 {
460 STRLEN i;
461 15478 ▶ for (i = 0; i < len; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) 15388 90 15478
462 15388 ▶ if ((unsigned char)src[i] & 0x80)
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned char)src[i] & 0x80) 6 15382 15388
463 6 return is_utf8_string((const U8 *)src, len)
464 6 ▶ ? STENCIL_PROG_SRC_UTF8 : 0;
Branch: 1 branch
BranchTrueFalseTotal
? STENCIL_PROG_SRC_UTF8 : 0; 5 1 6
465 90 return 0;
466 }
467  
468 1887 static stencil_program *compile_source(pTHX_ stencil_engine *e,
469 const char *src, STRLEN len,
470 const char *name, uint32_t flags,
471 SV **err)
472 {
473 stencil_program *prog;
474 1887 stencil_stat_compiles++;
475 1887 ▶ if (e->flags & STENCIL_EF_NO_FILES)
Branch: 1 branch
BranchTrueFalseTotal
if (e->flags & STENCIL_EF_NO_FILES) 30 1857 1887
476 30 flags |= STENCIL_COMPILE_NO_FILES;
477 1887 prog = stencil_compile(aTHX_ src, len, name, flags, err);
478 1887 ▶ if (prog && !validate_filters(aTHX_ e, prog, name, err)) {
Branch: 2 branches
BranchTrueFalseTotal
if (prog && !validate_filters(aTHX_ e, prog, name, err)) { 1398 489 1887
if (prog && !validate_filters(aTHX_ e, prog, name, err)) { 3 1395 1398
479 3 stencil_program_free(prog);
480 3 return NULL;
481 }
482 1884 return prog;
483 }
484  
485 111 static stencil_cache_ent *get_file_ent(pTHX_ stencil_engine *e,
486 const char *name, size_t nlen,
487 int *notfound, SV **err)
488 {
489 char path[STENCIL_PATH_MAX];
490 struct stat st;
491 uint64_t h;
492 stencil_cache_ent *ent;
493 char *src;
494 STRLEN slen;
495 time_t mtime;
496 Off_t fsize;
497 stencil_program *prog;
498  
499 111 ▶ if (notfound)
Branch: 1 branch
BranchTrueFalseTotal
if (notfound) 111 0 111
500 111 *notfound = 0;
501 111 ▶ if (!resolve_file(e, name, nlen, path, sizeof path, &st)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!resolve_file(e, name, nlen, path, sizeof path, &st)) { 8 103 111
502 8 ▶ if (notfound)
Branch: 1 branch
BranchTrueFalseTotal
if (notfound) 8 0 8
503 8 *notfound = 1;
504 8 return NULL;
505 }
506 103 h = stencil_fnv1a(path, strlen(path));
507 105 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 11 94 105
508 11 ▶ if (ent->abs_path && strcmp(ent->abs_path, path) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (ent->abs_path && strcmp(ent->abs_path, path) == 0) { 9 2 11
if (ent->abs_path && strcmp(ent->abs_path, path) == 0) { 9 0 9
509 9 stencil_stat_cache_hits++;
510 9 ▶ if (!ent_ready(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent, err)) 0 9 9
511 0 return NULL;
512 9 return ent;
513 }
514 }
515  
516 94 src = slurp(aTHX_ path, &slen, &mtime, &fsize, err);
517 94 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 94 94
518 0 return NULL;
519 94 prog = compile_source(aTHX_ e, src, slen, path,
520 file_compile_flags(aTHX_ src, slen), err);
521 94 free(src);
522 94 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 1 93 94
523 1 return NULL;
524  
525 93 ent = (stencil_cache_ent *)calloc(1, sizeof *ent);
526 93 ▶ if (!ent) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ent) { 0 93 93
527 0 stencil_program_free(prog);
528 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
529 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
530 0 return NULL;
531 }
532 93 ent->abs_path = strdup(path);
533 93 ent->path_len = (uint32_t)strlen(path);
534 93 ent->mtime = mtime;
535 93 ent->fsize = fsize;
536 93 ent->checked_at = time(NULL);
537 93 ent->prog = prog;
538 93 ent->next = *bucket_for(e, h);
539 93 *bucket_for(e, h) = ent;
540 93 e->n_ents++;
541 93 e->generation++;
542  
543 93 ▶ if (!link_ent(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!link_ent(aTHX_ e, ent, err)) 5 88 93
544 5 return NULL; /* entry stays cached but unusable; error out */
545 88 return ent;
546 }
547  
548 /* Insert a string-keyed (or alias) entry and enforce the LRU bound. */
549 1365 static stencil_cache_ent *string_ent_insert(pTHX_ stencil_engine *e,
550 const char *src, STRLEN len,
551 uint64_t h,
552 stencil_program *prog,
553 stencil_cache_ent *alias,
554 SV **err)
555 {
556 stencil_cache_ent *ent =
557 1365 (stencil_cache_ent *)calloc(1, sizeof *ent);
558 1365 ▶ if (!ent)
Branch: 1 branch
BranchTrueFalseTotal
if (!ent) 0 1365 1365
559 0 goto oom;
560 1365 ▶ ent->src = (char *)malloc(len ? len : 1);
Branch: 1 branch
BranchTrueFalseTotal
ent->src = (char *)malloc(len ? len : 1); 1363 2 1365
561 1365 ▶ if (!ent->src) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ent->src) { 0 1365 1365
562 0 free(ent);
563 0 goto oom;
564 }
565 1365 memcpy(ent->src, src, len);
566 1365 ent->src_len = len;
567 1365 ent->src_hash = h;
568 1365 ent->prog = prog;
569 1365 ent->alias = alias;
570 1365 ent->next = *bucket_for(e, h);
571 1365 *bucket_for(e, h) = ent;
572 1365 e->n_ents++;
573 1365 e->n_string++;
574 1365 e->generation++;
575 1365 lru_push_front(e, ent);
576 1540 ▶ while (e->n_string > e->cache_size && e->lru_tail
Branch: 1 branch
BranchTrueFalseTotal
while (e->n_string > e->cache_size && e->lru_tail 175 0 175
577 1715 ▶ && e->lru_tail != ent) {
Branch: 2 branches
BranchTrueFalseTotal
&& e->lru_tail != ent) { 175 1365 1540
&& e->lru_tail != ent) { 175 0 175
578 175 stencil_cache_ent *victim = e->lru_tail;
579 175 lru_unlink(e, victim);
580 175 bucket_unlink(e, victim, victim->src_hash);
581 175 ent_free(aTHX_ victim);
582 175 e->n_string--;
583 175 e->n_ents--;
584 175 e->generation++;
585 }
586 1365 return ent;
587 0 oom:
588 0 stencil_program_free(prog);
589 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
590 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
591 0 return NULL;
592 }
593  
594 1791 static stencil_cache_ent *get_string_ent(pTHX_ stencil_engine *e,
595 const char *src, STRLEN len,
596 uint32_t cflags, SV **err)
597 {
598 1791 uint64_t h = stencil_fnv1a(src, len);
599 stencil_cache_ent *ent;
600 stencil_program *prog;
601  
602 3667 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 1876 1791 3667
603 1876 ▶ if (!ent->abs_path && ent->src_hash == h && ent->src_len == len
Branch: 3 branches
BranchTrueFalseTotal
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 1876 0 1876
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 0 1876 1876
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 0 0 0
604 0 ▶ && memcmp(ent->src, src, len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(ent->src, src, len) == 0) { 0 0 0
605 0 stencil_stat_cache_hits++;
606 0 lru_unlink(e, ent);
607 0 lru_push_front(e, ent);
608 0 ▶ if (!ent_ready(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent, err)) 0 0 0
609 0 return NULL;
610 0 return ent;
611 }
612 }
613  
614 1791 prog = compile_source(aTHX_ e, src, len, "<string>", cflags, err);
615 1791 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 491 1300 1791
616 491 return NULL;
617 1300 ent = string_ent_insert(aTHX_ e, src, len, h, prog, NULL, err);
618 1300 ▶ if (!ent)
Branch: 1 branch
BranchTrueFalseTotal
if (!ent) 0 1300 1300
619 0 return NULL;
620 1300 ▶ if (!link_ent(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!link_ent(aTHX_ e, ent, err)) 6 1294 1300
621 6 return NULL;
622 1294 return ent;
623 }
624  
625 /* Name-keyed lookup of a file entry (wrapper fetch): an alias hit
626 * costs zero syscalls; a miss resolves and caches the alias. */
627 140 static stencil_cache_ent *get_named_file_ent(pTHX_ stencil_engine *e,
628 const char *name,
629 size_t nlen, SV **err)
630 {
631 140 uint64_t h = stencil_fnv1a(name, nlen);
632 stencil_cache_ent *ent, *file;
633  
634 142 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 120 22 142
635 120 ▶ if (!ent->abs_path && ent->src_hash == h && ent->src_len == nlen
Branch: 3 branches
BranchTrueFalseTotal
if (!ent->abs_path && ent->src_hash == h && ent->src_len == nlen 120 0 120
if (!ent->abs_path && ent->src_hash == h && ent->src_len == nlen 118 2 120
if (!ent->abs_path && ent->src_hash == h && ent->src_len == nlen 118 0 118
636 118 ▶ && memcmp(ent->src, name, nlen) == 0 && ent->alias) {
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(ent->src, name, nlen) == 0 && ent->alias) { 118 0 118
&& memcmp(ent->src, name, nlen) == 0 && ent->alias) { 118 0 118
637 118 stencil_stat_cache_hits++;
638 118 lru_unlink(e, ent);
639 118 lru_push_front(e, ent);
640 118 ▶ if (!ent_ready(aTHX_ e, ent->alias, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent->alias, err)) 0 118 118
641 0 return NULL;
642 118 return ent->alias;
643 }
644 }
645 {
646 22 int notfound = 0;
647 22 file = get_file_ent(aTHX_ e, name, nlen, &notfound, err);
648 22 ▶ if (!file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file) { 0 22 22
649 0 ▶ if (notfound && err)
Branch: 2 branches
BranchTrueFalseTotal
if (notfound && err) 0 0 0
if (notfound && err) 0 0 0
650 0 *err = sv_2mortal(newSVpvf(
651 "Template::Stencil: cannot find template '%.*s'",
652 (int)nlen, name));
653 0 return NULL;
654 }
655 }
656 22 ▶ if (!string_ent_insert(aTHX_ e, name, nlen, h, NULL, file, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!string_ent_insert(aTHX_ e, name, nlen, h, NULL, file, err)) 0 22 22
657 0 return NULL;
658 22 return file;
659 }
660  
661 /* Page dispatch: one cache lookup on the raw template argument. A hit
662 * on an alias entry reaches the file entry with zero syscalls (the
663 * stat_ttl => -1 steady state); a miss decides string-vs-file per the
664 * overview rule and caches the decision. */
665 4692 static stencil_cache_ent *get_page_ent(pTHX_ stencil_engine *e,
666 const char *p, STRLEN len,
667 uint32_t cflags, SV **err)
668 {
669 4692 uint64_t h = stencil_fnv1a(p, len);
670 stencil_cache_ent *ent;
671  
672 6573 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 4734 1839 6573
673 4734 ▶ if (!ent->abs_path && ent->src_hash == h && ent->src_len == len
Branch: 3 branches
BranchTrueFalseTotal
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 4734 0 4734
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 2853 1881 4734
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 2853 0 2853
674 2853 ▶ && memcmp(ent->src, p, len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(ent->src, p, len) == 0) { 2853 0 2853
675 2853 stencil_stat_cache_hits++;
676 2853 lru_unlink(e, ent);
677 2853 lru_push_front(e, ent);
678 2853 ▶ if (!ent_ready(aTHX_ e, ent->alias ? ent->alias : ent, err))
Branch: 2 branches
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent->alias ? ent->alias : ent, err)) 78 2775 2853
if (!ent_ready(aTHX_ e, ent->alias ? ent->alias : ent, err)) 0 2853 2853
679 0 return NULL;
680 2853 ▶ return ent->alias ? ent->alias : ent;
Branch: 1 branch
BranchTrueFalseTotal
return ent->alias ? ent->alias : ent; 78 2775 2853
681 }
682 }
683  
684 /* files => 0: a source is a source, never a file's name */
685 1839 ▶ if (!(e->flags & STENCIL_EF_NO_FILES) && !looks_like_source(p, len)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!(e->flags & STENCIL_EF_NO_FILES) && !looks_like_source(p, len)) { 1809 30 1839
if (!(e->flags & STENCIL_EF_NO_FILES) && !looks_like_source(p, len)) { 302 1507 1809
686 char path[STENCIL_PATH_MAX];
687 struct stat st;
688 302 ▶ if (resolve_file(e, p, len, path, sizeof path, &st)) {
Branch: 1 branch
BranchTrueFalseTotal
if (resolve_file(e, p, len, path, sizeof path, &st)) { 48 254 302
689 48 int notfound = 0;
690 stencil_cache_ent *file =
691 48 get_file_ent(aTHX_ e, p, len, &notfound, err);
692 48 ▶ if (!file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file) { 5 43 48
693 5 ▶ if (notfound && err) /* raced away; treat as error */
Branch: 2 branches
BranchTrueFalseTotal
if (notfound && err) /* raced away; treat as error */ 0 5 5
if (notfound && err) /* raced away; treat as error */ 0 0 0
694 0 *err = sv_2mortal(newSVpvf(
695 "Template::Stencil: cannot find template "
696 "'%.*s'", (int)len, p));
697 5 return NULL;
698 }
699 43 ▶ if (!string_ent_insert(aTHX_ e, p, len, h, NULL, file, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!string_ent_insert(aTHX_ e, p, len, h, NULL, file, err)) 0 43 43
700 0 return NULL;
701 43 return file;
702 }
703 }
704 1791 return get_string_ent(aTHX_ e, p, len, cflags, err);
705 }
706  
707 /* ---- revalidation and effective sizes --------------------------------- */
708  
709 4470 static int revalidate_ent(pTHX_ stencil_engine *e, stencil_cache_ent *ent,
710 SV **err)
711 {
712 struct stat st;
713 time_t now;
714 4470 ▶ if (!ent->abs_path || e->stat_ttl < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!ent->abs_path || e->stat_ttl < 0) 402 4068 4470
if (!ent->abs_path || e->stat_ttl < 0) 218 184 402
715 4286 return 1;
716 184 now = time(NULL);
717 184 ▶ if (e->stat_ttl > 0
Branch: 1 branch
BranchTrueFalseTotal
if (e->stat_ttl > 0 178 6 184
718 178 ▶ && (double)(now - ent->checked_at) <= e->stat_ttl)
Branch: 1 branch
BranchTrueFalseTotal
&& (double)(now - ent->checked_at) <= e->stat_ttl) 178 0 178
719 178 return 1;
720 6 ent->checked_at = now;
721 6 stencil_stat_stats++;
722 6 ▶ if (stat(ent->abs_path, &st) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(ent->abs_path, &st) != 0) { 0 6 6
723 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
724 0 *err = sv_2mortal(newSVpvf(
725 "Template::Stencil: template '%s' disappeared",
726 ent->abs_path));
727 0 return 0;
728 }
729 6 ▶ if (st.st_mtime == ent->mtime && st.st_size == ent->fsize)
Branch: 2 branches
BranchTrueFalseTotal
if (st.st_mtime == ent->mtime && st.st_size == ent->fsize) 4 2 6
if (st.st_mtime == ent->mtime && st.st_size == ent->fsize) 4 0 4
730 4 return 1;
731 {
732 char *src;
733 STRLEN slen;
734 time_t mtime;
735 Off_t fsize;
736 stencil_program *prog;
737 2 src = slurp(aTHX_ ent->abs_path, &slen, &mtime, &fsize, err);
738 2 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 2 2
739 0 return 0;
740 2 prog = compile_source(aTHX_ e, src, slen, ent->abs_path,
741 file_compile_flags(aTHX_ src, slen), err);
742 2 free(src);
743 2 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 0 2 2
744 0 return 0;
745 2 stencil_program_free(ent->prog);
746 2 ent->prog = prog;
747 2 ent->mtime = mtime;
748 2 ent->fsize = fsize;
749 2 e->generation++;
750 2 return link_ent(aTHX_ e, ent, err);
751 }
752 }
753  
754 /* Revalidate every node of an include graph once per render entry. The
755 * visit serial keeps shared includes from being stat'd twice. */
756 4471 static int revalidate_graph(pTHX_ stencil_engine *e,
757 stencil_cache_ent *ent, uint32_t visit,
758 SV **err)
759 {
760 uint32_t i;
761 4471 ▶ if (ent->seen == visit)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->seen == visit) 1 4470 4471
762 1 return 1;
763 4470 ent->seen = visit;
764 4470 ▶ if (!revalidate_ent(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!revalidate_ent(aTHX_ e, ent, err)) 0 4470 4470
765 0 return 0;
766 4614 ▶ for (i = 0; i < ent->prog->n_incs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ent->prog->n_incs; i++) 144 4470 4614
767 144 ▶ if (!revalidate_graph(aTHX_ e, ent->incs[i], visit, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!revalidate_graph(aTHX_ e, ent->incs[i], visit, err)) 0 144 144
768 0 return 0;
769 4470 return 1;
770 }
771  
772 /* Conservative render-state sizes across the include graph (sum along
773 * the worst nesting), memoised per generation. */
774 4360 static void ent_eff(stencil_engine *e, stencil_cache_ent *ent)
775 {
776 uint32_t s, f, b, i;
777 4360 uint32_t cs = 0, cf = 0, cb = 0;
778 4360 ▶ if (ent->eff_gen == e->generation)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->eff_gen == e->generation) 2929 1431 4360
779 2929 return;
780 1431 ent->eff_gen = e->generation;
781 1431 s = ent->prog->max_stack;
782 1431 f = ent->prog->max_frames;
783 1431 b = ent->prog->max_binds;
784 1464 ▶ for (i = 0; i < ent->prog->n_incs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ent->prog->n_incs; i++) { 33 1431 1464
785 33 stencil_cache_ent *ch = ent->incs[i];
786 33 ent_eff(e, ch);
787 33 ▶ if (ch->eff_stack > cs) cs = ch->eff_stack;
Branch: 1 branch
BranchTrueFalseTotal
if (ch->eff_stack > cs) cs = ch->eff_stack; 30 3 33
788 33 ▶ if (ch->eff_frames > cf) cf = ch->eff_frames;
Branch: 1 branch
BranchTrueFalseTotal
if (ch->eff_frames > cf) cf = ch->eff_frames; 0 33 33
789 33 ▶ if (ch->eff_binds > cb) cb = ch->eff_binds;
Branch: 1 branch
BranchTrueFalseTotal
if (ch->eff_binds > cb) cb = ch->eff_binds; 0 33 33
790 }
791 1431 ent->eff_stack = s + cs;
792 1431 ent->eff_frames = f + cf;
793 1431 ent->eff_binds = b + cb;
794 }
795  
796 /* ---- engine lifecycle ------------------------------------------------- */
797  
798 326 stencil_engine *stencil_engine_new(pTHX_ const char *template_dir,
799 const char *wrapper, uint32_t flags,
800 double stat_ttl, uint32_t cache_size,
801 HV *filters)
802 {
803 326 stencil_engine *e = (stencil_engine *)calloc(1, sizeof *e);
804 326 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 0 326 326
805 0 return NULL;
806 326 ▶ if (template_dir && *template_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (template_dir && *template_dir) { 59 267 326
if (template_dir && *template_dir) { 59 0 59
807 59 size_t len = strlen(template_dir);
808 59 ▶ while (len > 1 && template_dir[len - 1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (len > 1 && template_dir[len - 1] == '/') 59 0 59
while (len > 1 && template_dir[len - 1] == '/') 0 59 59
809 0 len--;
810 59 e->template_dir = (char *)malloc(len + 1);
811 59 memcpy(e->template_dir, template_dir, len);
812 59 e->template_dir[len] = '\0';
813 59 e->template_dir_len = len;
814 }
815 326 ▶ if (wrapper && *wrapper)
Branch: 2 branches
BranchTrueFalseTotal
if (wrapper && *wrapper) 16 310 326
if (wrapper && *wrapper) 16 0 16
816 16 e->wrapper = strdup(wrapper);
817 326 e->flags = flags;
818 326 e->stat_ttl = stat_ttl;
819 326 ▶ e->cache_size = cache_size ? cache_size : 256;
Branch: 1 branch
BranchTrueFalseTotal
e->cache_size = cache_size ? cache_size : 256; 326 0 326
820 326 e->n_buckets = STENCIL_N_BUCKETS;
821 326 e->buckets = (stencil_cache_ent **)
822 326 calloc(e->n_buckets, sizeof(stencil_cache_ent *));
823 326 stencil_stat_engines++;
824 326 e->generation = 1;
825 326 ▶ if (filters)
Branch: 1 branch
BranchTrueFalseTotal
if (filters) 19 307 326
826 19 e->filters = (HV *)SvREFCNT_inc((SV *)filters);
827 326 return e;
828 }
829  
830 326 void stencil_engine_free(pTHX_ stencil_engine *e)
831 {
832 326 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 0 326 326
833 0 return;
834 326 engine_cache_clear(aTHX_ e);
835 326 free(e->buckets);
836 326 free(e->template_dir);
837 326 free(e->wrapper);
838 /* during global destruction perl reclaims the filters HV itself */
839 326 ▶ if (e->filters && !PL_dirty)
Branch: 2 branches
BranchTrueFalseTotal
if (e->filters && !PL_dirty) 19 307 326
if (e->filters && !PL_dirty) 13 6 19
840 13 SvREFCNT_dec((SV *)e->filters);
841 326 free(e->rx_cache);
842 326 free(e);
843 326 stencil_stat_engines--;
844 }
845  
846 /* ---- render entry ------------------------------------------------------ */
847  
848 1809 static int looks_like_source(const char *p, STRLEN len)
849 {
850 const char *q, *end;
851 1809 ▶ if (memchr(p, '\n', len))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(p, '\n', len)) 159 1650 1809
852 159 return 1;
853 1650 ▶ if (len >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 2) { 1642 8 1650
854 1642 end = p + len - 1;
855 1642 for (q = p; (q = (const char *)memchr(q, '{',
856 1708 ▶ (size_t)(end - q))) != NULL; q++)
Branch: 1 branch
BranchTrueFalseTotal
(size_t)(end - q))) != NULL; q++) 1414 294 1708
857 1414 ▶ if (q[1] == '%')
Branch: 1 branch
BranchTrueFalseTotal
if (q[1] == '%') 1348 66 1414
858 1348 return 1;
859 }
860 302 return 0;
861 }
862  
863 4694 SV *stencil_engine_render(pTHX_ stencil_engine *e, SV *tmpl, HV *data,
864 SV *opts, SV **err)
865 {
866 STRLEN len;
867 4694 const char *p = SvPV_const(tmpl, len);
868 4694 stencil_cache_ent *page = NULL, *wrap = NULL;
869 4694 const char *wrap_name = e->wrapper;
870 SV *out;
871 uint32_t eff_s, eff_f, eff_b;
872 4694 uint32_t rf = e->flags & (STENCIL_RF_STRICT
873 | STENCIL_RF_NO_SORT_KEYS
874 | STENCIL_RF_NO_ESCAPE
875 | STENCIL_RF_CHARS
876 | STENCIL_RF_PRETTY);
877  
878 /* the engine owns encoding: a latin-1-repped template string is
879 * upgraded so compiled literals are always UTF-8 bytes */
880 4694 ▶ if (!SvUTF8(tmpl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(tmpl)) { 3156 1538 4694
881 STRLEN i;
882 1158105 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 1155474 2631 1158105
883 1155474 ▶ if ((unsigned char)p[i] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned char)p[i] & 0x80) { 525 1154949 1155474
884 525 tmpl = sv_mortalcopy(tmpl);
885 525 sv_utf8_upgrade(tmpl);
886 525 p = SvPV_const(tmpl, len);
887 525 break;
888 }
889 }
890 }
891  
892 4694 ▶ if (opts && SvOK(opts)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts && SvOK(opts)) { 861 3833 4694
if (opts && SvOK(opts)) { 861 0 861
893 HV *oh;
894 HE *he;
895 861 ▶ if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) { 861 0 861
if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) { 1 860 861
896 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
897 1 *err = sv_2mortal(newSVpvs(
898 "Template::Stencil: render options must be a hashref"));
899 1 return NULL;
900 }
901 860 oh = (HV *)SvRV(opts);
902 860 hv_iterinit(oh);
903 984 ▶ while ((he = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(oh))) { 125 859 984
904 STRLEN klen;
905 125 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 125 125
906 125 SV *v = HeVAL(he);
907 125 ▶ if (klen == 7 && memEQ(k, "wrapper", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memEQ(k, "wrapper", 7)) { 116 9 125
if (klen == 7 && memEQ(k, "wrapper", 7)) { 116 0 116
908 228 ▶ wrap_name = SvOK(v) ? SvPV_nolen(v) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
wrap_name = SvOK(v) ? SvPV_nolen(v) : NULL; 112 4 116
909 9 ▶ } else if (klen == 6 && memEQ(k, "strict", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 6 && memEQ(k, "strict", 6)) { 8 1 9
} else if (klen == 6 && memEQ(k, "strict", 6)) { 5 3 8
910 10 ▶ if (SvTRUE(v))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(v)) 3 2 5
911 3 rf |= STENCIL_RF_STRICT;
912 else
913 2 rf &= ~STENCIL_RF_STRICT;
914 4 ▶ } else if (klen == 6 && memEQ(k, "pretty", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
} else if (klen == 6 && memEQ(k, "pretty", 6)) { 3 1 4
} else if (klen == 6 && memEQ(k, "pretty", 6)) { 3 0 3
915 6 ▶ if (SvTRUE(v))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(v)) 2 1 3
916 2 rf |= STENCIL_RF_PRETTY;
917 else
918 1 rf &= ~STENCIL_RF_PRETTY;
919 } else {
920 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
921 1 *err = sv_2mortal(newSVpvf(
922 "Template::Stencil: unknown render option "
923 "'%.*s'", (int)klen, k));
924 1 return NULL;
925 }
926 }
927 }
928  
929 4692 page = get_page_ent(aTHX_ e, p, len,
930 4692 SvUTF8(tmpl) ? STENCIL_PROG_SRC_UTF8 : 0, err);
931 4692 ▶ if (!page)
Branch: 1 branch
BranchTrueFalseTotal
if (!page) 502 4190 4692
932 502 return NULL;
933  
934 4190 ▶ if (wrap_name && (e->flags & STENCIL_EF_NO_FILES)) {
Branch: 2 branches
BranchTrueFalseTotal
if (wrap_name && (e->flags & STENCIL_EF_NO_FILES)) { 141 4049 4190
if (wrap_name && (e->flags & STENCIL_EF_NO_FILES)) { 1 140 141
935 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
936 1 *err = sv_2mortal(newSVpvs(
937 "Template::Stencil: a wrapper is refused: this engine reads "
938 "no files (files => 0)"));
939 1 return NULL;
940 }
941 4189 ▶ if (wrap_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrap_name) { 140 4049 4189
942 140 wrap = get_named_file_ent(aTHX_ e, wrap_name, strlen(wrap_name),
943 err);
944 140 ▶ if (!wrap)
Branch: 1 branch
BranchTrueFalseTotal
if (!wrap) 0 140 140
945 0 return NULL;
946 140 ▶ if (!(wrap->prog->flags & STENCIL_PROG_IS_WRAPPER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(wrap->prog->flags & STENCIL_PROG_IS_WRAPPER)) { 1 139 140
947 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
948 1 *err = sv_2mortal(newSVpvf(
949 "Template::Stencil: wrapper '%s' has no "
950 "{%% content %%}", ent_name(wrap)));
951 1 return NULL;
952 }
953 }
954  
955 {
956 static uint32_t visit_serial = 0;
957 4188 uint32_t visit = ++visit_serial;
958 4188 ▶ if (!revalidate_graph(aTHX_ e, page, visit, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!revalidate_graph(aTHX_ e, page, visit, err)) 0 4188 4188
959 0 return NULL;
960 4188 ▶ if (wrap && !revalidate_graph(aTHX_ e, wrap, visit, err))
Branch: 2 branches
BranchTrueFalseTotal
if (wrap && !revalidate_graph(aTHX_ e, wrap, visit, err)) 139 4049 4188
if (wrap && !revalidate_graph(aTHX_ e, wrap, visit, err)) 0 139 139
961 0 return NULL;
962 }
963 4188 ent_eff(e, page);
964 4188 eff_s = page->eff_stack;
965 4188 eff_f = page->eff_frames;
966 4188 eff_b = page->eff_binds;
967 4188 ▶ if (wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrap) { 139 4049 4188
968 139 ent_eff(e, wrap);
969 139 eff_s += wrap->eff_stack;
970 139 eff_f += wrap->eff_frames;
971 139 eff_b += wrap->eff_binds;
972 }
973  
974 4188 ▶ out = stencil_render_core(aTHX_
Branch: 2 branches
BranchTrueFalseTotal
out = stencil_render_core(aTHX_ 139 4049 4188
out = stencil_render_core(aTHX_ 139 4049 4188
975 page->prog, page->incs, ent_name(page),
976 wrap ? wrap->prog : NULL, wrap ? wrap->incs : NULL,
977 139 wrap ? ent_name(wrap) : NULL,
978 4188 ▶ eff_s, eff_f, eff_b, data, e->filters, &e->rx_cache, rf, err);
Branch: 1 branch
BranchTrueFalseTotal
eff_s, eff_f, eff_b, data, e->filters, &e->rx_cache, rf, err); 139 4049 4188
979  
980 4188 ▶ if (out && (rf & STENCIL_RF_PRETTY)) {
Branch: 2 branches
BranchTrueFalseTotal
if (out && (rf & STENCIL_RF_PRETTY)) { 4157 31 4188
if (out && (rf & STENCIL_RF_PRETTY)) { 9 4148 4157
981 9 SV *dense = pretty_strip_blank_lines(aTHX_ out);
982 9 SV *shaped = eshu_prettify(aTHX_ dense, err);
983 9 SvREFCNT_dec(out);
984 9 SvREFCNT_dec(dense);
985 9 ▶ if (!shaped)
Branch: 1 branch
BranchTrueFalseTotal
if (!shaped) 0 9 9
986 0 out = NULL;
987 else {
988 9 ▶ if (!(rf & STENCIL_RF_CHARS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(rf & STENCIL_RF_CHARS)) 9 0 9
989 9 SvUTF8_off(shaped);
990 9 out = shaped;
991 }
992 }
993  
994 4188 ▶ if (e->flags & STENCIL_EF_NO_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if (e->flags & STENCIL_EF_NO_CACHE) 3 4185 4188
995 3 engine_cache_clear(aTHX_ e);
996 4188 return out;
997 }
« src/stencil_arena.c ↑ Summary Stencil.xs »