| 1 |
|
| #include "stencil.h" |
| 2 |
|
| |
| 3 |
|
| #include <stdarg.h> |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| |
| 6 |
|
| UV stencil_stat_scratch_allocs = 0; |
| 7 |
|
| |
| 8 |
|
| /* ==================================================================== |
| 9 |
|
| * Render state |
| 10 |
|
| * ==================================================================== */ |
| 11 |
|
| |
| 12 |
|
| #define STENCIL_VF_ESCAPED 0x1u /* html filter ran; do not re-escape */ |
| 13 |
|
| |
| 14 |
|
| typedef struct stencil_val { |
| 15 |
|
| SV *sv; /* NULL = undef */ |
| 16 |
|
| uint8_t owned; /* consume must SvREFCNT_dec */ |
| 17 |
|
| uint8_t vflags; |
| 18 |
|
| } stencil_val; |
| 19 |
|
| |
| 20 |
|
| typedef struct stencil_khent { |
| 21 |
|
| const char *pv; |
| 22 |
|
| STRLEN len; |
| 23 |
|
| int utf8; |
| 24 |
|
| SV *val; |
| 25 |
|
| } stencil_khent; |
| 26 |
|
| |
| 27 |
|
| /* Scope names are stored as (bytes, len, hash) rather than name ids: |
| 28 |
|
| * name ids are per-program, and an include shares the caller's scope |
| 29 |
|
| * across program boundaries. The pv points into the owning program's |
| 30 |
|
| * arena, which outlives the render. */ |
| 31 |
|
| typedef struct stencil_sname { |
| 32 |
|
| const char *pv; |
| 33 |
|
| uint32_t len; |
| 34 |
|
| uint32_t hash; |
| 35 |
|
| } stencil_sname; |
| 36 |
|
| |
| 37 |
|
| typedef struct stencil_rframe { |
| 38 |
|
| int is_hash; |
| 39 |
|
| AV *av; |
| 40 |
|
| HV *hv; |
| 41 |
|
| stencil_khent *kh; /* sorted key/value vector (hash) */ |
| 42 |
|
| SV *key_sv; /* reused key SV (hash) */ |
| 43 |
|
| SSize_t i, size; |
| 44 |
|
| stencil_sname name; /* array var / hash key var */ |
| 45 |
|
| stencil_sname val; /* hash value var */ |
| 46 |
|
| uint32_t serial; |
| 47 |
|
| SV *cur; /* array element / key_sv */ |
| 48 |
|
| SV *cur_val; /* hash value */ |
| 49 |
|
| SV *agg; /* the iterated aggregate */ |
| 50 |
|
| int agg_owned; |
| 51 |
|
| } stencil_rframe; |
| 52 |
|
| |
| 53 |
|
| typedef struct stencil_rbind { |
| 54 |
|
| stencil_sname name; |
| 55 |
|
| SV *sv; |
| 56 |
|
| uint32_t serial; |
| 57 |
|
| } stencil_rbind; |
| 58 |
|
| |
| 59 |
|
| #define SNAME_EQ(a, b) \ |
| 60 |
|
| ((a).hash == (b).hash && (a).len == (b).len \ |
| 61 |
|
| && memcmp((a).pv, (b).pv, (a).len) == 0) |
| 62 |
|
| |
| 63 |
|
| typedef struct stencil_rstate { |
| 64 |
|
| /* current unit (swapped for includes / content) */ |
| 65 |
|
| stencil_program *prog; |
| 66 |
|
| const uint8_t *code; |
| 67 |
|
| const char *pool; |
| 68 |
|
| const stencil_cname *names; |
| 69 |
|
| const stencil_cpath *paths; |
| 70 |
|
| const stencil_seg *segs; |
| 71 |
|
| const stencil_cfilt *filts; /* current unit's filter table */ |
| 72 |
|
| stencil_cache_ent **incs; /* current unit's include links */ |
| 73 |
|
| const char *unit_name; |
| 74 |
|
| /* content unit for {% content %} (page under a wrapper) */ |
| 75 |
|
| stencil_program *content_prog; |
| 76 |
|
| stencil_cache_ent **content_incs; |
| 77 |
|
| const char *content_name; |
| 78 |
|
| int content_done; |
| 79 |
|
| /* filters */ |
| 80 |
|
| HV *filters; /* engine's user coderef registry */ |
| 81 |
|
| SV *scratch[2]; /* filter output SVs, round-robin */ |
| 82 |
|
| int scr_i; |
| 83 |
|
| /* shared render state */ |
| 84 |
|
| stencil_buf buf; |
| 85 |
|
| HV *data; |
| 86 |
|
| stencil_val *stack; |
| 87 |
|
| int32_t sp; |
| 88 |
|
| stencil_rframe *frames; |
| 89 |
|
| int32_t nf; |
| 90 |
|
| stencil_rbind *binds; |
| 91 |
|
| int32_t nb; |
| 92 |
|
| uint32_t serial; |
| 93 |
|
| uint32_t flags; |
| 94 |
|
| uint32_t last_path; |
| 95 |
|
| /* error capture: message + the failing unit's name/line */ |
| 96 |
|
| char errbuf[192]; |
| 97 |
|
| int failed; |
| 98 |
|
| int err_final; |
| 99 |
|
| const char *err_name; |
| 100 |
|
| uint32_t err_line; |
| 101 |
|
| } stencil_rstate; |
| 102 |
|
| |
| 103 |
11459 |
| static stencil_sname sname_of(const struct stencil_rstate *r, uint32_t id) |
| 104 |
|
| { |
| 105 |
11459 |
| const stencil_cname *nm = &r->names[id]; |
| 106 |
|
| stencil_sname s; |
| 107 |
11459 |
| s.pv = r->pool + nm->off; |
| 108 |
11459 |
| s.len = nm->len; |
| 109 |
11459 |
| s.hash = nm->hash; |
| 110 |
11459 |
| return s; |
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
102833 |
| static void set_unit(stencil_rstate *r, stencil_program *prog, |
| 114 |
|
| stencil_cache_ent **incs, const char *name) |
| 115 |
|
| { |
| 116 |
102833 |
| r->prog = prog; |
| 117 |
102833 |
| r->code = stencil_prog_code(prog); |
| 118 |
102833 |
| r->pool = stencil_prog_pool(prog); |
| 119 |
102833 |
| r->names = stencil_prog_names(prog); |
| 120 |
102833 |
| r->paths = stencil_prog_paths(prog); |
| 121 |
102833 |
| r->segs = stencil_prog_segs(prog); |
| 122 |
102833 |
| r->filts = stencil_prog_filts(prog); |
| 123 |
102833 |
| r->incs = incs; |
| 124 |
102833 |
| r->unit_name = name; |
| 125 |
102833 |
| } |
| 126 |
|
| |
| 127 |
|
| /* ---- small helpers -------------------------------------------------- */ |
| 128 |
|
| |
| 129 |
249162 |
| STENCIL_INLINE uint32_t vm_u32(const uint8_t *code, uint32_t *pc) |
| 130 |
|
| { |
| 131 |
|
| uint32_t v; |
| 132 |
249162 |
| memcpy(&v, code + *pc, 4); |
| 133 |
249162 |
| *pc += 4; |
| 134 |
249162 |
| return v; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
|
| struct stencil_rstate; |
| 138 |
|
| static stencil_sname sname_of(const struct stencil_rstate *r, uint32_t id); |
| 139 |
|
| |
| 140 |
179960 |
| STENCIL_INLINE void val_release(pTHX_ stencil_val *v) |
| 141 |
|
| { |
| 142 |
179960 |
▶ | if (v->owned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->owned) |
969 |
178991 |
179960 |
|
| 143 |
969 |
| SvREFCNT_dec_NN(v->sv); |
| 144 |
179960 |
| } |
| 145 |
|
| |
| 146 |
|
| static uint32_t line_at(const stencil_program *prog, uint32_t off); |
| 147 |
|
| |
| 148 |
|
| /* First error wins; the message is captured here, the unit name and |
| 149 |
|
| * line are pinned by rfail_here at the failing op so nested-unit |
| 150 |
|
| * errors report the right template. */ |
| 151 |
2 |
| static int rerror(stencil_rstate *r, const char *fmt, ...) |
| 152 |
|
| { |
| 153 |
|
| va_list ap; |
| 154 |
2 |
▶ | if (r->failed) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->failed) |
0 |
2 |
2 |
|
| 155 |
0 |
| return 0; |
| 156 |
2 |
| r->failed = 1; |
| 157 |
2 |
| va_start(ap, fmt); |
| 158 |
2 |
| vsnprintf(r->errbuf, sizeof r->errbuf, fmt, ap); |
| 159 |
2 |
| va_end(ap); |
| 160 |
2 |
| return 0; |
| 161 |
|
| } |
| 162 |
|
| |
| 163 |
40 |
| static int rfail_here(stencil_rstate *r, uint32_t at) |
| 164 |
|
| { |
| 165 |
40 |
▶ | if (!r->err_final) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->err_final) { |
40 |
0 |
40 |
|
| 166 |
40 |
| r->err_final = 1; |
| 167 |
40 |
| r->err_name = r->unit_name; |
| 168 |
40 |
| r->err_line = line_at(r->prog, at); |
| 169 |
|
| } |
| 170 |
40 |
| return 0; |
| 171 |
|
| } |
| 172 |
|
| |
| 173 |
38 |
| static int rfail(stencil_rstate *r, uint32_t at, const char *fmt, ...) |
| 174 |
|
| { |
| 175 |
|
| va_list ap; |
| 176 |
38 |
▶ | if (!r->failed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->failed) { |
38 |
0 |
38 |
|
| 177 |
38 |
| r->failed = 1; |
| 178 |
38 |
| va_start(ap, fmt); |
| 179 |
38 |
| vsnprintf(r->errbuf, sizeof r->errbuf, fmt, ap); |
| 180 |
38 |
| va_end(ap); |
| 181 |
|
| } |
| 182 |
38 |
| return rfail_here(r, at); |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
29 |
| static int rfail_path(stencil_rstate *r, uint32_t at, uint32_t pid) |
| 186 |
|
| { |
| 187 |
29 |
| const stencil_cpath *p = &r->paths[pid]; |
| 188 |
58 |
| return rfail(r, at, "undef value for '%.*s'", |
| 189 |
29 |
| (int)p->full_len, r->pool + p->full_off); |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
40 |
| static uint32_t line_at(const stencil_program *prog, uint32_t off) |
| 193 |
|
| { |
| 194 |
40 |
| const stencil_cline *lines = stencil_prog_lines(prog); |
| 195 |
40 |
| uint32_t lo = 0, hi = prog->n_lines, line = 1; |
| 196 |
82 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
42 |
40 |
82 |
|
| 197 |
42 |
| uint32_t mid = (lo + hi) / 2; |
| 198 |
42 |
▶ | if (lines[mid].off <= off) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lines[mid].off <= off) { |
40 |
2 |
42 |
|
| 199 |
40 |
| line = lines[mid].line; |
| 200 |
40 |
| lo = mid + 1; |
| 201 |
|
| } else { |
| 202 |
2 |
| hi = mid; |
| 203 |
|
| } |
| 204 |
|
| } |
| 205 |
40 |
| return line; |
| 206 |
|
| } |
| 207 |
|
| |
| 208 |
|
| /* Perl truth, extended: an unblessed empty array/hash ref is false, so |
| 209 |
|
| * `{% if items %}` guards a loop naturally. */ |
| 210 |
10541 |
| static int sv_truthy(pTHX_ SV *sv) |
| 211 |
|
| { |
| 212 |
10541 |
▶ | if (!sv || !SvOK(sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) |
10523 |
18 |
10541 |
| if (!sv || !SvOK(sv)) |
2 |
10521 |
10523 |
|
| 213 |
20 |
| return 0; |
| 214 |
10521 |
▶ | if (SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
16 |
10505 |
10521 |
| if (SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
16 |
0 |
16 |
|
| 215 |
16 |
| SV *rv = SvRV(sv); |
| 216 |
16 |
▶ | if (SvTYPE(rv) == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) == SVt_PVAV) |
9 |
7 |
16 |
|
| 217 |
9 |
| return av_top_index((AV *)rv) >= 0; |
| 218 |
7 |
▶ | if (SvTYPE(rv) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) == SVt_PVHV) |
7 |
0 |
7 |
|
| 219 |
7 |
▶ | return HvKEYS((HV *)rv) > 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return HvKEYS((HV *)rv) > 0; |
0 |
7 |
7 |
|
| 220 |
|
| } |
| 221 |
10505 |
| return SvTRUE(sv); |
| 222 |
|
| } |
| 223 |
|
| |
| 224 |
|
| /* ---- hash iteration key vector -------------------------------------- */ |
| 225 |
|
| |
| 226 |
2504 |
| static int kh_cmp(const void *pa, const void *pb) |
| 227 |
|
| { |
| 228 |
2504 |
| const stencil_khent *a = (const stencil_khent *)pa; |
| 229 |
2504 |
| const stencil_khent *b = (const stencil_khent *)pb; |
| 230 |
2504 |
| STRLEN min = a->len < b->len ? a->len : b->len; |
| 231 |
2504 |
| int c = memcmp(a->pv, b->pv, min); |
| 232 |
2504 |
▶ | if (c) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) |
2504 |
0 |
2504 |
|
| 233 |
2504 |
| return c; |
| 234 |
0 |
▶ | return a->len < b->len ? -1 : a->len > b->len ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return a->len < b->len ? -1 : a->len > b->len ? 1 : 0; |
0 |
0 |
0 |
|
| 235 |
|
| } |
| 236 |
|
| |
| 237 |
339 |
| static stencil_khent *kh_collect(pTHX_ HV *hv, SSize_t size, int sorted) |
| 238 |
|
| { |
| 239 |
|
| stencil_khent *kh; |
| 240 |
|
| HE *he; |
| 241 |
339 |
| SSize_t n = 0; |
| 242 |
339 |
▶ | Newx(kh, (size_t)size, stencil_khent); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(kh, (size_t)size, stencil_khent); |
0 |
339 |
339 |
|
| 243 |
339 |
| stencil_stat_scratch_allocs++; |
| 244 |
339 |
| hv_iterinit(hv); |
| 245 |
1842 |
▶ | while ((he = hv_iternext(hv)) && n < size) { |
Branch: 2 branches
| Branch | True | False | Total |
| while ((he = hv_iternext(hv)) && n < size) { |
1503 |
339 |
1842 |
| while ((he = hv_iternext(hv)) && n < size) { |
1503 |
0 |
1503 |
|
| 246 |
|
| STRLEN len; |
| 247 |
1503 |
▶ | kh[n].pv = HePV(he, len); |
Branch: 1 branch
| Branch | True | False | Total |
| kh[n].pv = HePV(he, len); |
0 |
1503 |
1503 |
|
| 248 |
1503 |
| kh[n].len = len; |
| 249 |
1503 |
▶ | kh[n].utf8 = HeUTF8(he) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| kh[n].utf8 = HeUTF8(he) ? 1 : 0; |
0 |
1503 |
1503 |
|
| 250 |
1503 |
| kh[n].val = HeVAL(he); |
| 251 |
1503 |
| n++; |
| 252 |
|
| } |
| 253 |
339 |
▶ | if (sorted) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sorted) |
337 |
2 |
339 |
|
| 254 |
337 |
| qsort(kh, (size_t)n, sizeof *kh, kh_cmp); |
| 255 |
339 |
| return kh; |
| 256 |
|
| } |
| 257 |
|
| |
| 258 |
1503 |
| static void kh_bind(pTHX_ stencil_rframe *f, SSize_t i) |
| 259 |
|
| { |
| 260 |
1503 |
| sv_setpvn(f->key_sv, f->kh[i].pv, f->kh[i].len); |
| 261 |
1503 |
▶ | if (f->kh[i].utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->kh[i].utf8) |
0 |
1503 |
1503 |
|
| 262 |
0 |
| SvUTF8_on(f->key_sv); |
| 263 |
|
| else |
| 264 |
1503 |
| SvUTF8_off(f->key_sv); |
| 265 |
1503 |
| f->cur = f->key_sv; |
| 266 |
1503 |
| f->cur_val = f->kh[i].val; |
| 267 |
1503 |
| } |
| 268 |
|
| |
| 269 |
859 |
| static void frame_release(pTHX_ stencil_rframe *f) |
| 270 |
|
| { |
| 271 |
859 |
▶ | if (f->kh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->kh) { |
339 |
520 |
859 |
|
| 272 |
339 |
| Safefree(f->kh); |
| 273 |
339 |
| f->kh = NULL; |
| 274 |
|
| } |
| 275 |
859 |
▶ | if (f->key_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->key_sv) { |
339 |
520 |
859 |
|
| 276 |
339 |
| SvREFCNT_dec_NN(f->key_sv); |
| 277 |
339 |
| f->key_sv = NULL; |
| 278 |
|
| } |
| 279 |
859 |
▶ | if (f->agg_owned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->agg_owned) |
0 |
859 |
859 |
|
| 280 |
0 |
| SvREFCNT_dec(f->agg); |
| 281 |
859 |
| } |
| 282 |
|
| |
| 283 |
|
| /* ---- loop metadata (pure C, no HV) ----------------------------------- */ |
| 284 |
|
| |
| 285 |
10787 |
| static SV *loop_meta(pTHX_ const stencil_rframe *f, const char *n, |
| 286 |
|
| uint32_t len, int *owned) |
| 287 |
|
| { |
| 288 |
10787 |
| switch (len) { |
| 289 |
6 |
| case 3: |
| 290 |
6 |
▶ | if (memcmp(n, "key", 3) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "key", 3) == 0) |
6 |
0 |
6 |
|
| 291 |
6 |
▶ | return f->is_hash ? f->cur : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return f->is_hash ? f->cur : NULL; |
6 |
0 |
6 |
|
| 292 |
0 |
▶ | if (memcmp(n, "odd", 3) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "odd", 3) == 0) |
0 |
0 |
0 |
|
| 293 |
0 |
▶ | return ((f->i + 1) & 1) ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| return ((f->i + 1) & 1) ? &PL_sv_yes : &PL_sv_no; |
0 |
0 |
0 |
|
| 294 |
0 |
| break; |
| 295 |
10082 |
| case 4: |
| 296 |
10082 |
▶ | if (memcmp(n, "size", 4) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "size", 4) == 0) { |
26 |
10056 |
10082 |
|
| 297 |
26 |
| *owned = 1; |
| 298 |
26 |
| return newSViv((IV)f->size); |
| 299 |
|
| } |
| 300 |
10056 |
▶ | if (memcmp(n, "last", 4) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "last", 4) == 0) |
10044 |
12 |
10056 |
|
| 301 |
10044 |
▶ | return f->i == f->size - 1 ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| return f->i == f->size - 1 ? &PL_sv_yes : &PL_sv_no; |
16 |
10028 |
10044 |
|
| 302 |
12 |
▶ | if (memcmp(n, "even", 4) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "even", 4) == 0) |
12 |
0 |
12 |
|
| 303 |
12 |
▶ | return ((f->i + 1) & 1) ? &PL_sv_no : &PL_sv_yes; |
Branch: 1 branch
| Branch | True | False | Total |
| return ((f->i + 1) & 1) ? &PL_sv_no : &PL_sv_yes; |
8 |
4 |
12 |
|
| 304 |
0 |
| break; |
| 305 |
672 |
| case 5: |
| 306 |
672 |
▶ | if (memcmp(n, "index", 5) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "index", 5) == 0) { |
598 |
74 |
672 |
|
| 307 |
598 |
| *owned = 1; |
| 308 |
598 |
| return newSViv((IV)f->i); |
| 309 |
|
| } |
| 310 |
74 |
▶ | if (memcmp(n, "first", 5) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "first", 5) == 0) |
74 |
0 |
74 |
|
| 311 |
74 |
▶ | return f->i == 0 ? &PL_sv_yes : &PL_sv_no; |
Branch: 1 branch
| Branch | True | False | Total |
| return f->i == 0 ? &PL_sv_yes : &PL_sv_no; |
23 |
51 |
74 |
|
| 312 |
0 |
| break; |
| 313 |
27 |
| case 6: |
| 314 |
27 |
▶ | if (memcmp(n, "index1", 6) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "index1", 6) == 0) { |
27 |
0 |
27 |
|
| 315 |
27 |
| *owned = 1; |
| 316 |
27 |
| return newSViv((IV)(f->i + 1)); |
| 317 |
|
| } |
| 318 |
0 |
| break; |
| 319 |
|
| } |
| 320 |
0 |
| return NULL; |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
|
| /* Materialise the frame as a real HV - the only place a loop HV is |
| 324 |
|
| * ever built (capture via `set x = loop`). */ |
| 325 |
123 |
| static SV *loop_snapshot(pTHX_ const stencil_rframe *f) |
| 326 |
|
| { |
| 327 |
123 |
| HV *hv = newHV(); |
| 328 |
123 |
| (void)hv_stores(hv, "index", newSViv((IV)f->i)); |
| 329 |
123 |
| (void)hv_stores(hv, "index1", newSViv((IV)(f->i + 1))); |
| 330 |
123 |
| (void)hv_stores(hv, "size", newSViv((IV)f->size)); |
| 331 |
123 |
▶ | (void)hv_stores(hv, "first", newSVsv(boolSV(f->i == 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(hv, "first", newSVsv(boolSV(f->i == 0))); |
62 |
61 |
123 |
|
| 332 |
123 |
▶ | (void)hv_stores(hv, "last", newSVsv(boolSV(f->i == f->size - 1))); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(hv, "last", newSVsv(boolSV(f->i == f->size - 1))); |
61 |
62 |
123 |
|
| 333 |
123 |
▶ | (void)hv_stores(hv, "even", newSVsv(boolSV(((f->i + 1) & 1) == 0))); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(hv, "even", newSVsv(boolSV(((f->i + 1) & 1) == 0))); |
61 |
62 |
123 |
|
| 334 |
123 |
▶ | (void)hv_stores(hv, "odd", newSVsv(boolSV(((f->i + 1) & 1) == 1))); |
Branch: 1 branch
| Branch | True | False | Total |
| (void)hv_stores(hv, "odd", newSVsv(boolSV(((f->i + 1) & 1) == 1))); |
62 |
61 |
123 |
|
| 335 |
123 |
▶ | if (f->is_hash) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->is_hash) |
0 |
123 |
123 |
|
| 336 |
0 |
| (void)hv_stores(hv, "key", newSVsv(f->cur)); |
| 337 |
123 |
| return newRV_noinc((SV *)hv); |
| 338 |
|
| } |
| 339 |
|
| |
| 340 |
|
| /* ---- path resolution -------------------------------------------------- */ |
| 341 |
|
| |
| 342 |
|
| /* One key out of a hash, honouring magic. |
| 343 |
|
| * |
| 344 |
|
| * The fast path is hv_common with the hash this engine precomputed at |
| 345 |
|
| * compile time, and that is what every ordinary hash gets: no key SV, no |
| 346 |
|
| * rehash, one probe. |
| 347 |
|
| * |
| 348 |
|
| * It does not go through tie magic, though, and the way it fails is the |
| 349 |
|
| * problem. A tied hash read that way comes back EMPTY - not an error, not a |
| 350 |
|
| * wrong value, nothing - so a template handed one renders blanks and says |
| 351 |
|
| * nothing about why. That is a trap for any tied hash a caller passes in, |
| 352 |
|
| * and tied hashes are how a caller hands a template something it should not |
| 353 |
|
| * have to materialise up front: a big catalogue, a lazy record, a database |
| 354 |
|
| * row. |
| 355 |
|
| * |
| 356 |
|
| * So: anything magical takes hv_fetch_ent instead, which is the call perl's |
| 357 |
|
| * own $h{k} makes. The cost to everything else is one bit test. |
| 358 |
|
| */ |
| 359 |
103464 |
| static SV *stencil_hv_get(pTHX_ HV *hv, const char *pv, STRLEN len, |
| 360 |
|
| U32 hash, int kflags) |
| 361 |
|
| { |
| 362 |
103464 |
▶ | if (UNLIKELY(SvRMAGICAL((const SV *)hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (UNLIKELY(SvRMAGICAL((const SV *)hv))) { |
9 |
103455 |
103464 |
|
| 363 |
9 |
| SV *k = sv_2mortal(newSVpvn(pv, len)); |
| 364 |
|
| HE *he; |
| 365 |
|
| SV *v; |
| 366 |
9 |
▶ | if (kflags & HVhek_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (kflags & HVhek_UTF8) |
0 |
9 |
9 |
|
| 367 |
0 |
| SvUTF8_on(k); |
| 368 |
9 |
| he = hv_fetch_ent(hv, k, 0, 0); |
| 369 |
9 |
▶ | if (!he) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) |
0 |
9 |
9 |
|
| 370 |
0 |
| return NULL; |
| 371 |
9 |
| v = HeVAL(he); |
| 372 |
|
| /* What comes back from a tied hash is a PVLV carrying GET MAGIC, not |
| 373 |
|
| * the value: the tie's FETCH runs when the magic is read. Everything |
| 374 |
|
| * downstream here asks SvOK and SvPV, neither of which fires it, so |
| 375 |
|
| * without this the value reads as undef and the tag renders empty - |
| 376 |
|
| * which is the bug this branch exists to fix, arrived at one level |
| 377 |
|
| * further in. */ |
| 378 |
9 |
▶ | if (v) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) |
9 |
0 |
9 |
|
| 379 |
9 |
▶ | SvGETMAGIC(v); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(v); |
9 |
0 |
9 |
| SvGETMAGIC(v); |
0 |
9 |
9 |
|
| 380 |
9 |
| return v; |
| 381 |
|
| } |
| 382 |
|
| { |
| 383 |
103455 |
| SV **svp = (SV **)hv_common(hv, NULL, pv, len, kflags, |
| 384 |
|
| HV_FETCH_JUST_SV, NULL, hash); |
| 385 |
103455 |
▶ | return svp ? *svp : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return svp ? *svp : NULL; |
103371 |
84 |
103455 |
|
| 386 |
|
| } |
| 387 |
|
| } |
| 388 |
|
| |
| 389 |
139127 |
| static SV *resolve_path(pTHX_ stencil_rstate *r, uint32_t pid, int *owned) |
| 390 |
|
| { |
| 391 |
139127 |
| const stencil_cpath *path = &r->paths[pid]; |
| 392 |
139127 |
| const stencil_seg *segs = r->segs + path->seg_idx; |
| 393 |
|
| uint32_t i; |
| 394 |
139127 |
| SV *cur = NULL; |
| 395 |
139127 |
| int kflags = |
| 396 |
139127 |
| (r->prog->flags & STENCIL_PROG_SRC_UTF8) ? HVhek_UTF8 : 0; |
| 397 |
|
| |
| 398 |
139127 |
| *owned = 0; |
| 399 |
|
| |
| 400 |
139127 |
▶ | if (path->loop_rooted && r->nf > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (path->loop_rooted && r->nf > 0) { |
10910 |
128217 |
139127 |
| if (path->loop_rooted && r->nf > 0) { |
10910 |
0 |
10910 |
|
| 401 |
10910 |
| const stencil_rframe *f = &r->frames[r->nf - 1]; |
| 402 |
10910 |
▶ | if (path->n_segs == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (path->n_segs == 1) { |
123 |
10787 |
10910 |
|
| 403 |
123 |
| *owned = 1; |
| 404 |
123 |
| return loop_snapshot(aTHX_ f); |
| 405 |
|
| } |
| 406 |
10787 |
▶ | if (segs[1].is_index) |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs[1].is_index) |
0 |
10787 |
10787 |
|
| 407 |
0 |
| return NULL; |
| 408 |
|
| { |
| 409 |
10787 |
| const stencil_cname *nm = &r->names[segs[1].name_id]; |
| 410 |
10787 |
| cur = loop_meta(aTHX_ f, r->pool + nm->off, nm->len, owned); |
| 411 |
|
| } |
| 412 |
10787 |
▶ | if (!cur) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cur) |
0 |
10787 |
10787 |
|
| 413 |
0 |
| return NULL; |
| 414 |
10787 |
| i = 2; |
| 415 |
|
| } else { |
| 416 |
|
| /* first segment: innermost scope wins (serial-stamped binds |
| 417 |
|
| * and loop frames, matched by name bytes so includes share the |
| 418 |
|
| * caller's scope), then the root data hash */ |
| 419 |
128217 |
| const stencil_cname *nm = &r->names[segs[0].name_id]; |
| 420 |
|
| stencil_sname seg0; |
| 421 |
128217 |
| SV *best = NULL; |
| 422 |
128217 |
| uint32_t best_serial = 0; |
| 423 |
128217 |
| int found = 0; |
| 424 |
|
| int32_t k; |
| 425 |
128217 |
| seg0.pv = r->pool + nm->off; |
| 426 |
128217 |
| seg0.len = nm->len; |
| 427 |
128217 |
| seg0.hash = nm->hash; |
| 428 |
128738 |
▶ | for (k = r->nb - 1; k >= 0; k--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = r->nb - 1; k >= 0; k--) { |
5964 |
122774 |
128738 |
|
| 429 |
5964 |
▶ | if (SNAME_EQ(r->binds[k].name, seg0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SNAME_EQ(r->binds[k].name, seg0)) { |
5443 |
521 |
5964 |
| if (SNAME_EQ(r->binds[k].name, seg0)) { |
5443 |
0 |
5443 |
| if (SNAME_EQ(r->binds[k].name, seg0)) { |
5443 |
0 |
5443 |
|
| 430 |
5443 |
| best = r->binds[k].sv; |
| 431 |
5443 |
| best_serial = r->binds[k].serial; |
| 432 |
5443 |
| found = 1; |
| 433 |
5443 |
| break; |
| 434 |
|
| } |
| 435 |
|
| } |
| 436 |
133642 |
▶ | for (k = r->nf - 1; k >= 0; k--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = r->nf - 1; k >= 0; k--) { |
25365 |
108277 |
133642 |
|
| 437 |
25365 |
| const stencil_rframe *f = &r->frames[k]; |
| 438 |
25365 |
| SV *hit = NULL; |
| 439 |
25365 |
| int match = 0; |
| 440 |
25365 |
▶ | if (SNAME_EQ(f->name, seg0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SNAME_EQ(f->name, seg0)) { |
19479 |
5886 |
25365 |
| if (SNAME_EQ(f->name, seg0)) { |
19479 |
0 |
19479 |
| if (SNAME_EQ(f->name, seg0)) { |
19479 |
0 |
19479 |
|
| 441 |
19479 |
| hit = f->cur; |
| 442 |
19479 |
| match = 1; |
| 443 |
5886 |
▶ | } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
Branch: 4 branches
| Branch | True | False | Total |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
461 |
5425 |
5886 |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
461 |
0 |
461 |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
461 |
0 |
461 |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
461 |
0 |
461 |
|
| 444 |
461 |
| hit = f->cur_val; |
| 445 |
461 |
| match = 1; |
| 446 |
|
| } |
| 447 |
25365 |
▶ | if (match) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (match) { |
19940 |
5425 |
25365 |
|
| 448 |
19940 |
▶ | if (!found || f->serial > best_serial) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!found || f->serial > best_serial) |
2 |
19938 |
19940 |
| if (!found || f->serial > best_serial) |
0 |
2 |
2 |
|
| 449 |
19938 |
| best = hit; |
| 450 |
19940 |
| found = 1; |
| 451 |
19940 |
| break; |
| 452 |
|
| } |
| 453 |
|
| } |
| 454 |
128217 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
25381 |
102836 |
128217 |
|
| 455 |
25381 |
| cur = best; |
| 456 |
|
| } else { |
| 457 |
102836 |
| cur = stencil_hv_get(aTHX_ r->data, seg0.pv, seg0.len, |
| 458 |
|
| seg0.hash, kflags); |
| 459 |
|
| } |
| 460 |
128217 |
▶ | if (!cur) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cur) |
63 |
128154 |
128217 |
|
| 461 |
63 |
| return NULL; |
| 462 |
128154 |
| i = 1; |
| 463 |
|
| } |
| 464 |
|
| |
| 465 |
139562 |
▶ | for (; i < path->n_segs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < path->n_segs; i++) { |
646 |
138916 |
139562 |
|
| 466 |
|
| SV *rv; |
| 467 |
646 |
▶ | if (!SvROK(cur)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(cur)) |
1 |
645 |
646 |
|
| 468 |
1 |
| return NULL; |
| 469 |
645 |
| rv = SvRV(cur); |
| 470 |
645 |
▶ | if (SvOBJECT(rv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(rv)) { |
1 |
644 |
645 |
|
| 471 |
1 |
| rerror(r, "cannot traverse blessed reference in '%.*s'", |
| 472 |
1 |
| (int)path->full_len, r->pool + path->full_off); |
| 473 |
1 |
| return NULL; |
| 474 |
|
| } |
| 475 |
644 |
▶ | if (*owned) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*owned) { |
0 |
644 |
644 |
|
| 476 |
|
| /* intermediate owned value (loop meta deref) - hand its |
| 477 |
|
| * lifetime to the temp stack */ |
| 478 |
0 |
| sv_2mortal(cur); |
| 479 |
0 |
| *owned = 0; |
| 480 |
|
| } |
| 481 |
644 |
▶ | if (segs[i].is_index) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs[i].is_index) { |
16 |
628 |
644 |
|
| 482 |
|
| SV **svp; |
| 483 |
16 |
▶ | if (SvTYPE(rv) != SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) != SVt_PVAV) |
0 |
16 |
16 |
|
| 484 |
0 |
| return NULL; |
| 485 |
16 |
| svp = av_fetch((AV *)rv, segs[i].index, 0); |
| 486 |
16 |
▶ | cur = svp ? *svp : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| cur = svp ? *svp : NULL; |
14 |
2 |
16 |
|
| 487 |
|
| } else { |
| 488 |
628 |
| const stencil_cname *nm = &r->names[segs[i].name_id]; |
| 489 |
628 |
▶ | if (SvTYPE(rv) != SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) != SVt_PVHV) |
0 |
628 |
628 |
|
| 490 |
0 |
| return NULL; |
| 491 |
628 |
| cur = stencil_hv_get(aTHX_ (HV *)rv, r->pool + nm->off, |
| 492 |
628 |
| nm->len, nm->hash, kflags); |
| 493 |
|
| } |
| 494 |
644 |
▶ | if (!cur) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cur) |
23 |
621 |
644 |
|
| 495 |
23 |
| return NULL; |
| 496 |
|
| } |
| 497 |
138916 |
| return cur; |
| 498 |
|
| } |
| 499 |
|
| |
| 500 |
|
| /* ---- output encoding --------------------------------------------------- */ |
| 501 |
|
| |
| 502 |
|
| /* The buffer always holds valid UTF-8 bytes: UTF8-flagged values pass |
| 503 |
|
| * through, and unflagged values containing high bytes (perl's latin-1 |
| 504 |
|
| * internal form) are upgraded at print time. The engine owns encoding; |
| 505 |
|
| * callers never utf8::encode. */ |
| 506 |
|
| |
| 507 |
117467 |
| static int has_high_byte(const char *p, STRLEN n) |
| 508 |
|
| { |
| 509 |
117467 |
| const char *end = p + n; |
| 510 |
318205 |
▶ | while ((size_t)(end - p) >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((size_t)(end - p) >= 8) { |
200738 |
117467 |
318205 |
|
| 511 |
|
| uint64_t v; |
| 512 |
200738 |
| memcpy(&v, p, 8); |
| 513 |
200738 |
▶ | if (v & 0x8080808080808080ULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v & 0x8080808080808080ULL) |
0 |
200738 |
200738 |
|
| 514 |
0 |
| return 1; |
| 515 |
200738 |
| p += 8; |
| 516 |
|
| } |
| 517 |
447247 |
▶ | while (p < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) |
329784 |
117463 |
447247 |
|
| 518 |
329784 |
▶ | if ((unsigned char)*p++ & 0x80) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((unsigned char)*p++ & 0x80) |
4 |
329780 |
329784 |
|
| 519 |
4 |
| return 1; |
| 520 |
117463 |
| return 0; |
| 521 |
|
| } |
| 522 |
|
| |
| 523 |
|
| static SV *scratch_next(pTHX_ stencil_rstate *r); |
| 524 |
|
| |
| 525 |
4 |
| static const char *latin1_upgrade(pTHX_ stencil_rstate *r, |
| 526 |
|
| const char *p, STRLEN *np) |
| 527 |
|
| { |
| 528 |
4 |
| SV *sc = scratch_next(aTHX_ r); |
| 529 |
4 |
| const char *end = p + *np; |
| 530 |
|
| char *w; |
| 531 |
4 |
▶ | SvUPGRADE(sc, SVt_PV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE(sc, SVt_PV); |
0 |
4 |
4 |
|
| 532 |
4 |
▶ | SvGROW(sc, *np * 2 + 1); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGROW(sc, *np * 2 + 1); |
4 |
0 |
4 |
| SvGROW(sc, *np * 2 + 1); |
0 |
4 |
4 |
|
| 533 |
4 |
| SvPOK_only(sc); |
| 534 |
4 |
| w = SvPVX(sc); |
| 535 |
17 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
13 |
4 |
17 |
|
| 536 |
13 |
| unsigned char c = (unsigned char)*p++; |
| 537 |
13 |
▶ | if (c < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { |
9 |
4 |
13 |
|
| 538 |
9 |
| *w++ = (char)c; |
| 539 |
|
| } else { |
| 540 |
4 |
| *w++ = (char)(0xC0 | (c >> 6)); |
| 541 |
4 |
| *w++ = (char)(0x80 | (c & 0x3F)); |
| 542 |
|
| } |
| 543 |
|
| } |
| 544 |
4 |
| *np = (STRLEN)(w - SvPVX(sc)); |
| 545 |
4 |
| SvCUR_set(sc, *np); |
| 546 |
4 |
| return SvPVX(sc); |
| 547 |
|
| } |
| 548 |
|
| |
| 549 |
|
| /* ---- filter pipeline --------------------------------------------------- */ |
| 550 |
|
| |
| 551 |
51353 |
| static SV *scratch_next(pTHX_ stencil_rstate *r) |
| 552 |
|
| { |
| 553 |
51353 |
| SV **slot = &r->scratch[r->scr_i ^= 1]; |
| 554 |
51353 |
▶ | if (!*slot) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*slot) |
51003 |
350 |
51353 |
|
| 555 |
51003 |
| *slot = newSV(64); |
| 556 |
51353 |
| return *slot; |
| 557 |
|
| } |
| 558 |
|
| |
| 559 |
|
| /* Apply filter table entry `idx` (current unit) to the stack top in |
| 560 |
|
| * place. Returns 0 with the error message set (caller pins location). */ |
| 561 |
51481 |
| static int filter_apply(pTHX_ stencil_rstate *r, uint32_t idx) |
| 562 |
|
| { |
| 563 |
51481 |
| const stencil_cfilt *f = &r->filts[idx]; |
| 564 |
51481 |
| stencil_val *v = &r->stack[r->sp - 1]; |
| 565 |
51481 |
| SV *in = v->sv; |
| 566 |
|
| SV *out; |
| 567 |
|
| |
| 568 |
51481 |
▶ | if (f->builtin_id >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->builtin_id >= 0) { |
51467 |
14 |
51481 |
|
| 569 |
51467 |
▶ | if (f->builtin_id == STENCIL_FILT_DEFAULT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->builtin_id == STENCIL_FILT_DEFAULT) { |
124 |
51343 |
51467 |
|
| 570 |
124 |
| STRLEN n = 0; |
| 571 |
124 |
▶ | if (in && SvOK(in)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (in && SvOK(in)) { |
114 |
10 |
124 |
| if (in && SvOK(in)) { |
113 |
1 |
114 |
|
| 572 |
|
| (void)SvPV(in, n); |
| 573 |
113 |
▶ | if (n) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) |
112 |
1 |
113 |
|
| 574 |
112 |
| return 1; /* passthrough, flags untouched */ |
| 575 |
|
| } |
| 576 |
12 |
| out = scratch_next(aTHX_ r); |
| 577 |
12 |
▶ | if (f->arg_is_num) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->arg_is_num) { |
2 |
10 |
12 |
|
| 578 |
2 |
| sv_setnv(out, (NV)f->num_arg); |
| 579 |
|
| } else { |
| 580 |
10 |
| sv_setpvn(out, r->pool + f->str_off, f->str_len); |
| 581 |
10 |
▶ | if (r->prog->flags & STENCIL_PROG_SRC_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->prog->flags & STENCIL_PROG_SRC_UTF8) |
0 |
10 |
10 |
|
| 582 |
0 |
| SvUTF8_on(out); |
| 583 |
|
| else |
| 584 |
10 |
| SvUTF8_off(out); |
| 585 |
|
| } |
| 586 |
|
| } else { |
| 587 |
51343 |
▶ | if (!in || !SvOK(in)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!in || !SvOK(in)) |
51338 |
5 |
51343 |
| if (!in || !SvOK(in)) |
1 |
51337 |
51338 |
|
| 588 |
6 |
| return 1; /* undef passes through the others */ |
| 589 |
51337 |
| out = scratch_next(aTHX_ r); |
| 590 |
51337 |
| switch (f->builtin_id) { |
| 591 |
50619 |
| case STENCIL_FILT_UPPER: |
| 592 |
50619 |
| stencil_filt_case(aTHX_ in, out, 1); |
| 593 |
50619 |
| break; |
| 594 |
317 |
| case STENCIL_FILT_LOWER: |
| 595 |
317 |
| stencil_filt_case(aTHX_ in, out, 0); |
| 596 |
317 |
| break; |
| 597 |
344 |
| case STENCIL_FILT_TRIM: |
| 598 |
344 |
| stencil_filt_trim(aTHX_ in, out); |
| 599 |
344 |
| break; |
| 600 |
12 |
| case STENCIL_FILT_HTML: |
| 601 |
12 |
| stencil_filt_html(aTHX_ in, out); |
| 602 |
12 |
| break; |
| 603 |
10 |
| case STENCIL_FILT_URI: |
| 604 |
10 |
| stencil_filt_uri(aTHX_ in, out); |
| 605 |
10 |
| break; |
| 606 |
35 |
| case STENCIL_FILT_FMT: |
| 607 |
35 |
| stencil_filt_fmt(aTHX_ in, out, |
| 608 |
35 |
| r->pool + f->str_off, f->str_len, |
| 609 |
35 |
| !!(r->prog->flags & STENCIL_PROG_SRC_UTF8)); |
| 610 |
35 |
| break; |
| 611 |
0 |
| default: |
| 612 |
0 |
| return rerror(r, "corrupt filter table"); |
| 613 |
|
| } |
| 614 |
|
| } |
| 615 |
51349 |
| val_release(aTHX_ v); |
| 616 |
51349 |
| v->sv = out; |
| 617 |
51349 |
| v->owned = 0; |
| 618 |
51349 |
▶ | v->vflags = f->builtin_id == STENCIL_FILT_HTML |
Branch: 1 branch
| Branch | True | False | Total |
| v->vflags = f->builtin_id == STENCIL_FILT_HTML |
12 |
51337 |
51349 |
|
| 619 |
|
| ? STENCIL_VF_ESCAPED : 0; |
| 620 |
51349 |
| return 1; |
| 621 |
|
| } |
| 622 |
|
| |
| 623 |
|
| /* engine-registered Perl coderef */ |
| 624 |
|
| { |
| 625 |
28 |
| SV **cvp = r->filters |
| 626 |
14 |
| ? hv_fetch(r->filters, r->pool + f->name_off, |
| 627 |
|
| (I32)f->name_len, 0) |
| 628 |
14 |
▶ | : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| : NULL; |
14 |
0 |
14 |
|
| 629 |
14 |
| SV *copy = NULL; |
| 630 |
|
| int died; |
| 631 |
14 |
▶ | if (!cvp || !SvROK(*cvp) || SvTYPE(SvRV(*cvp)) != SVt_PVCV) |
Branch: 3 branches
| Branch | True | False | Total |
| if (!cvp || !SvROK(*cvp) || SvTYPE(SvRV(*cvp)) != SVt_PVCV) |
14 |
0 |
14 |
| if (!cvp || !SvROK(*cvp) || SvTYPE(SvRV(*cvp)) != SVt_PVCV) |
14 |
0 |
14 |
| if (!cvp || !SvROK(*cvp) || SvTYPE(SvRV(*cvp)) != SVt_PVCV) |
0 |
14 |
14 |
|
| 632 |
0 |
| return rerror(r, "unknown filter '%.*s'", |
| 633 |
0 |
| (int)f->name_len, r->pool + f->name_off); |
| 634 |
|
| { |
| 635 |
14 |
| dSP; |
| 636 |
14 |
| ENTER; |
| 637 |
14 |
| SAVETMPS; |
| 638 |
14 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
14 |
14 |
|
| 639 |
14 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
14 |
14 |
|
| 640 |
14 |
▶ | PUSHs(in ? in : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(in ? in : &PL_sv_undef); |
14 |
0 |
14 |
|
| 641 |
14 |
▶ | if (f->has_arg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->has_arg) { |
3 |
11 |
14 |
|
| 642 |
6 |
| SV *arg = f->arg_is_num |
| 643 |
2 |
| ? newSVnv((NV)f->num_arg) |
| 644 |
3 |
▶ | : newSVpvn(r->pool + f->str_off, f->str_len); |
Branch: 1 branch
| Branch | True | False | Total |
| : newSVpvn(r->pool + f->str_off, f->str_len); |
2 |
1 |
3 |
|
| 645 |
3 |
▶ | if (!f->arg_is_num |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f->arg_is_num |
1 |
2 |
3 |
|
| 646 |
1 |
▶ | && (r->prog->flags & STENCIL_PROG_SRC_UTF8)) |
Branch: 1 branch
| Branch | True | False | Total |
| && (r->prog->flags & STENCIL_PROG_SRC_UTF8)) |
0 |
1 |
1 |
|
| 647 |
0 |
| SvUTF8_on(arg); |
| 648 |
3 |
| PUSHs(sv_2mortal(arg)); |
| 649 |
|
| } |
| 650 |
14 |
| PUTBACK; |
| 651 |
14 |
| (void)call_sv(*cvp, G_SCALAR | G_EVAL); |
| 652 |
14 |
| SPAGAIN; |
| 653 |
14 |
▶ | died = SvTRUE(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV); |
14 |
0 |
14 |
|
| 654 |
14 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
1 |
13 |
14 |
|
| 655 |
1 |
| (void)POPs; |
| 656 |
|
| else |
| 657 |
13 |
| copy = newSVsv(POPs); |
| 658 |
14 |
| PUTBACK; |
| 659 |
14 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
14 |
0 |
14 |
|
| 660 |
14 |
| LEAVE; |
| 661 |
|
| } |
| 662 |
14 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
1 |
13 |
14 |
|
| 663 |
2 |
| return rerror(r, "filter '%.*s' died: %s", |
| 664 |
1 |
| (int)f->name_len, r->pool + f->name_off, |
| 665 |
1 |
▶ | SvPV_nolen(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| SvPV_nolen(ERRSV)); |
1 |
0 |
1 |
|
| 666 |
13 |
| val_release(aTHX_ v); |
| 667 |
13 |
| v->sv = copy; |
| 668 |
13 |
| v->owned = 1; |
| 669 |
13 |
| v->vflags = 0; |
| 670 |
13 |
| return 1; |
| 671 |
|
| } |
| 672 |
|
| } |
| 673 |
|
| |
| 674 |
|
| /* ---- comparison macros for the VM body -------------------------------- */ |
| 675 |
|
| |
| 676 |
|
| #define VM_CMP_NUM(OPR) \ |
| 677 |
|
| { \ |
| 678 |
|
| stencil_val vb = r->stack[--r->sp]; \ |
| 679 |
|
| stencil_val va = r->stack[--r->sp]; \ |
| 680 |
|
| NV nva = (va.sv && SvOK(va.sv)) ? SvNV(va.sv) : 0.0; \ |
| 681 |
|
| NV nvb = (vb.sv && SvOK(vb.sv)) ? SvNV(vb.sv) : 0.0; \ |
| 682 |
|
| int res = nva OPR nvb; \ |
| 683 |
|
| val_release(aTHX_ &va); \ |
| 684 |
|
| val_release(aTHX_ &vb); \ |
| 685 |
|
| r->stack[r->sp].sv = res ? &PL_sv_yes : &PL_sv_no; \ |
| 686 |
|
| r->stack[r->sp].owned = 0; \ |
| 687 |
|
| r->stack[r->sp].vflags = 0; \ |
| 688 |
|
| r->sp++; \ |
| 689 |
|
| } \ |
| 690 |
|
| VM_NEXT; |
| 691 |
|
| |
| 692 |
|
| #define VM_CMP_STR(EXPR) \ |
| 693 |
|
| { \ |
| 694 |
|
| stencil_val vb = r->stack[--r->sp]; \ |
| 695 |
|
| stencil_val va = r->stack[--r->sp]; \ |
| 696 |
|
| SV *a = (va.sv && SvOK(va.sv)) ? va.sv : &PL_sv_no; \ |
| 697 |
|
| SV *b = (vb.sv && SvOK(vb.sv)) ? vb.sv : &PL_sv_no; \ |
| 698 |
|
| int res = (EXPR) ? 1 : 0; \ |
| 699 |
|
| val_release(aTHX_ &va); \ |
| 700 |
|
| val_release(aTHX_ &vb); \ |
| 701 |
|
| r->stack[r->sp].sv = res ? &PL_sv_yes : &PL_sv_no; \ |
| 702 |
|
| r->stack[r->sp].owned = 0; \ |
| 703 |
|
| r->stack[r->sp].vflags = 0; \ |
| 704 |
|
| r->sp++; \ |
| 705 |
|
| } \ |
| 706 |
|
| VM_NEXT; |
| 707 |
|
| |
| 708 |
|
| /* ---- VM instantiations ------------------------------------------------ */ |
| 709 |
|
| |
| 710 |
|
| #ifdef STENCIL_HAVE_COMPUTED_GOTO |
| 711 |
|
| #define STENCIL_VM_GOTO 1 |
| 712 |
|
| #define STENCIL_VM_NAME stencil_vm_goto |
| 713 |
|
| #include "stencil_vm.inc" |
| 714 |
|
| #undef STENCIL_VM_GOTO |
| 715 |
|
| #undef STENCIL_VM_NAME |
| 716 |
|
| #endif |
| 717 |
|
| |
| 718 |
|
| #define STENCIL_VM_GOTO 0 |
| 719 |
|
| #define STENCIL_VM_NAME stencil_vm_switch |
| 720 |
|
| #include "stencil_vm.inc" |
| 721 |
|
| #undef STENCIL_VM_GOTO |
| 722 |
|
| #undef STENCIL_VM_NAME |
| 723 |
|
| |
| 724 |
|
| /* ---- public entries ---------------------------------------------------- */ |
| 725 |
|
| |
| 726 |
102297 |
| SV *stencil_render_core(pTHX_ |
| 727 |
|
| stencil_program *page_prog, stencil_cache_ent **page_incs, |
| 728 |
|
| const char *page_name, |
| 729 |
|
| stencil_program *wrap_prog, stencil_cache_ent **wrap_incs, |
| 730 |
|
| const char *wrap_name, |
| 731 |
|
| uint32_t eff_stack, uint32_t eff_frames, uint32_t eff_binds, |
| 732 |
|
| HV *data, HV *filters, uint32_t flags, SV **err) |
| 733 |
|
| { |
| 734 |
|
| stencil_rstate r; |
| 735 |
|
| stencil_val fix_stack[16]; |
| 736 |
|
| stencil_rframe fix_frames[8]; |
| 737 |
|
| stencil_rbind fix_binds[16]; |
| 738 |
102297 |
| int heap_stack = 0, heap_frames = 0, heap_binds = 0; |
| 739 |
|
| int ok; |
| 740 |
102297 |
▶ | stencil_program *entry_prog = wrap_prog ? wrap_prog : page_prog; |
Branch: 1 branch
| Branch | True | False | Total |
| stencil_program *entry_prog = wrap_prog ? wrap_prog : page_prog; |
132 |
102165 |
102297 |
|
| 741 |
|
| |
| 742 |
102297 |
| memset(&r, 0, sizeof r); |
| 743 |
102297 |
▶ | if (wrap_prog) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wrap_prog) { |
132 |
102165 |
102297 |
|
| 744 |
132 |
▶ | set_unit(&r, wrap_prog, wrap_incs, |
Branch: 1 branch
| Branch | True | False | Total |
| set_unit(&r, wrap_prog, wrap_incs, |
132 |
0 |
132 |
|
| 745 |
|
| wrap_name ? wrap_name : "<wrapper>"); |
| 746 |
132 |
| r.content_prog = page_prog; |
| 747 |
132 |
| r.content_incs = page_incs; |
| 748 |
132 |
▶ | r.content_name = page_name ? page_name : "<string>"; |
Branch: 1 branch
| Branch | True | False | Total |
| r.content_name = page_name ? page_name : "<string>"; |
132 |
0 |
132 |
|
| 749 |
|
| } else { |
| 750 |
102165 |
▶ | set_unit(&r, page_prog, page_incs, |
Branch: 1 branch
| Branch | True | False | Total |
| set_unit(&r, page_prog, page_incs, |
102165 |
0 |
102165 |
|
| 751 |
|
| page_name ? page_name : "<string>"); |
| 752 |
|
| } |
| 753 |
102297 |
| r.data = data; |
| 754 |
102297 |
| r.filters = filters; |
| 755 |
102297 |
| r.flags = flags; |
| 756 |
|
| |
| 757 |
102297 |
▶ | if (eff_stack <= 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eff_stack <= 16) { |
102297 |
0 |
102297 |
|
| 758 |
102297 |
| r.stack = fix_stack; |
| 759 |
|
| } else { |
| 760 |
0 |
| Newx(r.stack, eff_stack, stencil_val); |
| 761 |
0 |
| heap_stack = 1; |
| 762 |
|
| } |
| 763 |
102297 |
▶ | if (eff_frames <= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eff_frames <= 8) { |
102297 |
0 |
102297 |
|
| 764 |
102297 |
| r.frames = fix_frames; |
| 765 |
|
| } else { |
| 766 |
0 |
| Newx(r.frames, eff_frames, stencil_rframe); |
| 767 |
0 |
| heap_frames = 1; |
| 768 |
|
| } |
| 769 |
102297 |
▶ | if (eff_binds <= 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eff_binds <= 16) { |
102297 |
0 |
102297 |
|
| 770 |
102297 |
| r.binds = fix_binds; |
| 771 |
|
| } else { |
| 772 |
0 |
| Newx(r.binds, eff_binds, stencil_rbind); |
| 773 |
0 |
| heap_binds = 1; |
| 774 |
|
| } |
| 775 |
|
| |
| 776 |
|
| /* profiled_size is a capacity high-water (SvLEN), so it is used |
| 777 |
|
| * as-is: adding headroom here would compound with the stored |
| 778 |
|
| * capacity into geometric growth */ |
| 779 |
102297 |
| stencil_buf_init(aTHX_ &r.buf, |
| 780 |
102297 |
▶ | entry_prog->profiled_size |
Branch: 1 branch
| Branch | True | False | Total |
| entry_prog->profiled_size |
101290 |
1007 |
102297 |
|
| 781 |
|
| ? entry_prog->profiled_size : 64); |
| 782 |
102297 |
▶ | if ((page_prog->flags | (wrap_prog ? wrap_prog->flags : 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((page_prog->flags | (wrap_prog ? wrap_prog->flags : 0)) |
132 |
102165 |
102297 |
|
| 783 |
102297 |
▶ | & STENCIL_PROG_SRC_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| & STENCIL_PROG_SRC_UTF8) |
303 |
101994 |
102297 |
|
| 784 |
303 |
| r.buf.utf8 = 1; |
| 785 |
|
| |
| 786 |
|
| #ifdef STENCIL_HAVE_COMPUTED_GOTO |
| 787 |
204594 |
| ok = stencil_dispatch.force_switch |
| 788 |
0 |
| ? stencil_vm_switch(aTHX_ &r) |
| 789 |
102297 |
▶ | : stencil_vm_goto(aTHX_ &r); |
Branch: 1 branch
| Branch | True | False | Total |
| : stencil_vm_goto(aTHX_ &r); |
0 |
102297 |
102297 |
|
| 790 |
|
| #else |
| 791 |
|
| ok = stencil_vm_switch(aTHX_ &r); |
| 792 |
|
| #endif |
| 793 |
|
| |
| 794 |
|
| /* unwind whatever an error path left live */ |
| 795 |
102298 |
▶ | while (r.sp > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (r.sp > 0) { |
1 |
102297 |
102298 |
|
| 796 |
1 |
| r.sp--; |
| 797 |
1 |
| val_release(aTHX_ &r.stack[r.sp]); |
| 798 |
|
| } |
| 799 |
102317 |
▶ | while (r.nf > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (r.nf > 0) |
20 |
102297 |
102317 |
|
| 800 |
20 |
| frame_release(aTHX_ &r.frames[--r.nf]); |
| 801 |
102423 |
▶ | while (r.nb > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (r.nb > 0) |
126 |
102297 |
102423 |
|
| 802 |
126 |
| SvREFCNT_dec(r.binds[--r.nb].sv); |
| 803 |
102297 |
| SvREFCNT_dec(r.scratch[0]); |
| 804 |
102297 |
| SvREFCNT_dec(r.scratch[1]); |
| 805 |
102297 |
▶ | if (heap_stack) |
Branch: 1 branch
| Branch | True | False | Total |
| if (heap_stack) |
0 |
102297 |
102297 |
|
| 806 |
0 |
| Safefree(r.stack); |
| 807 |
102297 |
▶ | if (heap_frames) |
Branch: 1 branch
| Branch | True | False | Total |
| if (heap_frames) |
0 |
102297 |
102297 |
|
| 808 |
0 |
| Safefree(r.frames); |
| 809 |
102297 |
▶ | if (heap_binds) |
Branch: 1 branch
| Branch | True | False | Total |
| if (heap_binds) |
0 |
102297 |
102297 |
|
| 810 |
0 |
| Safefree(r.binds); |
| 811 |
|
| |
| 812 |
102297 |
▶ | if (!ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) { |
40 |
102257 |
102297 |
|
| 813 |
40 |
| SvREFCNT_dec(r.buf.sv); |
| 814 |
40 |
▶ | if (err) |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) |
40 |
0 |
40 |
|
| 815 |
40 |
▶ | *err = sv_2mortal(newSVpvf( |
Branch: 1 branch
| Branch | True | False | Total |
| *err = sv_2mortal(newSVpvf( |
40 |
0 |
40 |
|
| 816 |
|
| "Template::Stencil: %s:%u: %s", |
| 817 |
|
| r.err_final ? r.err_name : r.unit_name, |
| 818 |
|
| (unsigned)r.err_line, r.errbuf)); |
| 819 |
40 |
| return NULL; |
| 820 |
|
| } |
| 821 |
|
| { |
| 822 |
102257 |
| SV *out = stencil_buf_done(&r.buf); |
| 823 |
102257 |
▶ | if (!(flags & STENCIL_RF_CHARS)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(flags & STENCIL_RF_CHARS)) |
102242 |
15 |
102257 |
|
| 824 |
102242 |
| SvUTF8_off(out); /* default: wire-ready UTF-8 bytes */ |
| 825 |
|
| /* profile the capacity high-water (SvLEN, not SvCUR): the |
| 826 |
|
| * escaper reserves worst-case space, and pre-growing to only |
| 827 |
|
| * the final length would re-grow on every render of small |
| 828 |
|
| * templates with escaped values */ |
| 829 |
102257 |
▶ | if (SvLEN(out) > entry_prog->profiled_size) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvLEN(out) > entry_prog->profiled_size) |
102257 |
0 |
102257 |
|
| 830 |
102257 |
| entry_prog->profiled_size = SvLEN(out); |
| 831 |
102257 |
| return out; |
| 832 |
|
| } |
| 833 |
|
| } |
| 834 |
|
| |
| 835 |
100558 |
| SV *stencil_render_run(pTHX_ stencil_program *prog, HV *data, |
| 836 |
|
| uint32_t flags, const char *tname, SV **err) |
| 837 |
|
| { |
| 838 |
100558 |
| return stencil_render_core(aTHX_ prog, NULL, tname, |
| 839 |
|
| NULL, NULL, NULL, |
| 840 |
|
| prog->max_stack, prog->max_frames, |
| 841 |
|
| prog->max_binds, data, NULL, flags, err); |
| 842 |
|
| } |