src/stencil_engine.c

filter: Statementstmt 87.4% 495 / 566 Branchbran 72.3% 321 / 444 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 80.7% 816 / 1010 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 1886 static const char *ent_name(const stencil_cache_ent *ent)
46 {
47 1886 ▶ return ent->abs_path ? ent->abs_path : "<string>";
Branch: 1 branch
BranchTrueFalseTotal
return ent->abs_path ? ent->abs_path : "<string>"; 244 1642 1886
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 369 static int name_unsafe(const char *n, size_t len)
146 {
147 369 size_t i = 0;
148 369 ▶ if (!len || n[0] == '/' || n[0] == '\\')
Branch: 3 branches
BranchTrueFalseTotal
if (!len || n[0] == '/' || n[0] == '\\') 367 2 369
if (!len || n[0] == '/' || n[0] == '\\') 365 2 367
if (!len || n[0] == '/' || n[0] == '\\') 2 363 365
149 6 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 876 ▶ while (i < len) {
Branch: 1 branch
BranchTrueFalseTotal
while (i < len) { 518 358 876
156 518 size_t j = i;
157 128841 ▶ while (j < len && n[j] != '/' && n[j] != '\\')
Branch: 3 branches
BranchTrueFalseTotal
while (j < len && n[j] != '/' && n[j] != '\\') 128485 356 128841
while (j < len && n[j] != '/' && n[j] != '\\') 128397 88 128485
while (j < len && n[j] != '/' && n[j] != '\\') 128323 74 128397
158 128323 j++;
159 518 ▶ if (j - i == 2 && n[i] == '.' && n[i + 1] == '.')
Branch: 3 branches
BranchTrueFalseTotal
if (j - i == 2 && n[i] == '.' && n[i + 1] == '.') 12 506 518
if (j - i == 2 && n[i] == '.' && n[i + 1] == '.') 5 7 12
if (j - i == 2 && n[i] == '.' && n[i + 1] == '.') 5 0 5
160 5 return 1;
161 513 i = j + 1;
162 }
163 358 return 0;
164 }
165  
166 369 static int final_seg_has_dot(const char *n, size_t len)
167 {
168 369 size_t i = len;
169 118262 ▶ while (i > 0 && !STENCIL_IS_SEP(n[i - 1])) {
Branch: 2 branches
BranchTrueFalseTotal
while (i > 0 && !STENCIL_IS_SEP(n[i - 1])) { 118094 168 118262
while (i > 0 && !STENCIL_IS_SEP(n[i - 1])) { 118041 53 118094
170 118041 ▶ if (n[i - 1] == '.')
Branch: 1 branch
BranchTrueFalseTotal
if (n[i - 1] == '.') 148 117893 118041
171 148 return 1;
172 117893 i--;
173 }
174 221 return 0;
175 }
176  
177 605 static int try_stat(const char *path, struct stat *st)
178 {
179 605 stencil_stat_stats++;
180 605 ▶ return stat(path, st) == 0 && S_ISREG(st->st_mode);
Branch: 2 branches
BranchTrueFalseTotal
return stat(path, st) == 0 && S_ISREG(st->st_mode); 120 485 605
return stat(path, st) == 0 && S_ISREG(st->st_mode); 120 0 120
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 369 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 369 int has_dot = final_seg_has_dot(name, nlen);
196 369 ▶ if (name_unsafe(name, nlen))
Branch: 1 branch
BranchTrueFalseTotal
if (name_unsafe(name, nlen)) 11 358 369
197 11 return 0;
198 358 ▶ if (e->template_dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (e->template_dir) { 132 226 358
199 132 snprintf(out, outsz, "%s/%.*s", e->template_dir, (int)nlen, name);
200 132 out[outsz - 1] = '\0';
201 132 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 84 48 132
202 84 return 1;
203 48 ▶ if (!has_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_dot) { 45 3 48
204 45 snprintf(out, outsz, "%s/%.*s.tmpl", e->template_dir,
205 (int)nlen, name);
206 45 out[outsz - 1] = '\0';
207 45 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 32 13 45
208 32 return 1;
209 }
210 }
211 242 snprintf(out, outsz, "%.*s", (int)nlen, name);
212 242 out[outsz - 1] = '\0';
213 242 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 0 242 242
214 0 return 1;
215 242 ▶ if (!has_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (!has_dot) { 186 56 242
216 186 snprintf(out, outsz, "%.*s.tmpl", (int)nlen, name);
217 186 out[outsz - 1] = '\0';
218 186 ▶ if (try_stat(out, st))
Branch: 1 branch
BranchTrueFalseTotal
if (try_stat(out, st)) 4 182 186
219 4 return 1;
220 }
221 238 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 78 static char *slurp(pTHX_ const char *path, STRLEN *lenp, time_t *mtime,
227 Off_t *fsize, SV **err)
228 {
229 struct stat st;
230 78 char *buf = NULL;
231 SSize_t got;
232 78 size_t have = 0;
233 78 int fd = open(path, O_RDONLY | O_BINARY);
234 78 ▶ 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)) { 78 0 78
if (fd < 0 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { 78 0 78
if (fd < 0 || fstat(fd, &st) != 0 || !S_ISREG(st.st_mode)) { 0 78 78
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 78 *mtime = st.st_mtime;
243 78 *fsize = st.st_size;
244 78 buf = (char *)malloc((size_t)st.st_size + 1);
245 78 ▶ if (!buf) {
Branch: 1 branch
BranchTrueFalseTotal
if (!buf) { 0 78 78
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 156 ▶ while (have < (size_t)st.st_size) {
Branch: 1 branch
BranchTrueFalseTotal
while (have < (size_t)st.st_size) { 78 78 156
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 78 size_t want = (size_t)st.st_size - have;
256 78 ▶ if (want > 0x7ffff000u)
Branch: 1 branch
BranchTrueFalseTotal
if (want > 0x7ffff000u) 0 78 78
257 0 want = 0x7ffff000u;
258 78 got = read(fd, buf + have, (unsigned int)want);
259 78 ▶ if (got <= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) 0 78 78
260 0 break;
261 78 have += (size_t)got;
262 }
263 78 close(fd);
264 78 buf[have] = '\0';
265 78 *lenp = (STRLEN)have;
266 78 return buf;
267 }
268  
269 /* ---- cache table ------------------------------------------------------ */
270  
271 5023 static stencil_cache_ent **bucket_for(stencil_engine *e, uint64_t h)
272 {
273 5023 return &e->buckets[(uint32_t)h & (e->n_buckets - 1)];
274 }
275  
276 1178 static void lru_unlink(stencil_engine *e, stencil_cache_ent *ent)
277 {
278 1178 ▶ if (ent->lru_prev)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->lru_prev) 317 861 1178
279 317 ent->lru_prev->lru_next = ent->lru_next;
280 861 ▶ else if (e->lru_head == ent)
Branch: 1 branch
BranchTrueFalseTotal
else if (e->lru_head == ent) 861 0 861
281 861 e->lru_head = ent->lru_next;
282 1178 ▶ if (ent->lru_next)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->lru_next) 796 382 1178
283 796 ent->lru_next->lru_prev = ent->lru_prev;
284 382 ▶ else if (e->lru_tail == ent)
Branch: 1 branch
BranchTrueFalseTotal
else if (e->lru_tail == ent) 382 0 382
285 382 e->lru_tail = ent->lru_prev;
286 1178 ent->lru_next = ent->lru_prev = NULL;
287 1178 }
288  
289 1879 static void lru_push_front(stencil_engine *e, stencil_cache_ent *ent)
290 {
291 1879 ent->lru_prev = NULL;
292 1879 ent->lru_next = e->lru_head;
293 1879 ▶ if (e->lru_head)
Branch: 1 branch
BranchTrueFalseTotal
if (e->lru_head) 1306 573 1879
294 1306 e->lru_head->lru_prev = ent;
295 1879 e->lru_head = ent;
296 1879 ▶ if (!e->lru_tail)
Branch: 1 branch
BranchTrueFalseTotal
if (!e->lru_tail) 573 1306 1879
297 573 e->lru_tail = ent;
298 1879 }
299  
300 854 static void ent_free(pTHX_ stencil_cache_ent *ent)
301 {
302 854 free(ent->abs_path);
303 854 free(ent->src);
304 854 ▶ if (ent->incs)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->incs) 33 821 854
305 33 Safefree(ent->incs);
306 854 stencil_program_free(ent->prog);
307 854 free(ent);
308 854 }
309  
310 78 static void bucket_unlink(stencil_engine *e, stencil_cache_ent *ent,
311 uint64_t h)
312 {
313 78 stencil_cache_ent **pp = bucket_for(e, h);
314 233 ▶ while (*pp && *pp != ent)
Branch: 2 branches
BranchTrueFalseTotal
while (*pp && *pp != ent) 233 0 233
while (*pp && *pp != ent) 155 78 233
315 155 pp = &(*pp)->next;
316 78 ▶ if (*pp)
Branch: 1 branch
BranchTrueFalseTotal
if (*pp) 78 0 78
317 78 *pp = ent->next;
318 78 }
319  
320 299 static void engine_cache_clear(pTHX_ stencil_engine *e)
321 {
322 uint32_t i;
323 38571 ▶ for (i = 0; i < e->n_buckets; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < e->n_buckets; i++) { 38272 299 38571
324 38272 stencil_cache_ent *ent = e->buckets[i];
325 39048 ▶ while (ent) {
Branch: 1 branch
BranchTrueFalseTotal
while (ent) { 776 38272 39048
326 776 stencil_cache_ent *next = ent->next;
327 776 ent_free(aTHX_ ent);
328 776 ent = next;
329 }
330 38272 e->buckets[i] = NULL;
331 }
332 299 e->lru_head = e->lru_tail = NULL;
333 299 e->n_ents = e->n_string = 0;
334 299 e->generation++;
335 299 }
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 803 static int link_ent(pTHX_ stencil_engine *e, stencil_cache_ent *ent,
344 SV **err)
345 {
346 803 stencil_program *prog = ent->prog;
347 803 const stencil_cinc *incs = stencil_prog_incs(prog);
348 803 const char *pool = stencil_prog_pool(prog);
349 803 uint32_t n = prog->n_incs, i;
350  
351 803 ent->linked = 0;
352 803 ▶ if (ent->incs) {
Branch: 1 branch
BranchTrueFalseTotal
if (ent->incs) { 0 803 803
353 0 Safefree(ent->incs);
354 0 ent->incs = NULL;
355 }
356 803 ▶ if (!n) {
Branch: 1 branch
BranchTrueFalseTotal
if (!n) { 770 33 803
357 770 ent->linked = 1;
358 770 return 1;
359 }
360 33 Newxz(ent->incs, n, stencil_cache_ent *);
361 33 ent->linking = 1;
362 55 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 33 22 55
363 33 int notfound = 0;
364 stencil_cache_ent *child =
365 33 get_file_ent(aTHX_ e, pool + incs[i].name_off,
366 33 incs[i].name_len, &notfound, err);
367 33 ▶ if (!child) {
Branch: 1 branch
BranchTrueFalseTotal
if (!child) { 9 24 33
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 24 ▶ if (child->linking) {
Branch: 1 branch
BranchTrueFalseTotal
if (child->linking) { 2 22 24
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 22 ent->incs[i] = child;
390 }
391 22 ent->linking = 0;
392 22 ent->linked = 1;
393 22 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 1107 static int ent_ready(pTHX_ stencil_engine *e, stencil_cache_ent *ent,
400 SV **err)
401 {
402 1107 ▶ if (ent->linking || ent->linked)
Branch: 2 branches
BranchTrueFalseTotal
if (ent->linking || ent->linked) 1105 2 1107
if (ent->linking || ent->linked) 1105 0 1105
403 1107 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 805 static int validate_filters(pTHX_ const stencil_engine *e,
410 const stencil_program *prog,
411 const char *name, SV **err)
412 {
413 805 const stencil_cfilt *ft = stencil_prog_filts(prog);
414 805 const char *pool = stencil_prog_pool(prog);
415 uint32_t i;
416 879 ▶ for (i = 0; i < prog->n_filts; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < prog->n_filts; i++) { 76 803 879
417 SV **cvp;
418 76 ▶ if (ft[i].builtin_id >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (ft[i].builtin_id >= 0) 61 15 76
419 61 continue;
420 30 cvp = e->filters
421 14 ? hv_fetch(e->filters, pool + ft[i].name_off,
422 (I32)ft[i].name_len, 0)
423 15 ▶ : NULL;
Branch: 1 branch
BranchTrueFalseTotal
: NULL; 14 1 15
424 15 ▶ if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV)
Branch: 3 branches
BranchTrueFalseTotal
if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV) 13 2 15
if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV) 13 0 13
if (cvp && SvROK(*cvp) && SvTYPE(SvRV(*cvp)) == SVt_PVCV) 13 0 13
425 13 continue;
426 2 ▶ if (err) {
Branch: 1 branch
BranchTrueFalseTotal
if (err) { 2 0 2
427 2 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 3 ▶ if (e->filters && HvKEYS(e->filters)) {
Branch: 3 branches
BranchTrueFalseTotal
if (e->filters && HvKEYS(e->filters)) { 1 1 2
if (e->filters && HvKEYS(e->filters)) { 0 1 1
if (e->filters && HvKEYS(e->filters)) { 1 0 1
432 HE *he;
433 1 int first = 1;
434 1 hv_iterinit(e->filters);
435 5 ▶ while ((he = hv_iternext(e->filters))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(e->filters))) { 4 1 5
436 STRLEN klen;
437 4 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 4 4
438 4 ▶ sv_catpvf(msg, "%s %.*s", first ? "" : ",",
Branch: 1 branch
BranchTrueFalseTotal
sv_catpvf(msg, "%s %.*s", first ? "" : ",", 1 3 4
439 (int)klen, k);
440 4 first = 0;
441 }
442 } else {
443 1 sv_catpvs(msg, " none");
444 }
445 2 sv_catpvs(msg, ")");
446 2 *err = sv_2mortal(msg);
447 }
448 2 return 0;
449 }
450 803 return 1;
451 }
452  
453 1076 static stencil_program *compile_source(pTHX_ stencil_engine *e,
454 const char *src, STRLEN len,
455 const char *name, uint32_t flags,
456 SV **err)
457 {
458 stencil_program *prog;
459 1076 stencil_stat_compiles++;
460 1076 prog = stencil_compile(aTHX_ src, len, name, flags, err);
461 1076 ▶ if (prog && !validate_filters(aTHX_ e, prog, name, err)) {
Branch: 2 branches
BranchTrueFalseTotal
if (prog && !validate_filters(aTHX_ e, prog, name, err)) { 805 271 1076
if (prog && !validate_filters(aTHX_ e, prog, name, err)) { 2 803 805
462 2 stencil_program_free(prog);
463 2 return NULL;
464 }
465 1074 return prog;
466 }
467  
468 91 static stencil_cache_ent *get_file_ent(pTHX_ stencil_engine *e,
469 const char *name, size_t nlen,
470 int *notfound, SV **err)
471 {
472 char path[STENCIL_PATH_MAX];
473 struct stat st;
474 uint64_t h;
475 stencil_cache_ent *ent;
476 char *src;
477 STRLEN slen;
478 time_t mtime;
479 Off_t fsize;
480 stencil_program *prog;
481  
482 91 ▶ if (notfound)
Branch: 1 branch
BranchTrueFalseTotal
if (notfound) 91 0 91
483 91 *notfound = 0;
484 91 ▶ 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 83 91
485 8 ▶ if (notfound)
Branch: 1 branch
BranchTrueFalseTotal
if (notfound) 8 0 8
486 8 *notfound = 1;
487 8 return NULL;
488 }
489 83 h = stencil_fnv1a(path, strlen(path));
490 87 ▶ 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 76 87
491 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) { 7 4 11
if (ent->abs_path && strcmp(ent->abs_path, path) == 0) { 7 0 7
492 7 stencil_stat_cache_hits++;
493 7 ▶ if (!ent_ready(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent, err)) 0 7 7
494 0 return NULL;
495 7 return ent;
496 }
497 }
498  
499 76 src = slurp(aTHX_ path, &slen, &mtime, &fsize, err);
500 76 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 76 76
501 0 return NULL;
502 76 prog = compile_source(aTHX_ e, src, slen, path, 0, err);
503 76 free(src);
504 76 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 1 75 76
505 1 return NULL;
506  
507 75 ent = (stencil_cache_ent *)calloc(1, sizeof *ent);
508 75 ▶ if (!ent) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ent) { 0 75 75
509 0 stencil_program_free(prog);
510 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
511 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
512 0 return NULL;
513 }
514 75 ent->abs_path = strdup(path);
515 75 ent->path_len = (uint32_t)strlen(path);
516 75 ent->mtime = mtime;
517 75 ent->fsize = fsize;
518 75 ent->checked_at = time(NULL);
519 75 ent->prog = prog;
520 75 ent->next = *bucket_for(e, h);
521 75 *bucket_for(e, h) = ent;
522 75 e->n_ents++;
523 75 e->generation++;
524  
525 75 ▶ if (!link_ent(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!link_ent(aTHX_ e, ent, err)) 5 70 75
526 5 return NULL; /* entry stays cached but unusable; error out */
527 70 return ent;
528 }
529  
530 /* Insert a string-keyed (or alias) entry and enforce the LRU bound. */
531 779 static stencil_cache_ent *string_ent_insert(pTHX_ stencil_engine *e,
532 const char *src, STRLEN len,
533 uint64_t h,
534 stencil_program *prog,
535 stencil_cache_ent *alias,
536 SV **err)
537 {
538 stencil_cache_ent *ent =
539 779 (stencil_cache_ent *)calloc(1, sizeof *ent);
540 779 ▶ if (!ent)
Branch: 1 branch
BranchTrueFalseTotal
if (!ent) 0 779 779
541 0 goto oom;
542 779 ▶ ent->src = (char *)malloc(len ? len : 1);
Branch: 1 branch
BranchTrueFalseTotal
ent->src = (char *)malloc(len ? len : 1); 777 2 779
543 779 ▶ if (!ent->src) {
Branch: 1 branch
BranchTrueFalseTotal
if (!ent->src) { 0 779 779
544 0 free(ent);
545 0 goto oom;
546 }
547 779 memcpy(ent->src, src, len);
548 779 ent->src_len = len;
549 779 ent->src_hash = h;
550 779 ent->prog = prog;
551 779 ent->alias = alias;
552 779 ent->next = *bucket_for(e, h);
553 779 *bucket_for(e, h) = ent;
554 779 e->n_ents++;
555 779 e->n_string++;
556 779 e->generation++;
557 779 lru_push_front(e, ent);
558 857 ▶ while (e->n_string > e->cache_size && e->lru_tail
Branch: 1 branch
BranchTrueFalseTotal
while (e->n_string > e->cache_size && e->lru_tail 78 0 78
559 935 ▶ && e->lru_tail != ent) {
Branch: 2 branches
BranchTrueFalseTotal
&& e->lru_tail != ent) { 78 779 857
&& e->lru_tail != ent) { 78 0 78
560 78 stencil_cache_ent *victim = e->lru_tail;
561 78 lru_unlink(e, victim);
562 78 bucket_unlink(e, victim, victim->src_hash);
563 78 ent_free(aTHX_ victim);
564 78 e->n_string--;
565 78 e->n_ents--;
566 78 e->generation++;
567 }
568 779 return ent;
569 0 oom:
570 0 stencil_program_free(prog);
571 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
572 0 *err = sv_2mortal(newSVpvs("Template::Stencil: out of memory"));
573 0 return NULL;
574 }
575  
576 998 static stencil_cache_ent *get_string_ent(pTHX_ stencil_engine *e,
577 const char *src, STRLEN len,
578 uint32_t cflags, SV **err)
579 {
580 998 uint64_t h = stencil_fnv1a(src, len);
581 stencil_cache_ent *ent;
582 stencil_program *prog;
583  
584 1921 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 923 998 1921
585 923 ▶ 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 923 0 923
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 0 923 923
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 0 0 0
586 0 ▶ && memcmp(ent->src, src, len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(ent->src, src, len) == 0) { 0 0 0
587 0 stencil_stat_cache_hits++;
588 0 lru_unlink(e, ent);
589 0 lru_push_front(e, ent);
590 0 ▶ if (!ent_ready(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent, err)) 0 0 0
591 0 return NULL;
592 0 return ent;
593 }
594 }
595  
596 998 prog = compile_source(aTHX_ e, src, len, "<string>", cflags, err);
597 998 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 272 726 998
598 272 return NULL;
599 726 ent = string_ent_insert(aTHX_ e, src, len, h, prog, NULL, err);
600 726 ▶ if (!ent)
Branch: 1 branch
BranchTrueFalseTotal
if (!ent) 0 726 726
601 0 return NULL;
602 726 ▶ if (!link_ent(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!link_ent(aTHX_ e, ent, err)) 6 720 726
603 6 return NULL;
604 720 return ent;
605 }
606  
607 /* Name-keyed lookup of a file entry (wrapper fetch): an alias hit
608 * costs zero syscalls; a miss resolves and caches the alias. */
609 133 static stencil_cache_ent *get_named_file_ent(pTHX_ stencil_engine *e,
610 const char *name,
611 size_t nlen, SV **err)
612 {
613 133 uint64_t h = stencil_fnv1a(name, nlen);
614 stencil_cache_ent *ent, *file;
615  
616 135 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 114 21 135
617 114 ▶ 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 114 0 114
if (!ent->abs_path && ent->src_hash == h && ent->src_len == nlen 112 2 114
if (!ent->abs_path && ent->src_hash == h && ent->src_len == nlen 112 0 112
618 112 ▶ && memcmp(ent->src, name, nlen) == 0 && ent->alias) {
Branch: 2 branches
BranchTrueFalseTotal
&& memcmp(ent->src, name, nlen) == 0 && ent->alias) { 112 0 112
&& memcmp(ent->src, name, nlen) == 0 && ent->alias) { 112 0 112
619 112 stencil_stat_cache_hits++;
620 112 lru_unlink(e, ent);
621 112 lru_push_front(e, ent);
622 112 ▶ if (!ent_ready(aTHX_ e, ent->alias, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!ent_ready(aTHX_ e, ent->alias, err)) 0 112 112
623 0 return NULL;
624 112 return ent->alias;
625 }
626 }
627 {
628 21 int notfound = 0;
629 21 file = get_file_ent(aTHX_ e, name, nlen, &notfound, err);
630 21 ▶ if (!file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file) { 0 21 21
631 0 ▶ if (notfound && err)
Branch: 2 branches
BranchTrueFalseTotal
if (notfound && err) 0 0 0
if (notfound && err) 0 0 0
632 0 *err = sv_2mortal(newSVpvf(
633 "Template::Stencil: cannot find template '%.*s'",
634 (int)nlen, name));
635 0 return NULL;
636 }
637 }
638 21 ▶ 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 21 21
639 0 return NULL;
640 21 return file;
641 }
642  
643 /* Page dispatch: one cache lookup on the raw template argument. A hit
644 * on an alias entry reaches the file entry with zero syscalls (the
645 * stat_ttl => -1 steady state); a miss decides string-vs-file per the
646 * overview rule and caches the decision. */
647 2023 static stencil_cache_ent *get_page_ent(pTHX_ stencil_engine *e,
648 const char *p, STRLEN len,
649 uint32_t cflags, SV **err)
650 {
651 2023 uint64_t h = stencil_fnv1a(p, len);
652 stencil_cache_ent *ent;
653  
654 2949 ▶ for (ent = *bucket_for(e, h); ent; ent = ent->next) {
Branch: 1 branch
BranchTrueFalseTotal
for (ent = *bucket_for(e, h); ent; ent = ent->next) { 1914 1035 2949
655 1914 ▶ 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 1913 1 1914
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 988 925 1913
if (!ent->abs_path && ent->src_hash == h && ent->src_len == len 988 0 988
656 988 ▶ && memcmp(ent->src, p, len) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
&& memcmp(ent->src, p, len) == 0) { 988 0 988
657 988 stencil_stat_cache_hits++;
658 988 lru_unlink(e, ent);
659 988 lru_push_front(e, ent);
660 988 ▶ 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)) 72 916 988
if (!ent_ready(aTHX_ e, ent->alias ? ent->alias : ent, err)) 0 988 988
661 0 return NULL;
662 988 ▶ return ent->alias ? ent->alias : ent;
Branch: 1 branch
BranchTrueFalseTotal
return ent->alias ? ent->alias : ent; 72 916 988
663 }
664 }
665  
666 1035 ▶ if (!looks_like_source(p, len)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!looks_like_source(p, len)) { 278 757 1035
667 char path[STENCIL_PATH_MAX];
668 struct stat st;
669 278 ▶ if (resolve_file(e, p, len, path, sizeof path, &st)) {
Branch: 1 branch
BranchTrueFalseTotal
if (resolve_file(e, p, len, path, sizeof path, &st)) { 37 241 278
670 37 int notfound = 0;
671 stencil_cache_ent *file =
672 37 get_file_ent(aTHX_ e, p, len, &notfound, err);
673 37 ▶ if (!file) {
Branch: 1 branch
BranchTrueFalseTotal
if (!file) { 5 32 37
674 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
675 0 *err = sv_2mortal(newSVpvf(
676 "Template::Stencil: cannot find template "
677 "'%.*s'", (int)len, p));
678 5 return NULL;
679 }
680 32 ▶ 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 32 32
681 0 return NULL;
682 32 return file;
683 }
684 }
685 998 return get_string_ent(aTHX_ e, p, len, cflags, err);
686 }
687  
688 /* ---- revalidation and effective sizes --------------------------------- */
689  
690 2003 static int revalidate_ent(pTHX_ stencil_engine *e, stencil_cache_ent *ent,
691 SV **err)
692 {
693 struct stat st;
694 time_t now;
695 2003 ▶ if (!ent->abs_path || e->stat_ttl < 0)
Branch: 2 branches
BranchTrueFalseTotal
if (!ent->abs_path || e->stat_ttl < 0) 367 1636 2003
if (!ent->abs_path || e->stat_ttl < 0) 214 153 367
696 1850 return 1;
697 153 now = time(NULL);
698 153 ▶ if (e->stat_ttl > 0
Branch: 1 branch
BranchTrueFalseTotal
if (e->stat_ttl > 0 147 6 153
699 147 ▶ && (double)(now - ent->checked_at) <= e->stat_ttl)
Branch: 1 branch
BranchTrueFalseTotal
&& (double)(now - ent->checked_at) <= e->stat_ttl) 147 0 147
700 147 return 1;
701 6 ent->checked_at = now;
702 6 stencil_stat_stats++;
703 6 ▶ if (stat(ent->abs_path, &st) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat(ent->abs_path, &st) != 0) { 0 6 6
704 0 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 0 0 0
705 0 *err = sv_2mortal(newSVpvf(
706 "Template::Stencil: template '%s' disappeared",
707 ent->abs_path));
708 0 return 0;
709 }
710 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
711 4 return 1;
712 {
713 char *src;
714 STRLEN slen;
715 time_t mtime;
716 Off_t fsize;
717 stencil_program *prog;
718 2 src = slurp(aTHX_ ent->abs_path, &slen, &mtime, &fsize, err);
719 2 ▶ if (!src)
Branch: 1 branch
BranchTrueFalseTotal
if (!src) 0 2 2
720 0 return 0;
721 2 prog = compile_source(aTHX_ e, src, slen, ent->abs_path, 0, err);
722 2 free(src);
723 2 ▶ if (!prog)
Branch: 1 branch
BranchTrueFalseTotal
if (!prog) 0 2 2
724 0 return 0;
725 2 stencil_program_free(ent->prog);
726 2 ent->prog = prog;
727 2 ent->mtime = mtime;
728 2 ent->fsize = fsize;
729 2 e->generation++;
730 2 return link_ent(aTHX_ e, ent, err);
731 }
732 }
733  
734 /* Revalidate every node of an include graph once per render entry. The
735 * visit serial keeps shared includes from being stat'd twice. */
736 2004 static int revalidate_graph(pTHX_ stencil_engine *e,
737 stencil_cache_ent *ent, uint32_t visit,
738 SV **err)
739 {
740 uint32_t i;
741 2004 ▶ if (ent->seen == visit)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->seen == visit) 1 2003 2004
742 1 return 1;
743 2003 ent->seen = visit;
744 2003 ▶ if (!revalidate_ent(aTHX_ e, ent, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!revalidate_ent(aTHX_ e, ent, err)) 0 2003 2003
745 0 return 0;
746 2136 ▶ for (i = 0; i < ent->prog->n_incs; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ent->prog->n_incs; i++) 133 2003 2136
747 133 ▶ 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 133 133
748 0 return 0;
749 2003 return 1;
750 }
751  
752 /* Conservative render-state sizes across the include graph (sum along
753 * the worst nesting), memoised per generation. */
754 1896 static void ent_eff(stencil_engine *e, stencil_cache_ent *ent)
755 {
756 uint32_t s, f, b, i;
757 1896 uint32_t cs = 0, cf = 0, cb = 0;
758 1896 ▶ if (ent->eff_gen == e->generation)
Branch: 1 branch
BranchTrueFalseTotal
if (ent->eff_gen == e->generation) 1078 818 1896
759 1078 return;
760 818 ent->eff_gen = e->generation;
761 818 s = ent->prog->max_stack;
762 818 f = ent->prog->max_frames;
763 818 b = ent->prog->max_binds;
764 843 ▶ for (i = 0; i < ent->prog->n_incs; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ent->prog->n_incs; i++) { 25 818 843
765 25 stencil_cache_ent *ch = ent->incs[i];
766 25 ent_eff(e, ch);
767 25 ▶ if (ch->eff_stack > cs) cs = ch->eff_stack;
Branch: 1 branch
BranchTrueFalseTotal
if (ch->eff_stack > cs) cs = ch->eff_stack; 23 2 25
768 25 ▶ if (ch->eff_frames > cf) cf = ch->eff_frames;
Branch: 1 branch
BranchTrueFalseTotal
if (ch->eff_frames > cf) cf = ch->eff_frames; 0 25 25
769 25 ▶ if (ch->eff_binds > cb) cb = ch->eff_binds;
Branch: 1 branch
BranchTrueFalseTotal
if (ch->eff_binds > cb) cb = ch->eff_binds; 0 25 25
770 }
771 818 ent->eff_stack = s + cs;
772 818 ent->eff_frames = f + cf;
773 818 ent->eff_binds = b + cb;
774 }
775  
776 /* ---- engine lifecycle ------------------------------------------------- */
777  
778 296 stencil_engine *stencil_engine_new(pTHX_ const char *template_dir,
779 const char *wrapper, uint32_t flags,
780 double stat_ttl, uint32_t cache_size,
781 HV *filters)
782 {
783 296 stencil_engine *e = (stencil_engine *)calloc(1, sizeof *e);
784 296 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 0 296 296
785 0 return NULL;
786 296 ▶ if (template_dir && *template_dir) {
Branch: 2 branches
BranchTrueFalseTotal
if (template_dir && *template_dir) { 46 250 296
if (template_dir && *template_dir) { 46 0 46
787 46 size_t len = strlen(template_dir);
788 46 ▶ while (len > 1 && template_dir[len - 1] == '/')
Branch: 2 branches
BranchTrueFalseTotal
while (len > 1 && template_dir[len - 1] == '/') 46 0 46
while (len > 1 && template_dir[len - 1] == '/') 0 46 46
789 0 len--;
790 46 e->template_dir = (char *)malloc(len + 1);
791 46 memcpy(e->template_dir, template_dir, len);
792 46 e->template_dir[len] = '\0';
793 46 e->template_dir_len = len;
794 }
795 296 ▶ if (wrapper && *wrapper)
Branch: 2 branches
BranchTrueFalseTotal
if (wrapper && *wrapper) 15 281 296
if (wrapper && *wrapper) 15 0 15
796 15 e->wrapper = strdup(wrapper);
797 296 e->flags = flags;
798 296 e->stat_ttl = stat_ttl;
799 296 ▶ e->cache_size = cache_size ? cache_size : 256;
Branch: 1 branch
BranchTrueFalseTotal
e->cache_size = cache_size ? cache_size : 256; 296 0 296
800 296 e->n_buckets = STENCIL_N_BUCKETS;
801 296 e->buckets = (stencil_cache_ent **)
802 296 calloc(e->n_buckets, sizeof(stencil_cache_ent *));
803 296 stencil_stat_engines++;
804 296 e->generation = 1;
805 296 ▶ if (filters)
Branch: 1 branch
BranchTrueFalseTotal
if (filters) 11 285 296
806 11 e->filters = (HV *)SvREFCNT_inc((SV *)filters);
807 296 return e;
808 }
809  
810 296 void stencil_engine_free(pTHX_ stencil_engine *e)
811 {
812 296 ▶ if (!e)
Branch: 1 branch
BranchTrueFalseTotal
if (!e) 0 296 296
813 0 return;
814 296 engine_cache_clear(aTHX_ e);
815 296 free(e->buckets);
816 296 free(e->template_dir);
817 296 free(e->wrapper);
818 /* during global destruction perl reclaims the filters HV itself */
819 296 ▶ if (e->filters && !PL_dirty)
Branch: 2 branches
BranchTrueFalseTotal
if (e->filters && !PL_dirty) 11 285 296
if (e->filters && !PL_dirty) 9 2 11
820 9 SvREFCNT_dec((SV *)e->filters);
821 296 free(e);
822 296 stencil_stat_engines--;
823 }
824  
825 /* ---- render entry ------------------------------------------------------ */
826  
827 1035 static int looks_like_source(const char *p, STRLEN len)
828 {
829 const char *q, *end;
830 1035 ▶ if (memchr(p, '\n', len))
Branch: 1 branch
BranchTrueFalseTotal
if (memchr(p, '\n', len)) 188 847 1035
831 188 return 1;
832 847 ▶ if (len >= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (len >= 2) { 839 8 847
833 839 end = p + len - 1;
834 839 for (q = p; (q = (const char *)memchr(q, '{',
835 905 ▶ (size_t)(end - q))) != NULL; q++)
Branch: 1 branch
BranchTrueFalseTotal
(size_t)(end - q))) != NULL; q++) 635 270 905
836 635 ▶ if (q[1] == '%')
Branch: 1 branch
BranchTrueFalseTotal
if (q[1] == '%') 569 66 635
837 569 return 1;
838 }
839 278 return 0;
840 }
841  
842 2025 SV *stencil_engine_render(pTHX_ stencil_engine *e, SV *tmpl, HV *data,
843 SV *opts, SV **err)
844 {
845 STRLEN len;
846 2025 const char *p = SvPV_const(tmpl, len);
847 2025 stencil_cache_ent *page = NULL, *wrap = NULL;
848 2025 const char *wrap_name = e->wrapper;
849 SV *out;
850 uint32_t eff_s, eff_f, eff_b;
851 2025 uint32_t rf = e->flags & (STENCIL_RF_STRICT
852 | STENCIL_RF_NO_SORT_KEYS
853 | STENCIL_RF_NO_ESCAPE
854 | STENCIL_RF_CHARS
855 | STENCIL_RF_PRETTY);
856  
857 /* the engine owns encoding: a latin-1-repped template string is
858 * upgraded so compiled literals are always UTF-8 bytes */
859 2025 ▶ if (!SvUTF8(tmpl)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!SvUTF8(tmpl)) { 2022 3 2025
860 STRLEN i;
861 174167 ▶ for (i = 0; i < len; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < len; i++) { 172443 1724 174167
862 172443 ▶ if ((unsigned char)p[i] & 0x80) {
Branch: 1 branch
BranchTrueFalseTotal
if ((unsigned char)p[i] & 0x80) { 298 172145 172443
863 298 tmpl = sv_mortalcopy(tmpl);
864 298 sv_utf8_upgrade(tmpl);
865 298 p = SvPV_const(tmpl, len);
866 298 break;
867 }
868 }
869 }
870  
871 2025 ▶ if (opts && SvOK(opts)) {
Branch: 2 branches
BranchTrueFalseTotal
if (opts && SvOK(opts)) { 638 1387 2025
if (opts && SvOK(opts)) { 638 0 638
872 HV *oh;
873 HE *he;
874 638 ▶ if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) {
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) { 638 0 638
if (!SvROK(opts) || SvTYPE(SvRV(opts)) != SVt_PVHV) { 1 637 638
875 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
876 1 *err = sv_2mortal(newSVpvs(
877 "Template::Stencil: render options must be a hashref"));
878 1 return NULL;
879 }
880 637 oh = (HV *)SvRV(opts);
881 637 hv_iterinit(oh);
882 758 ▶ while ((he = hv_iternext(oh))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(oh))) { 122 636 758
883 STRLEN klen;
884 122 ▶ const char *k = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *k = HePV(he, klen); 0 122 122
885 122 SV *v = HeVAL(he);
886 122 ▶ if (klen == 7 && memEQ(k, "wrapper", 7)) {
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 7 && memEQ(k, "wrapper", 7)) { 113 9 122
if (klen == 7 && memEQ(k, "wrapper", 7)) { 113 0 113
887 222 ▶ wrap_name = SvOK(v) ? SvPV_nolen(v) : NULL;
Branch: 1 branch
BranchTrueFalseTotal
wrap_name = SvOK(v) ? SvPV_nolen(v) : NULL; 109 4 113
888 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
889 10 ▶ if (SvTRUE(v))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(v)) 3 2 5
890 3 rf |= STENCIL_RF_STRICT;
891 else
892 2 rf &= ~STENCIL_RF_STRICT;
893 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
894 6 ▶ if (SvTRUE(v))
Branch: 1 branch
BranchTrueFalseTotal
if (SvTRUE(v)) 2 1 3
895 2 rf |= STENCIL_RF_PRETTY;
896 else
897 1 rf &= ~STENCIL_RF_PRETTY;
898 } else {
899 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
900 1 *err = sv_2mortal(newSVpvf(
901 "Template::Stencil: unknown render option "
902 "'%.*s'", (int)klen, k));
903 1 return NULL;
904 }
905 }
906 }
907  
908 2023 page = get_page_ent(aTHX_ e, p, len,
909 2023 SvUTF8(tmpl) ? STENCIL_PROG_SRC_UTF8 : 0, err);
910 2023 ▶ if (!page)
Branch: 1 branch
BranchTrueFalseTotal
if (!page) 283 1740 2023
911 283 return NULL;
912  
913 1740 ▶ if (wrap_name) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrap_name) { 133 1607 1740
914 133 wrap = get_named_file_ent(aTHX_ e, wrap_name, strlen(wrap_name),
915 err);
916 133 ▶ if (!wrap)
Branch: 1 branch
BranchTrueFalseTotal
if (!wrap) 0 133 133
917 0 return NULL;
918 133 ▶ if (!(wrap->prog->flags & STENCIL_PROG_IS_WRAPPER)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(wrap->prog->flags & STENCIL_PROG_IS_WRAPPER)) { 1 132 133
919 1 ▶ if (err)
Branch: 1 branch
BranchTrueFalseTotal
if (err) 1 0 1
920 1 *err = sv_2mortal(newSVpvf(
921 "Template::Stencil: wrapper '%s' has no "
922 "{%% content %%}", ent_name(wrap)));
923 1 return NULL;
924 }
925 }
926  
927 {
928 static uint32_t visit_serial = 0;
929 1739 uint32_t visit = ++visit_serial;
930 1739 ▶ if (!revalidate_graph(aTHX_ e, page, visit, err))
Branch: 1 branch
BranchTrueFalseTotal
if (!revalidate_graph(aTHX_ e, page, visit, err)) 0 1739 1739
931 0 return NULL;
932 1739 ▶ if (wrap && !revalidate_graph(aTHX_ e, wrap, visit, err))
Branch: 2 branches
BranchTrueFalseTotal
if (wrap && !revalidate_graph(aTHX_ e, wrap, visit, err)) 132 1607 1739
if (wrap && !revalidate_graph(aTHX_ e, wrap, visit, err)) 0 132 132
933 0 return NULL;
934 }
935 1739 ent_eff(e, page);
936 1739 eff_s = page->eff_stack;
937 1739 eff_f = page->eff_frames;
938 1739 eff_b = page->eff_binds;
939 1739 ▶ if (wrap) {
Branch: 1 branch
BranchTrueFalseTotal
if (wrap) { 132 1607 1739
940 132 ent_eff(e, wrap);
941 132 eff_s += wrap->eff_stack;
942 132 eff_f += wrap->eff_frames;
943 132 eff_b += wrap->eff_binds;
944 }
945  
946 1871 ▶ out = stencil_render_core(aTHX_
Branch: 3 branches
BranchTrueFalseTotal
out = stencil_render_core(aTHX_ 132 1607 1739
out = stencil_render_core(aTHX_ 132 1607 1739
out = stencil_render_core(aTHX_ 132 1607 1739
947 page->prog, page->incs, ent_name(page),
948 wrap ? wrap->prog : NULL, wrap ? wrap->incs : NULL,
949 132 wrap ? ent_name(wrap) : NULL,
950 eff_s, eff_f, eff_b, data, e->filters, rf, err);
951  
952 1739 ▶ if (out && (rf & STENCIL_RF_PRETTY)) {
Branch: 2 branches
BranchTrueFalseTotal
if (out && (rf & STENCIL_RF_PRETTY)) { 1725 14 1739
if (out && (rf & STENCIL_RF_PRETTY)) { 9 1716 1725
953 9 SV *dense = pretty_strip_blank_lines(aTHX_ out);
954 9 SV *shaped = eshu_prettify(aTHX_ dense, err);
955 9 SvREFCNT_dec(out);
956 9 SvREFCNT_dec(dense);
957 9 ▶ if (!shaped)
Branch: 1 branch
BranchTrueFalseTotal
if (!shaped) 0 9 9
958 0 out = NULL;
959 else {
960 9 ▶ if (!(rf & STENCIL_RF_CHARS))
Branch: 1 branch
BranchTrueFalseTotal
if (!(rf & STENCIL_RF_CHARS)) 9 0 9
961 9 SvUTF8_off(shaped);
962 9 out = shaped;
963 }
964 }
965  
966 1739 ▶ if (e->flags & STENCIL_EF_NO_CACHE)
Branch: 1 branch
BranchTrueFalseTotal
if (e->flags & STENCIL_EF_NO_CACHE) 3 1736 1739
967 3 engine_cache_clear(aTHX_ e);
968 1739 return out;
969 }
« src/stencil_arena.c ↑ Summary Stencil.xs »