| 1 |
|
| #include "stencil.h" |
| 2 |
|
| #include "stencil_str.h" |
| 3 |
|
| |
| 4 |
|
| #include <stdarg.h> |
| 5 |
|
| #include <stdlib.h> |
| 6 |
|
| |
| 7 |
|
| UV stencil_stat_scratch_allocs = 0; |
| 8 |
|
| |
| 9 |
|
| /* ==================================================================== |
| 10 |
|
| * Render state |
| 11 |
|
| * ==================================================================== */ |
| 12 |
|
| |
| 13 |
|
| #define STENCIL_VF_ESCAPED 0x1u /* html filter ran; do not re-escape */ |
| 14 |
|
| |
| 15 |
|
| typedef struct stencil_val { |
| 16 |
|
| SV *sv; /* NULL = undef */ |
| 17 |
|
| uint8_t owned; /* consume must SvREFCNT_dec */ |
| 18 |
|
| uint8_t vflags; |
| 19 |
|
| } stencil_val; |
| 20 |
|
| |
| 21 |
|
| typedef struct stencil_khent { |
| 22 |
|
| const char *pv; |
| 23 |
|
| STRLEN len; |
| 24 |
|
| int utf8; |
| 25 |
|
| SV *val; |
| 26 |
|
| } stencil_khent; |
| 27 |
|
| |
| 28 |
|
| /* Scope names are stored as (bytes, len, hash) rather than name ids: |
| 29 |
|
| * name ids are per-program, and an include shares the caller's scope |
| 30 |
|
| * across program boundaries. The pv points into the owning program's |
| 31 |
|
| * arena, which outlives the render. */ |
| 32 |
|
| typedef struct stencil_sname { |
| 33 |
|
| const char *pv; |
| 34 |
|
| uint32_t len; |
| 35 |
|
| uint32_t hash; |
| 36 |
|
| } stencil_sname; |
| 37 |
|
| |
| 38 |
|
| typedef struct stencil_rframe { |
| 39 |
|
| int is_hash; |
| 40 |
|
| AV *av; |
| 41 |
|
| HV *hv; |
| 42 |
|
| stencil_khent *kh; /* sorted key/value vector (hash) */ |
| 43 |
|
| SV *key_sv; /* reused key SV (hash) */ |
| 44 |
|
| SSize_t i, size; |
| 45 |
|
| stencil_sname name; /* array var / hash key var */ |
| 46 |
|
| stencil_sname val; /* hash value var */ |
| 47 |
|
| uint32_t serial; |
| 48 |
|
| SV *cur; /* array element / key_sv */ |
| 49 |
|
| SV *cur_val; /* hash value */ |
| 50 |
|
| SV *agg; /* the iterated aggregate */ |
| 51 |
|
| int agg_owned; |
| 52 |
|
| } stencil_rframe; |
| 53 |
|
| |
| 54 |
|
| typedef struct stencil_rbind { |
| 55 |
|
| stencil_sname name; |
| 56 |
|
| SV *sv; |
| 57 |
|
| uint32_t serial; |
| 58 |
|
| } stencil_rbind; |
| 59 |
|
| |
| 60 |
|
| #define SNAME_EQ(a, b) \ |
| 61 |
|
| ((a).hash == (b).hash && (a).len == (b).len \ |
| 62 |
|
| && memcmp((a).pv, (b).pv, (a).len) == 0) |
| 63 |
|
| |
| 64 |
|
| typedef struct stencil_rstate { |
| 65 |
|
| /* current unit (swapped for includes / content) */ |
| 66 |
|
| stencil_program *prog; |
| 67 |
|
| const uint8_t *code; |
| 68 |
|
| const char *pool; |
| 69 |
|
| const stencil_cname *names; |
| 70 |
|
| const stencil_cpath *paths; |
| 71 |
|
| const stencil_seg *segs; |
| 72 |
|
| const stencil_cfilt *filts; /* current unit's filter table */ |
| 73 |
|
| stencil_cache_ent **incs; /* current unit's include links */ |
| 74 |
|
| const char *unit_name; |
| 75 |
|
| /* content unit for {% content %} (page under a wrapper) */ |
| 76 |
|
| stencil_program *content_prog; |
| 77 |
|
| stencil_cache_ent **content_incs; |
| 78 |
|
| const char *content_name; |
| 79 |
|
| int content_done; |
| 80 |
|
| /* filters */ |
| 81 |
|
| HV *filters; /* engine's user coderef registry */ |
| 82 |
|
| stencil_rx_cache **rx_cache; /* engine's data-pattern cache, or NULL */ |
| 83 |
|
| SV *scratch[2]; /* filter output SVs, round-robin */ |
| 84 |
|
| int scr_i; |
| 85 |
|
| /* shared render state */ |
| 86 |
|
| stencil_buf buf; |
| 87 |
|
| HV *data; |
| 88 |
|
| stencil_val *stack; |
| 89 |
|
| int32_t sp; |
| 90 |
|
| stencil_rframe *frames; |
| 91 |
|
| int32_t nf; |
| 92 |
|
| stencil_rbind *binds; |
| 93 |
|
| int32_t nb; |
| 94 |
|
| uint32_t serial; |
| 95 |
|
| uint32_t flags; |
| 96 |
|
| uint32_t last_path; |
| 97 |
|
| /* error capture: message + the failing unit's name/line */ |
| 98 |
|
| char errbuf[192]; |
| 99 |
|
| int failed; |
| 100 |
|
| int err_final; |
| 101 |
|
| const char *err_name; |
| 102 |
|
| uint32_t err_line; |
| 103 |
|
| } stencil_rstate; |
| 104 |
|
| |
| 105 |
11963 |
| static stencil_sname sname_of(const struct stencil_rstate *r, uint32_t id) |
| 106 |
|
| { |
| 107 |
11963 |
| const stencil_cname *nm = &r->names[id]; |
| 108 |
|
| stencil_sname s; |
| 109 |
11963 |
| s.pv = r->pool + nm->off; |
| 110 |
11963 |
| s.len = nm->len; |
| 111 |
11963 |
| s.hash = nm->hash; |
| 112 |
11963 |
| return s; |
| 113 |
|
| } |
| 114 |
|
| |
| 115 |
105425 |
| static void set_unit(stencil_rstate *r, stencil_program *prog, |
| 116 |
|
| stencil_cache_ent **incs, const char *name) |
| 117 |
|
| { |
| 118 |
105425 |
| r->prog = prog; |
| 119 |
105425 |
| r->code = stencil_prog_code(prog); |
| 120 |
105425 |
| r->pool = stencil_prog_pool(prog); |
| 121 |
105425 |
| r->names = stencil_prog_names(prog); |
| 122 |
105425 |
| r->paths = stencil_prog_paths(prog); |
| 123 |
105425 |
| r->segs = stencil_prog_segs(prog); |
| 124 |
105425 |
| r->filts = stencil_prog_filts(prog); |
| 125 |
105425 |
| r->incs = incs; |
| 126 |
105425 |
| r->unit_name = name; |
| 127 |
105425 |
| } |
| 128 |
|
| |
| 129 |
|
| /* ---- small helpers -------------------------------------------------- */ |
| 130 |
|
| |
| 131 |
351928 |
| STENCIL_INLINE uint32_t vm_u32(const uint8_t *code, uint32_t *pc) |
| 132 |
|
| { |
| 133 |
|
| uint32_t v; |
| 134 |
351928 |
| memcpy(&v, code + *pc, 4); |
| 135 |
351928 |
| *pc += 4; |
| 136 |
351928 |
| return v; |
| 137 |
|
| } |
| 138 |
|
| |
| 139 |
|
| struct stencil_rstate; |
| 140 |
|
| static stencil_sname sname_of(const struct stencil_rstate *r, uint32_t id); |
| 141 |
|
| |
| 142 |
247582 |
| STENCIL_INLINE void val_release(pTHX_ stencil_val *v) |
| 143 |
|
| { |
| 144 |
247582 |
▶ | if (v->owned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->owned) |
21068 |
226514 |
247582 |
|
| 145 |
21068 |
| SvREFCNT_dec_NN(v->sv); |
| 146 |
247582 |
| } |
| 147 |
|
| |
| 148 |
|
| static uint32_t line_at(const stencil_program *prog, uint32_t off); |
| 149 |
|
| |
| 150 |
|
| /* First error wins; the message is captured here, the unit name and |
| 151 |
|
| * line are pinned by rfail_here at the failing op so nested-unit |
| 152 |
|
| * errors report the right template. */ |
| 153 |
38 |
| static int rerror(stencil_rstate *r, const char *fmt, ...) |
| 154 |
|
| { |
| 155 |
|
| va_list ap; |
| 156 |
38 |
▶ | if (r->failed) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->failed) |
0 |
38 |
38 |
|
| 157 |
0 |
| return 0; |
| 158 |
38 |
| r->failed = 1; |
| 159 |
38 |
| va_start(ap, fmt); |
| 160 |
38 |
| vsnprintf(r->errbuf, sizeof r->errbuf, fmt, ap); |
| 161 |
38 |
| va_end(ap); |
| 162 |
38 |
| return 0; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
99 |
| static int rfail_here(stencil_rstate *r, uint32_t at) |
| 166 |
|
| { |
| 167 |
99 |
▶ | if (!r->err_final) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->err_final) { |
99 |
0 |
99 |
|
| 168 |
99 |
| r->err_final = 1; |
| 169 |
99 |
| r->err_name = r->unit_name; |
| 170 |
99 |
| r->err_line = line_at(r->prog, at); |
| 171 |
|
| } |
| 172 |
99 |
| return 0; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
61 |
| static int rfail(stencil_rstate *r, uint32_t at, const char *fmt, ...) |
| 176 |
|
| { |
| 177 |
|
| va_list ap; |
| 178 |
61 |
▶ | if (!r->failed) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!r->failed) { |
61 |
0 |
61 |
|
| 179 |
61 |
| r->failed = 1; |
| 180 |
61 |
| va_start(ap, fmt); |
| 181 |
61 |
| vsnprintf(r->errbuf, sizeof r->errbuf, fmt, ap); |
| 182 |
61 |
| va_end(ap); |
| 183 |
|
| } |
| 184 |
61 |
| return rfail_here(r, at); |
| 185 |
|
| } |
| 186 |
|
| |
| 187 |
51 |
| static int rfail_path(stencil_rstate *r, uint32_t at, uint32_t pid) |
| 188 |
|
| { |
| 189 |
51 |
| const stencil_cpath *p = &r->paths[pid]; |
| 190 |
102 |
| return rfail(r, at, "undef value for '%.*s'", |
| 191 |
51 |
| (int)p->full_len, r->pool + p->full_off); |
| 192 |
|
| } |
| 193 |
|
| |
| 194 |
99 |
| static uint32_t line_at(const stencil_program *prog, uint32_t off) |
| 195 |
|
| { |
| 196 |
99 |
| const stencil_cline *lines = stencil_prog_lines(prog); |
| 197 |
99 |
| uint32_t lo = 0, hi = prog->n_lines, line = 1; |
| 198 |
201 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
102 |
99 |
201 |
|
| 199 |
102 |
| uint32_t mid = (lo + hi) / 2; |
| 200 |
102 |
▶ | if (lines[mid].off <= off) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lines[mid].off <= off) { |
101 |
1 |
102 |
|
| 201 |
101 |
| line = lines[mid].line; |
| 202 |
101 |
| lo = mid + 1; |
| 203 |
|
| } else { |
| 204 |
1 |
| hi = mid; |
| 205 |
|
| } |
| 206 |
|
| } |
| 207 |
99 |
| return line; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
|
| /* Perl truth, extended: an unblessed empty array/hash ref is false, so |
| 211 |
|
| * `{% if items %}` guards a loop naturally. */ |
| 212 |
30639 |
| static int sv_truthy(pTHX_ SV *sv) |
| 213 |
|
| { |
| 214 |
30639 |
▶ | if (!sv || !SvOK(sv)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) |
30619 |
20 |
30639 |
| if (!sv || !SvOK(sv)) |
2 |
30617 |
30619 |
|
| 215 |
22 |
| return 0; |
| 216 |
30617 |
▶ | if (SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
16 |
30601 |
30617 |
| if (SvROK(sv) && !SvOBJECT(SvRV(sv))) { |
16 |
0 |
16 |
|
| 217 |
16 |
| SV *rv = SvRV(sv); |
| 218 |
16 |
▶ | if (SvTYPE(rv) == SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) == SVt_PVAV) |
9 |
7 |
16 |
|
| 219 |
9 |
| return av_top_index((AV *)rv) >= 0; |
| 220 |
7 |
▶ | if (SvTYPE(rv) == SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) == SVt_PVHV) |
7 |
0 |
7 |
|
| 221 |
7 |
▶ | return HvKEYS((HV *)rv) > 0; |
Branch: 1 branch
| Branch | True | False | Total |
| return HvKEYS((HV *)rv) > 0; |
0 |
7 |
7 |
|
| 222 |
|
| } |
| 223 |
30601 |
| return SvTRUE(sv); |
| 224 |
|
| } |
| 225 |
|
| |
| 226 |
|
| /* ---- hash iteration key vector -------------------------------------- */ |
| 227 |
|
| |
| 228 |
2818 |
| static int kh_cmp(const void *pa, const void *pb) |
| 229 |
|
| { |
| 230 |
2818 |
| const stencil_khent *a = (const stencil_khent *)pa; |
| 231 |
2818 |
| const stencil_khent *b = (const stencil_khent *)pb; |
| 232 |
2818 |
| STRLEN min = a->len < b->len ? a->len : b->len; |
| 233 |
2818 |
| int c = memcmp(a->pv, b->pv, min); |
| 234 |
2818 |
▶ | if (c) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c) |
2818 |
0 |
2818 |
|
| 235 |
2818 |
| return c; |
| 236 |
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 |
|
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
342 |
| static stencil_khent *kh_collect(pTHX_ HV *hv, SSize_t size, int sorted) |
| 240 |
|
| { |
| 241 |
|
| stencil_khent *kh; |
| 242 |
|
| HE *he; |
| 243 |
342 |
| SSize_t n = 0; |
| 244 |
342 |
▶ | Newx(kh, (size_t)size, stencil_khent); |
Branch: 1 branch
| Branch | True | False | Total |
| Newx(kh, (size_t)size, stencil_khent); |
0 |
342 |
342 |
|
| 245 |
342 |
| stencil_stat_scratch_allocs++; |
| 246 |
342 |
| hv_iterinit(hv); |
| 247 |
1852 |
▶ | while ((he = hv_iternext(hv)) && n < size) { |
Branch: 2 branches
| Branch | True | False | Total |
| while ((he = hv_iternext(hv)) && n < size) { |
1510 |
342 |
1852 |
| while ((he = hv_iternext(hv)) && n < size) { |
1510 |
0 |
1510 |
|
| 248 |
|
| STRLEN len; |
| 249 |
1510 |
▶ | kh[n].pv = HePV(he, len); |
Branch: 1 branch
| Branch | True | False | Total |
| kh[n].pv = HePV(he, len); |
0 |
1510 |
1510 |
|
| 250 |
1510 |
| kh[n].len = len; |
| 251 |
1510 |
▶ | kh[n].utf8 = HeUTF8(he) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| kh[n].utf8 = HeUTF8(he) ? 1 : 0; |
0 |
1510 |
1510 |
|
| 252 |
1510 |
| kh[n].val = HeVAL(he); |
| 253 |
1510 |
| n++; |
| 254 |
|
| } |
| 255 |
342 |
▶ | if (sorted) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sorted) |
340 |
2 |
342 |
|
| 256 |
340 |
| qsort(kh, (size_t)n, sizeof *kh, kh_cmp); |
| 257 |
342 |
| return kh; |
| 258 |
|
| } |
| 259 |
|
| |
| 260 |
1510 |
| static void kh_bind(pTHX_ stencil_rframe *f, SSize_t i) |
| 261 |
|
| { |
| 262 |
1510 |
| sv_setpvn(f->key_sv, f->kh[i].pv, f->kh[i].len); |
| 263 |
1510 |
▶ | if (f->kh[i].utf8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->kh[i].utf8) |
0 |
1510 |
1510 |
|
| 264 |
0 |
| SvUTF8_on(f->key_sv); |
| 265 |
|
| else |
| 266 |
1510 |
| SvUTF8_off(f->key_sv); |
| 267 |
1510 |
| f->cur = f->key_sv; |
| 268 |
1510 |
| f->cur_val = f->kh[i].val; |
| 269 |
1510 |
| } |
| 270 |
|
| |
| 271 |
1345 |
| static void frame_release(pTHX_ stencil_rframe *f) |
| 272 |
|
| { |
| 273 |
1345 |
▶ | if (f->kh) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->kh) { |
342 |
1003 |
1345 |
|
| 274 |
342 |
| Safefree(f->kh); |
| 275 |
342 |
| f->kh = NULL; |
| 276 |
|
| } |
| 277 |
1345 |
▶ | if (f->key_sv) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->key_sv) { |
342 |
1003 |
1345 |
|
| 278 |
342 |
| SvREFCNT_dec_NN(f->key_sv); |
| 279 |
342 |
| f->key_sv = NULL; |
| 280 |
|
| } |
| 281 |
1345 |
▶ | if (f->agg_owned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->agg_owned) |
0 |
1345 |
1345 |
|
| 282 |
0 |
| SvREFCNT_dec(f->agg); |
| 283 |
1345 |
| } |
| 284 |
|
| |
| 285 |
|
| /* ---- loop metadata (pure C, no HV) ----------------------------------- */ |
| 286 |
|
| |
| 287 |
10797 |
| static SV *loop_meta(pTHX_ const stencil_rframe *f, const char *n, |
| 288 |
|
| uint32_t len, int *owned) |
| 289 |
|
| { |
| 290 |
10797 |
| switch (len) { |
| 291 |
6 |
| case 3: |
| 292 |
6 |
▶ | if (memcmp(n, "key", 3) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "key", 3) == 0) |
6 |
0 |
6 |
|
| 293 |
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 |
|
| 294 |
0 |
▶ | if (memcmp(n, "odd", 3) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "odd", 3) == 0) |
0 |
0 |
0 |
|
| 295 |
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 |
|
| 296 |
0 |
| break; |
| 297 |
10082 |
| case 4: |
| 298 |
10082 |
▶ | if (memcmp(n, "size", 4) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "size", 4) == 0) { |
26 |
10056 |
10082 |
|
| 299 |
26 |
| *owned = 1; |
| 300 |
26 |
| return newSViv((IV)f->size); |
| 301 |
|
| } |
| 302 |
10056 |
▶ | if (memcmp(n, "last", 4) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "last", 4) == 0) |
10044 |
12 |
10056 |
|
| 303 |
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 |
|
| 304 |
12 |
▶ | if (memcmp(n, "even", 4) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "even", 4) == 0) |
12 |
0 |
12 |
|
| 305 |
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 |
|
| 306 |
0 |
| break; |
| 307 |
676 |
| case 5: |
| 308 |
676 |
▶ | if (memcmp(n, "index", 5) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "index", 5) == 0) { |
602 |
74 |
676 |
|
| 309 |
602 |
| *owned = 1; |
| 310 |
602 |
| return newSViv((IV)f->i); |
| 311 |
|
| } |
| 312 |
74 |
▶ | if (memcmp(n, "first", 5) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "first", 5) == 0) |
74 |
0 |
74 |
|
| 313 |
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 |
|
| 314 |
0 |
| break; |
| 315 |
33 |
| case 6: |
| 316 |
33 |
▶ | if (memcmp(n, "index1", 6) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (memcmp(n, "index1", 6) == 0) { |
33 |
0 |
33 |
|
| 317 |
33 |
| *owned = 1; |
| 318 |
33 |
| return newSViv((IV)(f->i + 1)); |
| 319 |
|
| } |
| 320 |
0 |
| break; |
| 321 |
|
| } |
| 322 |
0 |
| return NULL; |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
|
| /* Materialise the frame as a real HV - the only place a loop HV is |
| 326 |
|
| * ever built (capture via `set x = loop`). */ |
| 327 |
123 |
| static SV *loop_snapshot(pTHX_ const stencil_rframe *f) |
| 328 |
|
| { |
| 329 |
123 |
| HV *hv = newHV(); |
| 330 |
123 |
| (void)hv_stores(hv, "index", newSViv((IV)f->i)); |
| 331 |
123 |
| (void)hv_stores(hv, "index1", newSViv((IV)(f->i + 1))); |
| 332 |
123 |
| (void)hv_stores(hv, "size", newSViv((IV)f->size)); |
| 333 |
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 |
|
| 334 |
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 |
|
| 335 |
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 |
|
| 336 |
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 |
|
| 337 |
123 |
▶ | if (f->is_hash) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->is_hash) |
0 |
123 |
123 |
|
| 338 |
0 |
| (void)hv_stores(hv, "key", newSVsv(f->cur)); |
| 339 |
123 |
| return newRV_noinc((SV *)hv); |
| 340 |
|
| } |
| 341 |
|
| |
| 342 |
|
| /* ---- path resolution -------------------------------------------------- */ |
| 343 |
|
| |
| 344 |
|
| /* One key out of a hash, honouring magic. |
| 345 |
|
| * |
| 346 |
|
| * The fast path is hv_common with the hash this engine precomputed at |
| 347 |
|
| * compile time, and that is what every ordinary hash gets: no key SV, no |
| 348 |
|
| * rehash, one probe. |
| 349 |
|
| * |
| 350 |
|
| * It does not go through tie magic, though, and the way it fails is the |
| 351 |
|
| * problem. A tied hash read that way comes back EMPTY - not an error, not a |
| 352 |
|
| * wrong value, nothing - so a template handed one renders blanks and says |
| 353 |
|
| * nothing about why. That is a trap for any tied hash a caller passes in, |
| 354 |
|
| * and tied hashes are how a caller hands a template something it should not |
| 355 |
|
| * have to materialise up front: a big catalogue, a lazy record, a database |
| 356 |
|
| * row. |
| 357 |
|
| * |
| 358 |
|
| * So: anything magical takes hv_fetch_ent instead, which is the call perl's |
| 359 |
|
| * own $h{k} makes. The cost to everything else is one bit test. |
| 360 |
|
| */ |
| 361 |
129699 |
| static SV *stencil_hv_get(pTHX_ HV *hv, const char *pv, STRLEN len, |
| 362 |
|
| U32 hash, int kflags) |
| 363 |
|
| { |
| 364 |
129699 |
▶ | if (UNLIKELY(SvRMAGICAL((const SV *)hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (UNLIKELY(SvRMAGICAL((const SV *)hv))) { |
21 |
129678 |
129699 |
|
| 365 |
21 |
| SV *k = sv_2mortal(newSVpvn(pv, len)); |
| 366 |
|
| HE *he; |
| 367 |
|
| SV *v; |
| 368 |
21 |
▶ | if (kflags & HVhek_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (kflags & HVhek_UTF8) |
0 |
21 |
21 |
|
| 369 |
0 |
| SvUTF8_on(k); |
| 370 |
21 |
| he = hv_fetch_ent(hv, k, 0, 0); |
| 371 |
21 |
▶ | if (!he) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!he) |
0 |
21 |
21 |
|
| 372 |
0 |
| return NULL; |
| 373 |
21 |
| v = HeVAL(he); |
| 374 |
|
| /* What comes back from a tied hash is a PVLV carrying GET MAGIC, not |
| 375 |
|
| * the value: the tie's FETCH runs when the magic is read. Everything |
| 376 |
|
| * downstream here asks SvOK and SvPV, neither of which fires it, so |
| 377 |
|
| * without this the value reads as undef and the tag renders empty - |
| 378 |
|
| * which is the bug this branch exists to fix, arrived at one level |
| 379 |
|
| * further in. */ |
| 380 |
21 |
▶ | if (v) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) |
21 |
0 |
21 |
|
| 381 |
21 |
▶ | SvGETMAGIC(v); |
Branch: 2 branches
| Branch | True | False | Total |
| SvGETMAGIC(v); |
21 |
0 |
21 |
| SvGETMAGIC(v); |
0 |
21 |
21 |
|
| 382 |
21 |
| return v; |
| 383 |
|
| } |
| 384 |
|
| { |
| 385 |
129678 |
| SV **svp = (SV **)hv_common(hv, NULL, pv, len, kflags, |
| 386 |
|
| HV_FETCH_JUST_SV, NULL, hash); |
| 387 |
129678 |
▶ | return svp ? *svp : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| return svp ? *svp : NULL; |
129541 |
137 |
129678 |
|
| 388 |
|
| } |
| 389 |
|
| } |
| 390 |
|
| |
| 391 |
170604 |
| static SV *resolve_path(pTHX_ stencil_rstate *r, uint32_t pid, int *owned) |
| 392 |
|
| { |
| 393 |
170604 |
| const stencil_cpath *path = &r->paths[pid]; |
| 394 |
170604 |
| const stencil_seg *segs = r->segs + path->seg_idx; |
| 395 |
|
| uint32_t i; |
| 396 |
170604 |
| SV *cur = NULL; |
| 397 |
170604 |
| int kflags = |
| 398 |
170604 |
| (r->prog->flags & STENCIL_PROG_SRC_UTF8) ? HVhek_UTF8 : 0; |
| 399 |
|
| |
| 400 |
170604 |
| *owned = 0; |
| 401 |
|
| |
| 402 |
170604 |
▶ | if (path->loop_rooted && r->nf > 0) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (path->loop_rooted && r->nf > 0) { |
10921 |
159683 |
170604 |
| if (path->loop_rooted && r->nf > 0) { |
10920 |
1 |
10921 |
|
| 403 |
10920 |
| const stencil_rframe *f = &r->frames[r->nf - 1]; |
| 404 |
10920 |
▶ | if (path->n_segs == 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (path->n_segs == 1) { |
123 |
10797 |
10920 |
|
| 405 |
123 |
| *owned = 1; |
| 406 |
123 |
| return loop_snapshot(aTHX_ f); |
| 407 |
|
| } |
| 408 |
10797 |
▶ | if (segs[1].is_index) |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs[1].is_index) |
0 |
10797 |
10797 |
|
| 409 |
0 |
| return NULL; |
| 410 |
|
| { |
| 411 |
10797 |
| const stencil_cname *nm = &r->names[segs[1].name_id]; |
| 412 |
10797 |
| cur = loop_meta(aTHX_ f, r->pool + nm->off, nm->len, owned); |
| 413 |
|
| } |
| 414 |
10797 |
▶ | if (!cur) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cur) |
0 |
10797 |
10797 |
|
| 415 |
0 |
| return NULL; |
| 416 |
10797 |
| i = 2; |
| 417 |
|
| } else { |
| 418 |
|
| /* first segment: innermost scope wins (serial-stamped binds |
| 419 |
|
| * and loop frames, matched by name bytes so includes share the |
| 420 |
|
| * caller's scope), then the root data hash */ |
| 421 |
159684 |
| const stencil_cname *nm = &r->names[segs[0].name_id]; |
| 422 |
|
| stencil_sname seg0; |
| 423 |
159684 |
| SV *best = NULL; |
| 424 |
159684 |
| uint32_t best_serial = 0; |
| 425 |
159684 |
| int found = 0; |
| 426 |
|
| int32_t k; |
| 427 |
159684 |
| seg0.pv = r->pool + nm->off; |
| 428 |
159684 |
| seg0.len = nm->len; |
| 429 |
159684 |
| seg0.hash = nm->hash; |
| 430 |
160212 |
▶ | for (k = r->nb - 1; k >= 0; k--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = r->nb - 1; k >= 0; k--) { |
5986 |
154226 |
160212 |
|
| 431 |
5986 |
▶ | if (SNAME_EQ(r->binds[k].name, seg0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SNAME_EQ(r->binds[k].name, seg0)) { |
5458 |
528 |
5986 |
| if (SNAME_EQ(r->binds[k].name, seg0)) { |
5458 |
0 |
5458 |
| if (SNAME_EQ(r->binds[k].name, seg0)) { |
5458 |
0 |
5458 |
|
| 432 |
5458 |
| best = r->binds[k].sv; |
| 433 |
5458 |
| best_serial = r->binds[k].serial; |
| 434 |
5458 |
| found = 1; |
| 435 |
5458 |
| break; |
| 436 |
|
| } |
| 437 |
|
| } |
| 438 |
168425 |
▶ | for (k = r->nf - 1; k >= 0; k--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = r->nf - 1; k >= 0; k--) { |
33917 |
134508 |
168425 |
|
| 439 |
33917 |
| const stencil_rframe *f = &r->frames[k]; |
| 440 |
33917 |
| SV *hit = NULL; |
| 441 |
33917 |
| int match = 0; |
| 442 |
33917 |
▶ | if (SNAME_EQ(f->name, seg0)) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SNAME_EQ(f->name, seg0)) { |
24709 |
9208 |
33917 |
| if (SNAME_EQ(f->name, seg0)) { |
24709 |
0 |
24709 |
| if (SNAME_EQ(f->name, seg0)) { |
24709 |
0 |
24709 |
|
| 443 |
24709 |
| hit = f->cur; |
| 444 |
24709 |
| match = 1; |
| 445 |
9208 |
▶ | } 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)) { |
467 |
8741 |
9208 |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
467 |
0 |
467 |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
467 |
0 |
467 |
| } else if (f->is_hash && SNAME_EQ(f->val, seg0)) { |
467 |
0 |
467 |
|
| 446 |
467 |
| hit = f->cur_val; |
| 447 |
467 |
| match = 1; |
| 448 |
|
| } |
| 449 |
33917 |
▶ | if (match) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (match) { |
25176 |
8741 |
33917 |
|
| 450 |
25176 |
▶ | if (!found || f->serial > best_serial) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!found || f->serial > best_serial) |
2 |
25174 |
25176 |
| if (!found || f->serial > best_serial) |
0 |
2 |
2 |
|
| 451 |
25174 |
| best = hit; |
| 452 |
25176 |
| found = 1; |
| 453 |
25176 |
| break; |
| 454 |
|
| } |
| 455 |
|
| } |
| 456 |
159684 |
▶ | if (found) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (found) { |
30632 |
129052 |
159684 |
|
| 457 |
30632 |
| cur = best; |
| 458 |
|
| } else { |
| 459 |
129052 |
| cur = stencil_hv_get(aTHX_ r->data, seg0.pv, seg0.len, |
| 460 |
|
| seg0.hash, kflags); |
| 461 |
|
| } |
| 462 |
159684 |
▶ | if (!cur) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cur) |
116 |
159568 |
159684 |
|
| 463 |
116 |
| return NULL; |
| 464 |
159568 |
| i = 1; |
| 465 |
|
| } |
| 466 |
|
| |
| 467 |
171007 |
▶ | for (; i < path->n_segs; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; i < path->n_segs; i++) { |
670 |
170337 |
171007 |
|
| 468 |
|
| SV *rv; |
| 469 |
670 |
▶ | if (!SvROK(cur)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(cur)) |
3 |
667 |
670 |
|
| 470 |
3 |
| return NULL; |
| 471 |
667 |
| rv = SvRV(cur); |
| 472 |
667 |
▶ | if (SvOBJECT(rv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvOBJECT(rv)) { |
2 |
665 |
667 |
|
| 473 |
2 |
| rerror(r, "cannot traverse blessed reference in '%.*s'", |
| 474 |
2 |
| (int)path->full_len, r->pool + path->full_off); |
| 475 |
2 |
| return NULL; |
| 476 |
|
| } |
| 477 |
665 |
▶ | if (*owned) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*owned) { |
0 |
665 |
665 |
|
| 478 |
|
| /* intermediate owned value (loop meta deref) - hand its |
| 479 |
|
| * lifetime to the temp stack */ |
| 480 |
0 |
| sv_2mortal(cur); |
| 481 |
0 |
| *owned = 0; |
| 482 |
|
| } |
| 483 |
665 |
▶ | if (segs[i].is_index) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (segs[i].is_index) { |
18 |
647 |
665 |
|
| 484 |
|
| SV **svp; |
| 485 |
18 |
▶ | if (SvTYPE(rv) != SVt_PVAV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) != SVt_PVAV) |
0 |
18 |
18 |
|
| 486 |
0 |
| return NULL; |
| 487 |
18 |
| svp = av_fetch((AV *)rv, segs[i].index, 0); |
| 488 |
18 |
▶ | cur = svp ? *svp : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| cur = svp ? *svp : NULL; |
16 |
2 |
18 |
|
| 489 |
|
| } else { |
| 490 |
647 |
| const stencil_cname *nm = &r->names[segs[i].name_id]; |
| 491 |
647 |
▶ | if (SvTYPE(rv) != SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(rv) != SVt_PVHV) |
0 |
647 |
647 |
|
| 492 |
0 |
| return NULL; |
| 493 |
647 |
| cur = stencil_hv_get(aTHX_ (HV *)rv, r->pool + nm->off, |
| 494 |
647 |
| nm->len, nm->hash, kflags); |
| 495 |
|
| } |
| 496 |
665 |
▶ | if (!cur) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cur) |
23 |
642 |
665 |
|
| 497 |
23 |
| return NULL; |
| 498 |
|
| } |
| 499 |
170337 |
| return cur; |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
|
| /* ---- output encoding --------------------------------------------------- */ |
| 503 |
|
| |
| 504 |
|
| /* The buffer always holds valid UTF-8 bytes: UTF8-flagged values pass |
| 505 |
|
| * through, and unflagged values containing high bytes (perl's latin-1 |
| 506 |
|
| * internal form) are upgraded at print time. The engine owns encoding; |
| 507 |
|
| * callers never utf8::encode. */ |
| 508 |
|
| |
| 509 |
117588 |
| static int has_high_byte(const char *p, STRLEN n) |
| 510 |
|
| { |
| 511 |
117588 |
| const char *end = p + n; |
| 512 |
318340 |
▶ | while ((size_t)(end - p) >= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((size_t)(end - p) >= 8) { |
200752 |
117588 |
318340 |
|
| 513 |
|
| uint64_t v; |
| 514 |
200752 |
| memcpy(&v, p, 8); |
| 515 |
200752 |
▶ | if (v & 0x8080808080808080ULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (v & 0x8080808080808080ULL) |
0 |
200752 |
200752 |
|
| 516 |
0 |
| return 1; |
| 517 |
200752 |
| p += 8; |
| 518 |
|
| } |
| 519 |
447750 |
▶ | while (p < end) |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) |
330166 |
117584 |
447750 |
|
| 520 |
330166 |
▶ | if ((unsigned char)*p++ & 0x80) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((unsigned char)*p++ & 0x80) |
4 |
330162 |
330166 |
|
| 521 |
4 |
| return 1; |
| 522 |
117584 |
| return 0; |
| 523 |
|
| } |
| 524 |
|
| |
| 525 |
|
| static SV *scratch_next(pTHX_ stencil_rstate *r); |
| 526 |
|
| |
| 527 |
4 |
| static const char *latin1_upgrade(pTHX_ stencil_rstate *r, |
| 528 |
|
| const char *p, STRLEN *np) |
| 529 |
|
| { |
| 530 |
4 |
| SV *sc = scratch_next(aTHX_ r); |
| 531 |
4 |
| const char *end = p + *np; |
| 532 |
|
| char *w; |
| 533 |
4 |
▶ | SvUPGRADE(sc, SVt_PV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE(sc, SVt_PV); |
0 |
4 |
4 |
|
| 534 |
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 |
|
| 535 |
4 |
| SvPOK_only(sc); |
| 536 |
4 |
| w = SvPVX(sc); |
| 537 |
17 |
▶ | while (p < end) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (p < end) { |
13 |
4 |
17 |
|
| 538 |
13 |
| unsigned char c = (unsigned char)*p++; |
| 539 |
13 |
▶ | if (c < 0x80) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0x80) { |
9 |
4 |
13 |
|
| 540 |
9 |
| *w++ = (char)c; |
| 541 |
|
| } else { |
| 542 |
4 |
| *w++ = (char)(0xC0 | (c >> 6)); |
| 543 |
4 |
| *w++ = (char)(0x80 | (c & 0x3F)); |
| 544 |
|
| } |
| 545 |
|
| } |
| 546 |
4 |
| *np = (STRLEN)(w - SvPVX(sc)); |
| 547 |
4 |
| SvCUR_set(sc, *np); |
| 548 |
4 |
| return SvPVX(sc); |
| 549 |
|
| } |
| 550 |
|
| |
| 551 |
|
| /* ---- filter pipeline --------------------------------------------------- */ |
| 552 |
|
| |
| 553 |
51367 |
| static SV *scratch_next(pTHX_ stencil_rstate *r) |
| 554 |
|
| { |
| 555 |
51367 |
| SV **slot = &r->scratch[r->scr_i ^= 1]; |
| 556 |
51367 |
▶ | if (!*slot) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*slot) |
51015 |
352 |
51367 |
|
| 557 |
51015 |
| *slot = newSV(64); |
| 558 |
51367 |
| return *slot; |
| 559 |
|
| } |
| 560 |
|
| |
| 561 |
|
| /* Apply filter table entry `idx` (current unit) to the stack top in |
| 562 |
|
| * place. Returns 0 with the error message set (caller pins location). */ |
| 563 |
51499 |
| static int filter_apply(pTHX_ stencil_rstate *r, uint32_t idx) |
| 564 |
|
| { |
| 565 |
51499 |
| const stencil_cfilt *f = &r->filts[idx]; |
| 566 |
51499 |
| stencil_val *v = &r->stack[r->sp - 1]; |
| 567 |
51499 |
| SV *in = v->sv; |
| 568 |
|
| SV *out; |
| 569 |
|
| |
| 570 |
51499 |
▶ | if (f->builtin_id >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->builtin_id >= 0) { |
51485 |
14 |
51499 |
|
| 571 |
51485 |
▶ | if (f->builtin_id == STENCIL_FILT_DEFAULT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->builtin_id == STENCIL_FILT_DEFAULT) { |
130 |
51355 |
51485 |
|
| 572 |
130 |
| STRLEN n = 0; |
| 573 |
130 |
▶ | if (in && SvOK(in)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (in && SvOK(in)) { |
118 |
12 |
130 |
| if (in && SvOK(in)) { |
117 |
1 |
118 |
|
| 574 |
|
| (void)SvPV(in, n); |
| 575 |
117 |
▶ | if (n) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n) |
116 |
1 |
117 |
|
| 576 |
116 |
| return 1; /* passthrough, flags untouched */ |
| 577 |
|
| } |
| 578 |
14 |
| out = scratch_next(aTHX_ r); |
| 579 |
14 |
▶ | if (f->arg_is_num) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->arg_is_num) { |
2 |
12 |
14 |
|
| 580 |
2 |
| sv_setnv(out, (NV)f->num_arg); |
| 581 |
|
| } else { |
| 582 |
12 |
| sv_setpvn(out, r->pool + f->str_off, f->str_len); |
| 583 |
12 |
▶ | if (r->prog->flags & STENCIL_PROG_SRC_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->prog->flags & STENCIL_PROG_SRC_UTF8) |
0 |
12 |
12 |
|
| 584 |
0 |
| SvUTF8_on(out); |
| 585 |
|
| else |
| 586 |
12 |
| SvUTF8_off(out); |
| 587 |
|
| } |
| 588 |
|
| } else { |
| 589 |
51355 |
▶ | if (!in || !SvOK(in)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!in || !SvOK(in)) |
51350 |
5 |
51355 |
| if (!in || !SvOK(in)) |
1 |
51349 |
51350 |
|
| 590 |
6 |
| return 1; /* undef passes through the others */ |
| 591 |
51349 |
| out = scratch_next(aTHX_ r); |
| 592 |
51349 |
| switch (f->builtin_id) { |
| 593 |
50625 |
| case STENCIL_FILT_UPPER: |
| 594 |
50625 |
| stencil_filt_case(aTHX_ in, out, 1); |
| 595 |
50625 |
| break; |
| 596 |
319 |
| case STENCIL_FILT_LOWER: |
| 597 |
319 |
| stencil_filt_case(aTHX_ in, out, 0); |
| 598 |
319 |
| break; |
| 599 |
346 |
| case STENCIL_FILT_TRIM: |
| 600 |
346 |
| stencil_filt_trim(aTHX_ in, out); |
| 601 |
346 |
| break; |
| 602 |
12 |
| case STENCIL_FILT_HTML: |
| 603 |
12 |
| stencil_filt_html(aTHX_ in, out); |
| 604 |
12 |
| break; |
| 605 |
12 |
| case STENCIL_FILT_URI: |
| 606 |
12 |
| stencil_filt_uri(aTHX_ in, out); |
| 607 |
12 |
| break; |
| 608 |
35 |
| case STENCIL_FILT_FMT: |
| 609 |
35 |
| stencil_filt_fmt(aTHX_ in, out, |
| 610 |
35 |
| r->pool + f->str_off, f->str_len, |
| 611 |
35 |
| !!(r->prog->flags & STENCIL_PROG_SRC_UTF8)); |
| 612 |
35 |
| break; |
| 613 |
0 |
| default: |
| 614 |
0 |
| return rerror(r, "corrupt filter table"); |
| 615 |
|
| } |
| 616 |
|
| } |
| 617 |
51363 |
| val_release(aTHX_ v); |
| 618 |
51363 |
| v->sv = out; |
| 619 |
51363 |
| v->owned = 0; |
| 620 |
51363 |
▶ | v->vflags = f->builtin_id == STENCIL_FILT_HTML |
Branch: 1 branch
| Branch | True | False | Total |
| v->vflags = f->builtin_id == STENCIL_FILT_HTML |
12 |
51351 |
51363 |
|
| 621 |
|
| ? STENCIL_VF_ESCAPED : 0; |
| 622 |
51363 |
| return 1; |
| 623 |
|
| } |
| 624 |
|
| |
| 625 |
|
| /* engine-registered Perl coderef */ |
| 626 |
|
| { |
| 627 |
28 |
| SV **cvp = r->filters |
| 628 |
14 |
| ? hv_fetch(r->filters, r->pool + f->name_off, |
| 629 |
|
| (I32)f->name_len, 0) |
| 630 |
14 |
▶ | : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| : NULL; |
14 |
0 |
14 |
|
| 631 |
14 |
| SV *copy = NULL; |
| 632 |
|
| int died; |
| 633 |
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 |
|
| 634 |
0 |
| return rerror(r, "unknown filter '%.*s'", |
| 635 |
0 |
| (int)f->name_len, r->pool + f->name_off); |
| 636 |
|
| { |
| 637 |
14 |
| dSP; |
| 638 |
14 |
| ENTER; |
| 639 |
14 |
| SAVETMPS; |
| 640 |
14 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
14 |
14 |
|
| 641 |
14 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
14 |
14 |
|
| 642 |
14 |
▶ | PUSHs(in ? in : &PL_sv_undef); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHs(in ? in : &PL_sv_undef); |
14 |
0 |
14 |
|
| 643 |
14 |
▶ | if (f->has_arg) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (f->has_arg) { |
3 |
11 |
14 |
|
| 644 |
6 |
| SV *arg = f->arg_is_num |
| 645 |
2 |
| ? newSVnv((NV)f->num_arg) |
| 646 |
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 |
|
| 647 |
3 |
▶ | if (!f->arg_is_num |
Branch: 1 branch
| Branch | True | False | Total |
| if (!f->arg_is_num |
1 |
2 |
3 |
|
| 648 |
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 |
|
| 649 |
0 |
| SvUTF8_on(arg); |
| 650 |
3 |
| PUSHs(sv_2mortal(arg)); |
| 651 |
|
| } |
| 652 |
14 |
| PUTBACK; |
| 653 |
14 |
| (void)call_sv(*cvp, G_SCALAR | G_EVAL); |
| 654 |
14 |
| SPAGAIN; |
| 655 |
14 |
▶ | died = SvTRUE(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV); |
14 |
0 |
14 |
|
| 656 |
14 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
1 |
13 |
14 |
|
| 657 |
1 |
| (void)POPs; |
| 658 |
|
| else |
| 659 |
13 |
| copy = newSVsv(POPs); |
| 660 |
14 |
| PUTBACK; |
| 661 |
14 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
14 |
0 |
14 |
|
| 662 |
14 |
| LEAVE; |
| 663 |
|
| } |
| 664 |
14 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
1 |
13 |
14 |
|
| 665 |
2 |
| return rerror(r, "filter '%.*s' died: %s", |
| 666 |
1 |
| (int)f->name_len, r->pool + f->name_off, |
| 667 |
1 |
▶ | SvPV_nolen(ERRSV)); |
Branch: 1 branch
| Branch | True | False | Total |
| SvPV_nolen(ERRSV)); |
1 |
0 |
1 |
|
| 668 |
13 |
| val_release(aTHX_ v); |
| 669 |
13 |
| v->sv = copy; |
| 670 |
13 |
| v->owned = 1; |
| 671 |
13 |
| v->vflags = 0; |
| 672 |
13 |
| return 1; |
| 673 |
|
| } |
| 674 |
|
| } |
| 675 |
|
| |
| 676 |
|
| /* ---- {% switch %}: evaluating a case ------------------------------------ |
| 677 |
|
| * |
| 678 |
|
| * The topic sits at stack[sp-1] for the whole block and is only read here. |
| 679 |
|
| * Switch::Declare's rules: an undef topic matches only `case undef` and no |
| 680 |
|
| * other pattern is evaluated (a predicate is not called); a numeric pattern |
| 681 |
|
| * needs a topic that looks like a number and compares as perl's == does, |
| 682 |
|
| * exactly for integers; a string pattern compares with eq, by code point |
| 683 |
|
| * across latin-1 and UTF-8. The topic was fetched once when pushed, so |
| 684 |
|
| * nothing here fires get magic again, and nothing allocates: an integer is |
| 685 |
|
| * stringified into a stack buffer, never cached in the caller's SV. */ |
| 686 |
|
| |
| 687 |
|
| typedef struct stencil_num { |
| 688 |
|
| int is_int, is_uv; |
| 689 |
|
| IV iv; |
| 690 |
|
| NV nv; |
| 691 |
|
| } stencil_num; |
| 692 |
|
| |
| 693 |
|
| /* a grok_number result into a stencil_num; 0 if not an exact integer */ |
| 694 |
29 |
| static int num_from_grok(int grok, UV uv, stencil_num *n) |
| 695 |
|
| { |
| 696 |
29 |
▶ | if (!(grok & IS_NUMBER_IN_UV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(grok & IS_NUMBER_IN_UV) |
28 |
1 |
29 |
|
| 697 |
28 |
▶ | || (grok & (IS_NUMBER_NOT_INT | IS_NUMBER_GREATER_THAN_UV_MAX))) |
Branch: 1 branch
| Branch | True | False | Total |
| || (grok & (IS_NUMBER_NOT_INT | IS_NUMBER_GREATER_THAN_UV_MAX))) |
3 |
25 |
28 |
|
| 698 |
4 |
| return 0; |
| 699 |
25 |
| n->is_uv = 0; |
| 700 |
25 |
▶ | if (grok & IS_NUMBER_NEG) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (grok & IS_NUMBER_NEG) { |
0 |
25 |
25 |
|
| 701 |
0 |
▶ | if (uv <= (UV)IV_MAX) |
Branch: 1 branch
| Branch | True | False | Total |
| if (uv <= (UV)IV_MAX) |
0 |
0 |
0 |
|
| 702 |
0 |
| n->iv = -(IV)uv; |
| 703 |
0 |
▶ | else if (uv == (UV)IV_MAX + 1) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (uv == (UV)IV_MAX + 1) |
0 |
0 |
0 |
|
| 704 |
0 |
| n->iv = IV_MIN; |
| 705 |
|
| else |
| 706 |
0 |
| return 0; |
| 707 |
|
| } else { |
| 708 |
25 |
| n->iv = (IV)uv; |
| 709 |
25 |
| n->is_uv = uv > (UV)IV_MAX; |
| 710 |
|
| } |
| 711 |
25 |
| n->is_int = 1; |
| 712 |
25 |
▶ | n->nv = n->is_uv ? (NV)uv : (NV)n->iv; |
Branch: 1 branch
| Branch | True | False | Total |
| n->nv = n->is_uv ? (NV)uv : (NV)n->iv; |
0 |
25 |
25 |
|
| 713 |
25 |
| return 1; |
| 714 |
|
| } |
| 715 |
|
| |
| 716 |
|
| /* The numeric value of a defined SV, if it looks like a number: the same |
| 717 |
|
| * test as perl's looks_like_number, done inline so a string is parsed once |
| 718 |
|
| * (grok_number) rather than once to test and again to read. Integers come |
| 719 |
|
| * back exact. A string is read, not numified in place, so the caller's SV |
| 720 |
|
| * is left as it was. */ |
| 721 |
7894 |
| static int sv_num(pTHX_ SV *sv, stencil_num *n) |
| 722 |
|
| { |
| 723 |
7894 |
| U32 flags = SvFLAGS(sv); |
| 724 |
7894 |
| const char *pv = NULL; |
| 725 |
7894 |
| STRLEN len = 0; |
| 726 |
7894 |
| UV uv = 0; |
| 727 |
7894 |
| int grok = 0; |
| 728 |
7894 |
| n->is_int = n->is_uv = 0; |
| 729 |
7894 |
| n->iv = 0; |
| 730 |
7894 |
▶ | if (flags & SVp_POK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & SVp_POK) { |
7142 |
752 |
7894 |
|
| 731 |
7142 |
| pv = SvPV_nomg_const(sv, len); |
| 732 |
7142 |
| grok = grok_number(pv, len, &uv); |
| 733 |
7142 |
▶ | if (!grok) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!grok) |
7113 |
29 |
7142 |
|
| 734 |
7113 |
| return 0; /* not a number: a miss, no warning */ |
| 735 |
752 |
▶ | } else if (!(flags & (SVf_IOK | SVp_IOK | SVf_NOK | SVp_NOK))) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!(flags & (SVf_IOK | SVp_IOK | SVf_NOK | SVp_NOK))) { |
401 |
351 |
752 |
|
| 736 |
401 |
| return 0; /* a reference, or no value at all */ |
| 737 |
|
| } |
| 738 |
|
| /* the value perl's == would use: an integer when perl holds one, else |
| 739 |
|
| * its float, else the string's own numification */ |
| 740 |
380 |
▶ | if ((flags & SVf_IOK) |
Branch: 1 branch
| Branch | True | False | Total |
| if ((flags & SVf_IOK) |
42 |
338 |
380 |
|
| 741 |
42 |
▶ | || ((flags & SVp_IOK) && !(flags & (SVp_NOK | SVp_POK)))) { |
Branch: 2 branches
| Branch | True | False | Total |
| || ((flags & SVp_IOK) && !(flags & (SVp_NOK | SVp_POK)))) { |
0 |
42 |
42 |
| || ((flags & SVp_IOK) && !(flags & (SVp_NOK | SVp_POK)))) { |
0 |
0 |
0 |
|
| 742 |
338 |
| n->is_int = 1; |
| 743 |
338 |
| n->is_uv = SvIsUV(sv) ? 1 : 0; |
| 744 |
338 |
| n->iv = SvIVX(sv); |
| 745 |
338 |
▶ | n->nv = n->is_uv ? (NV)SvUVX(sv) : (NV)n->iv; |
Branch: 1 branch
| Branch | True | False | Total |
| n->nv = n->is_uv ? (NV)SvUVX(sv) : (NV)n->iv; |
1 |
337 |
338 |
|
| 746 |
42 |
▶ | } else if (flags & SVp_NOK) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (flags & SVp_NOK) { |
13 |
29 |
42 |
|
| 747 |
13 |
| n->nv = SvNVX(sv); |
| 748 |
29 |
▶ | } else if (!num_from_grok(grok, uv, n)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (!num_from_grok(grok, uv, n)) { |
4 |
25 |
29 |
|
| 749 |
4 |
| n->nv = my_atof(pv); /* perl's own numification */ |
| 750 |
|
| } |
| 751 |
380 |
| return 1; |
| 752 |
|
| } |
| 753 |
|
| |
| 754 |
338 |
| static void num_from_cval(const stencil_cval *v, stencil_num *n) |
| 755 |
|
| { |
| 756 |
338 |
| n->is_int = v->is_int; |
| 757 |
338 |
| n->is_uv = v->is_uv; |
| 758 |
338 |
| n->iv = v->iv; |
| 759 |
338 |
| n->nv = v->nv; |
| 760 |
338 |
| } |
| 761 |
|
| |
| 762 |
|
| /* -1, 0, 1 - or 2 when unordered (a NaN) */ |
| 763 |
358 |
| static int num_cmp(const stencil_num *a, const stencil_num *b) |
| 764 |
|
| { |
| 765 |
358 |
▶ | if (a->is_int && b->is_int) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (a->is_int && b->is_int) { |
342 |
16 |
358 |
| if (a->is_int && b->is_int) { |
337 |
5 |
342 |
|
| 766 |
337 |
▶ | if (a->is_uv || b->is_uv) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (a->is_uv || b->is_uv) { |
336 |
1 |
337 |
| if (a->is_uv || b->is_uv) { |
1 |
335 |
336 |
|
| 767 |
|
| /* a UV is above IV_MAX; a negative IV is below every UV */ |
| 768 |
2 |
▶ | if (!a->is_uv && a->iv < 0) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a->is_uv && a->iv < 0) return -1; |
1 |
1 |
2 |
| if (!a->is_uv && a->iv < 0) return -1; |
1 |
0 |
1 |
|
| 769 |
1 |
▶ | if (!b->is_uv && b->iv < 0) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b->is_uv && b->iv < 0) return 1; |
0 |
1 |
1 |
| if (!b->is_uv && b->iv < 0) return 1; |
0 |
0 |
0 |
|
| 770 |
1 |
▶ | return (UV)a->iv < (UV)b->iv ? -1 : (UV)a->iv > (UV)b->iv; |
Branch: 1 branch
| Branch | True | False | Total |
| return (UV)a->iv < (UV)b->iv ? -1 : (UV)a->iv > (UV)b->iv; |
1 |
0 |
1 |
|
| 771 |
|
| } |
| 772 |
335 |
▶ | return a->iv < b->iv ? -1 : a->iv > b->iv; |
Branch: 1 branch
| Branch | True | False | Total |
| return a->iv < b->iv ? -1 : a->iv > b->iv; |
296 |
39 |
335 |
|
| 773 |
|
| } |
| 774 |
21 |
▶ | if (a->nv < b->nv) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->nv < b->nv) return -1; |
3 |
18 |
21 |
|
| 775 |
18 |
▶ | if (a->nv > b->nv) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->nv > b->nv) return 1; |
6 |
12 |
18 |
|
| 776 |
12 |
▶ | if (a->nv == b->nv) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (a->nv == b->nv) return 0; |
11 |
1 |
12 |
|
| 777 |
1 |
| return 2; |
| 778 |
|
| } |
| 779 |
|
| |
| 780 |
|
| #define STENCIL_INTBUF 32 |
| 781 |
|
| |
| 782 |
|
| /* The string form of a defined SV. An integer that has no string yet is |
| 783 |
|
| * written into buf (perl's own spelling for an IV/UV) instead of caching a |
| 784 |
|
| * PV in the caller's SV; anything else is read with SvPV_nomg. */ |
| 785 |
14535 |
| static const char *sv_str(pTHX_ SV *sv, STRLEN *len, int *utf8, char *buf) |
| 786 |
|
| { |
| 787 |
14535 |
| U32 f = SvFLAGS(sv); |
| 788 |
14535 |
▶ | if ((f & SVp_IOK) && !(f & (SVp_POK | SVp_NOK | SVf_ROK))) { |
Branch: 2 branches
| Branch | True | False | Total |
| if ((f & SVp_IOK) && !(f & (SVp_POK | SVp_NOK | SVf_ROK))) { |
85 |
14450 |
14535 |
| if ((f & SVp_IOK) && !(f & (SVp_POK | SVp_NOK | SVf_ROK))) { |
85 |
0 |
85 |
|
| 789 |
85 |
| char *e = buf + STENCIL_INTBUF, *p = e; |
| 790 |
|
| UV u; |
| 791 |
85 |
| int neg = 0; |
| 792 |
85 |
▶ | if (SvIsUV(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIsUV(sv)) { |
0 |
85 |
85 |
|
| 793 |
0 |
| u = SvUVX(sv); |
| 794 |
85 |
▶ | } else if (SvIVX(sv) < 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvIVX(sv) < 0) { |
1 |
84 |
85 |
|
| 795 |
1 |
| neg = 1; |
| 796 |
1 |
| u = (UV)(-(SvIVX(sv) + 1)) + 1; /* safe for IV_MIN */ |
| 797 |
|
| } else { |
| 798 |
84 |
| u = (UV)SvIVX(sv); |
| 799 |
|
| } |
| 800 |
|
| do { |
| 801 |
96 |
| *--p = (char)('0' + u % 10); |
| 802 |
96 |
| u /= 10; |
| 803 |
96 |
▶ | } while (u); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (u); |
11 |
85 |
96 |
|
| 804 |
85 |
▶ | if (neg) |
Branch: 1 branch
| Branch | True | False | Total |
| if (neg) |
1 |
84 |
85 |
|
| 805 |
1 |
| *--p = '-'; |
| 806 |
85 |
| *len = (STRLEN)(e - p); |
| 807 |
85 |
| *utf8 = 0; |
| 808 |
85 |
| return p; |
| 809 |
|
| } |
| 810 |
|
| { |
| 811 |
14450 |
| const char *pv = SvPV_nomg_const(sv, *len); |
| 812 |
14450 |
| *utf8 = SvUTF8(sv) ? 1 : 0; |
| 813 |
14450 |
| return pv; |
| 814 |
|
| } |
| 815 |
|
| } |
| 816 |
|
| |
| 817 |
|
| /* is the program's source (and so every case literal) UTF-8? */ |
| 818 |
|
| #define POOL_UTF8(r) (((r)->prog->flags & STENCIL_PROG_SRC_UTF8) ? 1 : 0) |
| 819 |
|
| |
| 820 |
|
| /* the topic (a defined SV) compared with one case literal: <0, 0, >0, or 2 |
| 821 |
|
| * for a numeric literal against a topic that is not a number or is NaN */ |
| 822 |
11513 |
| static int cval_cmp(pTHX_ stencil_rstate *r, SV *topic, const stencil_cval *v) |
| 823 |
|
| { |
| 824 |
11513 |
▶ | if (v->is_num) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->is_num) { |
6857 |
4656 |
11513 |
|
| 825 |
|
| stencil_num topic_num, lit_num; |
| 826 |
6857 |
▶ | if (!sv_num(aTHX_ topic, &topic_num)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_num(aTHX_ topic, &topic_num)) |
6519 |
338 |
6857 |
|
| 827 |
6519 |
| return 2; |
| 828 |
338 |
| num_from_cval(v, &lit_num); |
| 829 |
338 |
| return num_cmp(&topic_num, &lit_num); |
| 830 |
|
| } else { |
| 831 |
|
| char buf[STENCIL_INTBUF]; |
| 832 |
|
| STRLEN topic_len; |
| 833 |
|
| int topic_utf8; |
| 834 |
4656 |
| const char *topic_pv = sv_str(aTHX_ topic, &topic_len, &topic_utf8, |
| 835 |
|
| buf); |
| 836 |
4656 |
| return stencil_str_cmp(topic_pv, topic_len, topic_utf8, |
| 837 |
4656 |
| r->pool + v->str_off, v->str_len, |
| 838 |
4656 |
| POOL_UTF8(r)); |
| 839 |
|
| } |
| 840 |
|
| } |
| 841 |
|
| |
| 842 |
|
| /* topic within [lo, hi], each bound compared by its own spelling */ |
| 843 |
2258 |
| static int cval_in_range(pTHX_ stencil_rstate *r, SV *topic, |
| 844 |
|
| const stencil_cval *lo, const stencil_cval *hi) |
| 845 |
|
| { |
| 846 |
2258 |
| int c = cval_cmp(aTHX_ r, topic, lo); |
| 847 |
2258 |
▶ | if (c == 2 || c < 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (c == 2 || c < 0) |
54 |
2204 |
2258 |
| if (c == 2 || c < 0) |
8 |
46 |
54 |
|
| 848 |
2212 |
| return 0; |
| 849 |
46 |
| c = cval_cmp(aTHX_ r, topic, hi); |
| 850 |
46 |
▶ | return c != 2 && c <= 0; |
Branch: 2 branches
| Branch | True | False | Total |
| return c != 2 && c <= 0; |
46 |
0 |
46 |
| return c != 2 && c <= 0; |
36 |
10 |
46 |
|
| 851 |
|
| } |
| 852 |
|
| |
| 853 |
|
| /* Match the topic with a compiled program. Out of line: the matcher's |
| 854 |
|
| * state sets are a few KB of stack. */ |
| 855 |
4252 |
| static STENCIL_NOINLINE int rx_match_sv(pTHX_ const stencil_rx *rx, SV *sv) |
| 856 |
|
| { |
| 857 |
|
| char buf[STENCIL_INTBUF]; |
| 858 |
|
| STRLEN len; |
| 859 |
|
| int utf8; |
| 860 |
4252 |
| const char *s = sv_str(aTHX_ sv, &len, &utf8, buf); |
| 861 |
4252 |
| return stencil_rx_match(rx, s, len, utf8); |
| 862 |
|
| } |
| 863 |
|
| |
| 864 |
|
| /* The per-engine cache of compiled data patterns, keyed by the exact |
| 865 |
|
| * pattern bytes, flags and encoding. Four entries replaced round-robin in |
| 866 |
|
| * one block allocated on the engine's first data pattern, so a steady |
| 867 |
|
| * state allocates nothing and a template without =~ never pays. Lookup and |
| 868 |
|
| * match run with no perl call between them, so a render nested inside a |
| 869 |
|
| * predicate cannot replace an entry from under its user. */ |
| 870 |
|
| #define STENCIL_RX_CACHE_N 4 |
| 871 |
|
| |
| 872 |
|
| typedef struct stencil_rx_cache_ent { |
| 873 |
|
| uint32_t len; |
| 874 |
|
| uint16_t flags; |
| 875 |
|
| uint8_t utf8, used; |
| 876 |
|
| char pat[STENCIL_RX_MAX_PATTERN]; |
| 877 |
|
| union { |
| 878 |
|
| stencil_rx rx; |
| 879 |
|
| uint32_t w[(STENCIL_RX_PROG_MAX + 3) / 4]; |
| 880 |
|
| } prog; |
| 881 |
|
| } stencil_rx_cache_ent; |
| 882 |
|
| |
| 883 |
|
| struct stencil_rx_cache { |
| 884 |
|
| uint32_t next; |
| 885 |
|
| stencil_rx_cache_ent e[STENCIL_RX_CACHE_N]; |
| 886 |
|
| }; |
| 887 |
|
| |
| 888 |
1323 |
| static const stencil_rx *rx_cache_find(stencil_rx_cache *cache, |
| 889 |
|
| const char *pat, STRLEN len, |
| 890 |
|
| unsigned flags, int utf8) |
| 891 |
|
| { |
| 892 |
|
| int i; |
| 893 |
1648 |
▶ | for (i = 0; i < STENCIL_RX_CACHE_N; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < STENCIL_RX_CACHE_N; i++) { |
1603 |
45 |
1648 |
|
| 894 |
1603 |
| const stencil_rx_cache_ent *e = &cache->e[i]; |
| 895 |
1603 |
▶ | if (e->used && e->len == len && e->flags == flags && e->utf8 == utf8 |
Branch: 4 branches
| Branch | True | False | Total |
| if (e->used && e->len == len && e->flags == flags && e->utf8 == utf8 |
1579 |
24 |
1603 |
| if (e->used && e->len == len && e->flags == flags && e->utf8 == utf8 |
1487 |
92 |
1579 |
| if (e->used && e->len == len && e->flags == flags && e->utf8 == utf8 |
1481 |
6 |
1487 |
| if (e->used && e->len == len && e->flags == flags && e->utf8 == utf8 |
1478 |
3 |
1481 |
|
| 896 |
1478 |
▶ | && memcmp(e->pat, pat, len) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| && memcmp(e->pat, pat, len) == 0) |
1278 |
200 |
1478 |
|
| 897 |
1278 |
| return &e->prog.rx; |
| 898 |
|
| } |
| 899 |
45 |
| return NULL; |
| 900 |
|
| } |
| 901 |
|
| |
| 902 |
66 |
| static void rx_cache_store(stencil_rx_cache *cache, const char *pat, |
| 903 |
|
| STRLEN len, unsigned flags, int utf8, |
| 904 |
|
| const stencil_rx *rx) |
| 905 |
|
| { |
| 906 |
66 |
| stencil_rx_cache_ent *e = &cache->e[cache->next]; |
| 907 |
66 |
| cache->next = (cache->next + 1) % STENCIL_RX_CACHE_N; |
| 908 |
66 |
| e->used = 1; |
| 909 |
66 |
| e->len = (uint32_t)len; |
| 910 |
66 |
| e->flags = (uint16_t)flags; |
| 911 |
66 |
| e->utf8 = (uint8_t)utf8; |
| 912 |
66 |
| memcpy(e->pat, pat, len); |
| 913 |
66 |
| memcpy(&e->prog, rx, rx->size); |
| 914 |
66 |
| } |
| 915 |
|
| |
| 916 |
|
| /* {% case =~ path %}: a pattern from the data, compiled per evaluation by |
| 917 |
|
| * the same engine and under the same caps as a template pattern, into a |
| 918 |
|
| * work area on this frame's stack (cached per engine, see above). Out of |
| 919 |
|
| * line so the ~19 KB work area is never part of the recursive VM's frame. |
| 920 |
|
| * Returns 1/0, or -1 with the error set. */ |
| 921 |
1475 |
| static STENCIL_NOINLINE int case_data_regex(pTHX_ stencil_rstate *r, |
| 922 |
|
| uint32_t pid, SV *pat, |
| 923 |
|
| SV *topic) |
| 924 |
|
| { |
| 925 |
1475 |
| const stencil_cpath *path = &r->paths[pid]; |
| 926 |
|
| stencil_rx_work w; |
| 927 |
|
| stencil_rx_err err; |
| 928 |
|
| const stencil_rx *rx; |
| 929 |
|
| const char *pat_pv; |
| 930 |
|
| STRLEN pat_len; |
| 931 |
|
| int pat_utf8; |
| 932 |
1475 |
| unsigned flags = 0; |
| 933 |
|
| |
| 934 |
1475 |
▶ | if (SvROK(pat) && SvRXOK(pat)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(pat) && SvRXOK(pat)) { |
1441 |
34 |
1475 |
| if (SvROK(pat) && SvRXOK(pat)) { |
1440 |
1 |
1441 |
|
| 935 |
1440 |
| REGEXP *re = SvRX(pat); |
| 936 |
1440 |
| U32 ef = RX_EXTFLAGS(re); |
| 937 |
|
| #ifdef RXf_PMf_EXTENDED_MORE |
| 938 |
1440 |
▶ | if (ef & RXf_PMf_EXTENDED_MORE) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ef & RXf_PMf_EXTENDED_MORE) |
1 |
1439 |
1440 |
|
| 939 |
1 |
| return rerror(r, "case =~ %.*s: the /xx flag is not supported " |
| 940 |
1 |
| "(i, m, s and x are)", (int)path->full_len, |
| 941 |
1 |
| r->pool + path->full_off) - 1; |
| 942 |
|
| #endif |
| 943 |
1439 |
▶ | if (ef & RXf_PMf_FOLD) flags |= STENCIL_RX_I; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ef & RXf_PMf_FOLD) flags |= STENCIL_RX_I; |
4 |
1435 |
1439 |
|
| 944 |
1439 |
▶ | if (ef & RXf_PMf_MULTILINE) flags |= STENCIL_RX_M; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ef & RXf_PMf_MULTILINE) flags |= STENCIL_RX_M; |
1 |
1438 |
1439 |
|
| 945 |
1439 |
▶ | if (ef & RXf_PMf_SINGLELINE) flags |= STENCIL_RX_S; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ef & RXf_PMf_SINGLELINE) flags |= STENCIL_RX_S; |
1 |
1438 |
1439 |
|
| 946 |
1439 |
▶ | if (ef & RXf_PMf_EXTENDED) flags |= STENCIL_RX_X; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ef & RXf_PMf_EXTENDED) flags |= STENCIL_RX_X; |
1 |
1438 |
1439 |
|
| 947 |
|
| /* /a /u /l /d, /n and /p change nothing here: classes are always |
| 948 |
|
| * ASCII and captures are never seen */ |
| 949 |
1439 |
| pat_pv = RX_PRECOMP(re); |
| 950 |
1439 |
| pat_len = RX_PRELEN(re); |
| 951 |
1439 |
| pat_utf8 = RX_UTF8(re) ? 1 : 0; |
| 952 |
35 |
▶ | } else if (SvROK(pat)) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (SvROK(pat)) { |
1 |
34 |
35 |
|
| 953 |
1 |
| return rerror(r, "case =~ %.*s: not a pattern (a qr// or a string)", |
| 954 |
1 |
| (int)path->full_len, r->pool + path->full_off) - 1; |
| 955 |
|
| } else { |
| 956 |
34 |
| pat_pv = SvPV_nomg_const(pat, pat_len); |
| 957 |
34 |
| pat_utf8 = SvUTF8(pat) ? 1 : 0; |
| 958 |
|
| } |
| 959 |
1473 |
▶ | if (r->rx_cache && *r->rx_cache && pat_len <= STENCIL_RX_MAX_PATTERN) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (r->rx_cache && *r->rx_cache && pat_len <= STENCIL_RX_MAX_PATTERN) { |
1353 |
120 |
1473 |
| if (r->rx_cache && *r->rx_cache && pat_len <= STENCIL_RX_MAX_PATTERN) { |
1324 |
29 |
1353 |
| if (r->rx_cache && *r->rx_cache && pat_len <= STENCIL_RX_MAX_PATTERN) { |
1323 |
1 |
1324 |
|
| 960 |
1323 |
| rx = rx_cache_find(*r->rx_cache, pat_pv, pat_len, flags, pat_utf8); |
| 961 |
1323 |
▶ | if (rx) |
Branch: 1 branch
| Branch | True | False | Total |
| if (rx) |
1278 |
45 |
1323 |
|
| 962 |
1278 |
| return rx_match_sv(aTHX_ rx, topic); |
| 963 |
|
| } |
| 964 |
195 |
| rx = stencil_rx_compile(pat_pv, pat_len, pat_utf8, flags, &w, &err); |
| 965 |
195 |
▶ | if (!rx) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!rx) |
29 |
166 |
195 |
|
| 966 |
29 |
| return rerror(r, "case =~ %.*s: regex: %s (at byte %lu)", |
| 967 |
29 |
| (int)path->full_len, r->pool + path->full_off, |
| 968 |
29 |
| err.msg, (unsigned long)err.at) - 1; |
| 969 |
166 |
▶ | if (r->rx_cache) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->rx_cache) { |
66 |
100 |
166 |
|
| 970 |
66 |
▶ | if (!*r->rx_cache) /* once per engine, on its first pattern */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*r->rx_cache) /* once per engine, on its first pattern */ |
28 |
38 |
66 |
|
| 971 |
28 |
| *r->rx_cache = (stencil_rx_cache *)calloc(1, sizeof **r->rx_cache); |
| 972 |
66 |
▶ | if (*r->rx_cache) /* no memory: just don't cache */ |
Branch: 1 branch
| Branch | True | False | Total |
| if (*r->rx_cache) /* no memory: just don't cache */ |
66 |
0 |
66 |
|
| 973 |
66 |
| rx_cache_store(*r->rx_cache, pat_pv, pat_len, flags, pat_utf8, |
| 974 |
|
| rx); |
| 975 |
|
| } |
| 976 |
166 |
| return rx_match_sv(aTHX_ rx, topic); |
| 977 |
|
| } |
| 978 |
|
| |
| 979 |
|
| /* {% case \&name %}: the registered filter `name`, called with the topic; |
| 980 |
|
| * a perl-true return matches. Returns 1/0, or -1 with the error set. */ |
| 981 |
655 |
| static int case_predicate_call(pTHX_ stencil_rstate *r, uint32_t fidx, |
| 982 |
|
| SV *topic) |
| 983 |
|
| { |
| 984 |
655 |
| const stencil_cfilt *f = &r->filts[fidx]; |
| 985 |
1310 |
| SV **cvp = r->filters |
| 986 |
654 |
| ? hv_fetch(r->filters, r->pool + f->name_off, (I32)f->name_len, 0) |
| 987 |
655 |
▶ | : NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| : NULL; |
654 |
1 |
655 |
|
| 988 |
655 |
| int died, truth = 0; |
| 989 |
655 |
▶ | 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) |
654 |
1 |
655 |
| if (!cvp || !SvROK(*cvp) || SvTYPE(SvRV(*cvp)) != SVt_PVCV) |
654 |
0 |
654 |
| if (!cvp || !SvROK(*cvp) || SvTYPE(SvRV(*cvp)) != SVt_PVCV) |
0 |
654 |
654 |
|
| 990 |
1 |
| return rerror(r, "unknown filter '%.*s' in case predicate", |
| 991 |
1 |
| (int)f->name_len, r->pool + f->name_off) - 1; |
| 992 |
|
| { |
| 993 |
654 |
| dSP; |
| 994 |
654 |
| ENTER; |
| 995 |
654 |
| SAVETMPS; |
| 996 |
654 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
654 |
654 |
|
| 997 |
654 |
▶ | if (SvGMAGICAL(topic)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvGMAGICAL(topic)) { |
0 |
654 |
654 |
|
| 998 |
|
| /* a tied topic was FETCHed once, when the switch pushed it; |
| 999 |
|
| * hand the predicate that value, not the tie, or its $_[0] |
| 1000 |
|
| * would FETCH again */ |
| 1001 |
0 |
| SV *copy = sv_newmortal(); |
| 1002 |
0 |
| sv_setsv_flags(copy, topic, 0); |
| 1003 |
0 |
▶ | XPUSHs(copy); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(copy); |
0 |
0 |
0 |
|
| 1004 |
|
| } else { |
| 1005 |
654 |
▶ | XPUSHs(topic); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(topic); |
0 |
654 |
654 |
|
| 1006 |
|
| } |
| 1007 |
654 |
| PUTBACK; |
| 1008 |
654 |
| (void)call_sv(*cvp, G_SCALAR | G_EVAL); |
| 1009 |
654 |
| SPAGAIN; |
| 1010 |
654 |
▶ | died = SvTRUE(ERRSV); |
Branch: 1 branch
| Branch | True | False | Total |
| died = SvTRUE(ERRSV); |
654 |
0 |
654 |
|
| 1011 |
|
| { |
| 1012 |
654 |
| SV *ret = POPs; |
| 1013 |
654 |
▶ | if (!died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!died) |
651 |
3 |
654 |
|
| 1014 |
651 |
| truth = SvTRUE(ret) ? 1 : 0; |
| 1015 |
|
| } |
| 1016 |
654 |
| PUTBACK; |
| 1017 |
654 |
▶ | FREETMPS; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; |
654 |
0 |
654 |
|
| 1018 |
654 |
| LEAVE; |
| 1019 |
|
| } |
| 1020 |
654 |
▶ | if (died) |
Branch: 1 branch
| Branch | True | False | Total |
| if (died) |
3 |
651 |
654 |
|
| 1021 |
3 |
| return rerror(r, "case predicate '%.*s' died: %s", |
| 1022 |
3 |
| (int)f->name_len, r->pool + f->name_off, |
| 1023 |
6 |
▶ | SvPV_nolen(ERRSV)) - 1; |
Branch: 1 branch
| Branch | True | False | Total |
| SvPV_nolen(ERRSV)) - 1; |
3 |
0 |
3 |
|
| 1024 |
651 |
| return truth; |
| 1025 |
|
| } |
| 1026 |
|
| |
| 1027 |
|
| /* `ref(T)` / `reftype(T)`: the reference's type name against T */ |
| 1028 |
118 |
| static int reftype_is(pTHX_ stencil_rstate *r, SV *topic, |
| 1029 |
|
| const stencil_ccase *cs, int blessed_name) |
| 1030 |
|
| { |
| 1031 |
118 |
| SV *rv = SvRV(topic); |
| 1032 |
|
| const char *name; |
| 1033 |
118 |
| int name_utf8 = 0; |
| 1034 |
118 |
▶ | if (!cs->b) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cs->b) |
6 |
112 |
118 |
|
| 1035 |
6 |
| return 1; /* bare ref / reftype: any reference */ |
| 1036 |
112 |
| name = sv_reftype(rv, blessed_name); |
| 1037 |
|
| #ifdef HvNAMEUTF8 |
| 1038 |
112 |
▶ | if (blessed_name && SvOBJECT(rv) && SvSTASH(rv)) |
Branch: 3 branches
| Branch | True | False | Total |
| if (blessed_name && SvOBJECT(rv) && SvSTASH(rv)) |
105 |
7 |
112 |
| if (blessed_name && SvOBJECT(rv) && SvSTASH(rv)) |
26 |
79 |
105 |
| if (blessed_name && SvOBJECT(rv) && SvSTASH(rv)) |
26 |
0 |
26 |
|
| 1039 |
26 |
▶ | name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
Branch: 8 branches
| Branch | True | False | Total |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
26 |
0 |
26 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
26 |
0 |
26 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
0 |
26 |
26 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
0 |
0 |
0 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
26 |
0 |
26 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
0 |
26 |
26 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
0 |
26 |
26 |
| name_utf8 = HvNAMEUTF8(SvSTASH(rv)) ? 1 : 0; |
0 |
26 |
26 |
|
| 1040 |
|
| #endif |
| 1041 |
112 |
| return stencil_str_cmp(name, strlen(name), name_utf8, |
| 1042 |
112 |
| r->pool + cs->str_off, cs->str_len, |
| 1043 |
112 |
| POOL_UTF8(r)) == 0; |
| 1044 |
|
| } |
| 1045 |
|
| |
| 1046 |
|
| /* `isa(Class)`: a blessed object derived from Class (an @ISA walk, no |
| 1047 |
|
| * method call); never matches a class-name string or a plain reference */ |
| 1048 |
177 |
| static int isa_is(pTHX_ stencil_rstate *r, SV *topic, const stencil_ccase *cs) |
| 1049 |
|
| { |
| 1050 |
177 |
| const char *class_pv = r->pool + cs->str_off; |
| 1051 |
177 |
▶ | if (!sv_isobject(topic)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sv_isobject(topic)) |
95 |
82 |
177 |
|
| 1052 |
95 |
| return 0; |
| 1053 |
|
| #if PERL_VERSION_GE(5,16,0) |
| 1054 |
82 |
▶ | return sv_derived_from_pvn(topic, class_pv, cs->str_len, |
Branch: 1 branch
| Branch | True | False | Total |
| return sv_derived_from_pvn(topic, class_pv, cs->str_len, |
0 |
82 |
82 |
|
| 1055 |
82 |
| POOL_UTF8(r) ? SVf_UTF8 : 0) ? 1 : 0; |
| 1056 |
|
| #else |
| 1057 |
|
| { |
| 1058 |
|
| char buf[256]; |
| 1059 |
|
| if (cs->str_len < sizeof buf) { |
| 1060 |
|
| memcpy(buf, class_pv, cs->str_len); |
| 1061 |
|
| buf[cs->str_len] = '\0'; |
| 1062 |
|
| return sv_derived_from(topic, buf) ? 1 : 0; |
| 1063 |
|
| } |
| 1064 |
|
| return sv_derived_from(topic, |
| 1065 |
|
| SvPVX(sv_2mortal(newSVpvn(class_pv, cs->str_len)))) |
| 1066 |
|
| ? 1 : 0; |
| 1067 |
|
| } |
| 1068 |
|
| #endif |
| 1069 |
|
| } |
| 1070 |
|
| |
| 1071 |
|
| /* A run of string arms compiled into a dispatch table: binary-search the |
| 1072 |
|
| * topic's string among the sorted keys. Returns the matching arm's body |
| 1073 |
|
| * offset, or 0 for no match (the topic push precedes every arm, so no |
| 1074 |
|
| * arm body is at offset 0). An undef topic matches no string arm. */ |
| 1075 |
3629 |
| static uint32_t case_table_lookup(pTHX_ stencil_rstate *r, uint32_t id, |
| 1076 |
|
| SV *topic) |
| 1077 |
|
| { |
| 1078 |
3629 |
| const stencil_ctab *tab = &stencil_prog_ctabs(r->prog)[id]; |
| 1079 |
3629 |
| const stencil_ctent *keys = stencil_prog_ctents(r->prog) + tab->first; |
| 1080 |
3629 |
| int keys_utf8 = POOL_UTF8(r); |
| 1081 |
|
| char buf[STENCIL_INTBUF]; |
| 1082 |
|
| STRLEN topic_len; |
| 1083 |
|
| int topic_utf8; |
| 1084 |
|
| const char *topic_pv; |
| 1085 |
3629 |
| uint32_t lo = 0, hi = tab->n; |
| 1086 |
3629 |
▶ | if (!topic || !SvOK(topic)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!topic || !SvOK(topic)) |
3629 |
0 |
3629 |
| if (!topic || !SvOK(topic)) |
0 |
3629 |
3629 |
|
| 1087 |
0 |
| return 0; |
| 1088 |
3629 |
| topic_pv = sv_str(aTHX_ topic, &topic_len, &topic_utf8, buf); |
| 1089 |
12592 |
▶ | while (lo < hi) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (lo < hi) { |
11298 |
1294 |
12592 |
|
| 1090 |
11298 |
| uint32_t mid = lo + (hi - lo) / 2; |
| 1091 |
11298 |
| int c = stencil_str_cmp(topic_pv, topic_len, topic_utf8, |
| 1092 |
11298 |
| r->pool + keys[mid].str_off, |
| 1093 |
11298 |
| keys[mid].str_len, keys_utf8); |
| 1094 |
11298 |
▶ | if (c == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == 0) |
2335 |
8963 |
11298 |
|
| 1095 |
2335 |
| return keys[mid].target; |
| 1096 |
8963 |
▶ | if (c < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (c < 0) |
5685 |
3278 |
8963 |
|
| 1097 |
5685 |
| hi = mid; |
| 1098 |
|
| else |
| 1099 |
3278 |
| lo = mid + 1; |
| 1100 |
|
| } |
| 1101 |
1294 |
| return 0; |
| 1102 |
|
| } |
| 1103 |
|
| |
| 1104 |
|
| /* == path / eq path / =~ path: the topic against a value from the data. |
| 1105 |
|
| * A missing or undef operand is a miss. 1/0, or -1 with the error set. */ |
| 1106 |
3504 |
| static int case_path_match(pTHX_ stencil_rstate *r, const stencil_ccase *cs, |
| 1107 |
|
| SV *topic) |
| 1108 |
|
| { |
| 1109 |
3504 |
| int owned = 0, matched = 0; |
| 1110 |
3504 |
| SV *operand = resolve_path(aTHX_ r, cs->a, &owned); |
| 1111 |
3504 |
▶ | if (STENCIL_UNLIKELY(r->failed)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (STENCIL_UNLIKELY(r->failed)) { |
1 |
3503 |
3504 |
|
| 1112 |
1 |
▶ | if (owned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (owned) |
0 |
1 |
1 |
|
| 1113 |
0 |
| SvREFCNT_dec(operand); |
| 1114 |
1 |
| return -1; |
| 1115 |
|
| } |
| 1116 |
3503 |
▶ | if (operand && SvOK(operand)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (operand && SvOK(operand)) { |
3493 |
10 |
3503 |
| if (operand && SvOK(operand)) { |
3489 |
4 |
3493 |
|
| 1117 |
3489 |
▶ | if (cs->kind == STENCIL_CASE_PATH_NUM) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cs->kind == STENCIL_CASE_PATH_NUM) { |
1015 |
2474 |
3489 |
|
| 1118 |
|
| stencil_num topic_num, operand_num; |
| 1119 |
2030 |
| matched = sv_num(aTHX_ topic, &topic_num) |
| 1120 |
22 |
▶ | && sv_num(aTHX_ operand, &operand_num) |
Branch: 1 branch
| Branch | True | False | Total |
| && sv_num(aTHX_ operand, &operand_num) |
20 |
2 |
22 |
|
| 1121 |
1037 |
▶ | && num_cmp(&topic_num, &operand_num) == 0; |
Branch: 2 branches
| Branch | True | False | Total |
| && num_cmp(&topic_num, &operand_num) == 0; |
22 |
993 |
1015 |
| && num_cmp(&topic_num, &operand_num) == 0; |
13 |
7 |
20 |
|
| 1122 |
2474 |
▶ | } else if (cs->kind == STENCIL_CASE_PATH_STR) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (cs->kind == STENCIL_CASE_PATH_STR) { |
999 |
1475 |
2474 |
|
| 1123 |
|
| char tbuf[STENCIL_INTBUF], obuf[STENCIL_INTBUF]; |
| 1124 |
|
| STRLEN tlen, olen; |
| 1125 |
|
| int tutf8, outf8; |
| 1126 |
999 |
| const char *tpv = sv_str(aTHX_ topic, &tlen, &tutf8, tbuf); |
| 1127 |
999 |
| const char *opv = sv_str(aTHX_ operand, &olen, &outf8, obuf); |
| 1128 |
999 |
| matched = stencil_str_cmp(tpv, tlen, tutf8, opv, olen, outf8) == 0; |
| 1129 |
|
| } else { |
| 1130 |
1475 |
| matched = case_data_regex(aTHX_ r, cs->a, operand, topic); |
| 1131 |
|
| } |
| 1132 |
|
| } |
| 1133 |
3503 |
▶ | if (owned) |
Branch: 1 branch
| Branch | True | False | Total |
| if (owned) |
6 |
3497 |
3503 |
|
| 1134 |
6 |
| SvREFCNT_dec(operand); |
| 1135 |
3503 |
| return matched; |
| 1136 |
|
| } |
| 1137 |
|
| |
| 1138 |
|
| /* Does case `id` match the topic? 1 or 0; -1 on a render error (set). */ |
| 1139 |
17785 |
| static int case_match(pTHX_ stencil_rstate *r, uint32_t id, SV *topic) |
| 1140 |
|
| { |
| 1141 |
17785 |
| const stencil_ccase *cs = &stencil_prog_cases(r->prog)[id]; |
| 1142 |
|
| const stencil_cval *vals; |
| 1143 |
|
| |
| 1144 |
17785 |
▶ | if (!topic || !SvOK(topic)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!topic || !SvOK(topic)) |
17770 |
15 |
17785 |
| if (!topic || !SvOK(topic)) |
557 |
17213 |
17770 |
|
| 1145 |
572 |
| return cs->kind == STENCIL_CASE_UNDEF; |
| 1146 |
|
| |
| 1147 |
17213 |
| vals = stencil_prog_cvals(r->prog) + cs->a; |
| 1148 |
17213 |
| switch (cs->kind) { |
| 1149 |
16 |
| case STENCIL_CASE_UNDEF: |
| 1150 |
16 |
| return 0; |
| 1151 |
4564 |
| case STENCIL_CASE_VALUE: |
| 1152 |
4564 |
| return cval_cmp(aTHX_ r, topic, &vals[0]) == 0; |
| 1153 |
2462 |
| case STENCIL_CASE_LIST: { |
| 1154 |
|
| uint32_t i; |
| 1155 |
6757 |
▶ | for (i = 0; i < cs->b; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < cs->b; i++) |
4645 |
2112 |
6757 |
|
| 1156 |
4645 |
▶ | if (cval_cmp(aTHX_ r, topic, &vals[i]) == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cval_cmp(aTHX_ r, topic, &vals[i]) == 0) |
350 |
4295 |
4645 |
|
| 1157 |
350 |
| return 1; |
| 1158 |
2112 |
| return 0; |
| 1159 |
|
| } |
| 1160 |
2258 |
| case STENCIL_CASE_RANGE: |
| 1161 |
2258 |
| return cval_in_range(aTHX_ r, topic, &vals[0], &vals[1]); |
| 1162 |
2808 |
| case STENCIL_CASE_RX: |
| 1163 |
2808 |
| return rx_match_sv(aTHX_ stencil_prog_rx(r->prog, cs->a), topic); |
| 1164 |
3504 |
| case STENCIL_CASE_PATH_NUM: |
| 1165 |
|
| case STENCIL_CASE_PATH_STR: |
| 1166 |
|
| case STENCIL_CASE_PATH_RX: |
| 1167 |
3504 |
| return case_path_match(aTHX_ r, cs, topic); |
| 1168 |
655 |
| case STENCIL_CASE_PRED: |
| 1169 |
655 |
| return case_predicate_call(aTHX_ r, cs->a, topic); |
| 1170 |
758 |
| case STENCIL_CASE_REF: |
| 1171 |
758 |
▶ | return SvROK(topic) && reftype_is(aTHX_ r, topic, cs, 1); |
Branch: 2 branches
| Branch | True | False | Total |
| return SvROK(topic) && reftype_is(aTHX_ r, topic, cs, 1); |
109 |
649 |
758 |
| return SvROK(topic) && reftype_is(aTHX_ r, topic, cs, 1); |
68 |
41 |
109 |
|
| 1172 |
11 |
| case STENCIL_CASE_REFTYPE: |
| 1173 |
11 |
▶ | return SvROK(topic) && reftype_is(aTHX_ r, topic, cs, 0); |
Branch: 2 branches
| Branch | True | False | Total |
| return SvROK(topic) && reftype_is(aTHX_ r, topic, cs, 0); |
9 |
2 |
11 |
| return SvROK(topic) && reftype_is(aTHX_ r, topic, cs, 0); |
8 |
1 |
9 |
|
| 1174 |
177 |
| case STENCIL_CASE_ISA: |
| 1175 |
177 |
| return isa_is(aTHX_ r, topic, cs); |
| 1176 |
|
| } |
| 1177 |
0 |
| return rerror(r, "corrupt case table") - 1; |
| 1178 |
|
| } |
| 1179 |
|
| |
| 1180 |
|
| /* ---- comparison macros for the VM body -------------------------------- */ |
| 1181 |
|
| |
| 1182 |
|
| #define VM_CMP_NUM(OPR) \ |
| 1183 |
|
| { \ |
| 1184 |
|
| stencil_val vb = r->stack[--r->sp]; \ |
| 1185 |
|
| stencil_val va = r->stack[--r->sp]; \ |
| 1186 |
|
| NV nva = (va.sv && SvOK(va.sv)) ? SvNV(va.sv) : 0.0; \ |
| 1187 |
|
| NV nvb = (vb.sv && SvOK(vb.sv)) ? SvNV(vb.sv) : 0.0; \ |
| 1188 |
|
| int res = nva OPR nvb; \ |
| 1189 |
|
| val_release(aTHX_ &va); \ |
| 1190 |
|
| val_release(aTHX_ &vb); \ |
| 1191 |
|
| r->stack[r->sp].sv = res ? &PL_sv_yes : &PL_sv_no; \ |
| 1192 |
|
| r->stack[r->sp].owned = 0; \ |
| 1193 |
|
| r->stack[r->sp].vflags = 0; \ |
| 1194 |
|
| r->sp++; \ |
| 1195 |
|
| } \ |
| 1196 |
|
| VM_NEXT; |
| 1197 |
|
| |
| 1198 |
|
| #define VM_CMP_STR(EXPR) \ |
| 1199 |
|
| { \ |
| 1200 |
|
| stencil_val vb = r->stack[--r->sp]; \ |
| 1201 |
|
| stencil_val va = r->stack[--r->sp]; \ |
| 1202 |
|
| SV *a = (va.sv && SvOK(va.sv)) ? va.sv : &PL_sv_no; \ |
| 1203 |
|
| SV *b = (vb.sv && SvOK(vb.sv)) ? vb.sv : &PL_sv_no; \ |
| 1204 |
|
| int res = (EXPR) ? 1 : 0; \ |
| 1205 |
|
| val_release(aTHX_ &va); \ |
| 1206 |
|
| val_release(aTHX_ &vb); \ |
| 1207 |
|
| r->stack[r->sp].sv = res ? &PL_sv_yes : &PL_sv_no; \ |
| 1208 |
|
| r->stack[r->sp].owned = 0; \ |
| 1209 |
|
| r->stack[r->sp].vflags = 0; \ |
| 1210 |
|
| r->sp++; \ |
| 1211 |
|
| } \ |
| 1212 |
|
| VM_NEXT; |
| 1213 |
|
| |
| 1214 |
|
| /* ---- VM instantiations ------------------------------------------------ */ |
| 1215 |
|
| |
| 1216 |
|
| #ifdef STENCIL_HAVE_COMPUTED_GOTO |
| 1217 |
|
| #define STENCIL_VM_GOTO 1 |
| 1218 |
|
| #define STENCIL_VM_NAME stencil_vm_goto |
| 1219 |
|
| #include "stencil_vm.inc" |
| 1220 |
|
| #undef STENCIL_VM_GOTO |
| 1221 |
|
| #undef STENCIL_VM_NAME |
| 1222 |
|
| #endif |
| 1223 |
|
| |
| 1224 |
|
| #define STENCIL_VM_GOTO 0 |
| 1225 |
|
| #define STENCIL_VM_NAME stencil_vm_switch |
| 1226 |
|
| #include "stencil_vm.inc" |
| 1227 |
|
| #undef STENCIL_VM_GOTO |
| 1228 |
|
| #undef STENCIL_VM_NAME |
| 1229 |
|
| |
| 1230 |
|
| /* ---- public entries ---------------------------------------------------- */ |
| 1231 |
|
| |
| 1232 |
104849 |
| SV *stencil_render_core(pTHX_ |
| 1233 |
|
| stencil_program *page_prog, stencil_cache_ent **page_incs, |
| 1234 |
|
| const char *page_name, |
| 1235 |
|
| stencil_program *wrap_prog, stencil_cache_ent **wrap_incs, |
| 1236 |
|
| const char *wrap_name, |
| 1237 |
|
| uint32_t eff_stack, uint32_t eff_frames, uint32_t eff_binds, |
| 1238 |
|
| HV *data, HV *filters, stencil_rx_cache **rx_cache, uint32_t flags, |
| 1239 |
|
| SV **err) |
| 1240 |
|
| { |
| 1241 |
|
| stencil_rstate r; |
| 1242 |
|
| stencil_val fix_stack[16]; |
| 1243 |
|
| stencil_rframe fix_frames[8]; |
| 1244 |
|
| stencil_rbind fix_binds[16]; |
| 1245 |
104849 |
| int heap_stack = 0, heap_frames = 0, heap_binds = 0; |
| 1246 |
|
| int ok; |
| 1247 |
104849 |
▶ | 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; |
139 |
104710 |
104849 |
|
| 1248 |
|
| |
| 1249 |
104849 |
| memset(&r, 0, sizeof r); |
| 1250 |
104849 |
▶ | if (wrap_prog) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (wrap_prog) { |
139 |
104710 |
104849 |
|
| 1251 |
139 |
▶ | set_unit(&r, wrap_prog, wrap_incs, |
Branch: 1 branch
| Branch | True | False | Total |
| set_unit(&r, wrap_prog, wrap_incs, |
139 |
0 |
139 |
|
| 1252 |
|
| wrap_name ? wrap_name : "<wrapper>"); |
| 1253 |
139 |
| r.content_prog = page_prog; |
| 1254 |
139 |
| r.content_incs = page_incs; |
| 1255 |
139 |
▶ | r.content_name = page_name ? page_name : "<string>"; |
Branch: 1 branch
| Branch | True | False | Total |
| r.content_name = page_name ? page_name : "<string>"; |
139 |
0 |
139 |
|
| 1256 |
|
| } else { |
| 1257 |
104710 |
▶ | set_unit(&r, page_prog, page_incs, |
Branch: 1 branch
| Branch | True | False | Total |
| set_unit(&r, page_prog, page_incs, |
104710 |
0 |
104710 |
|
| 1258 |
|
| page_name ? page_name : "<string>"); |
| 1259 |
|
| } |
| 1260 |
104849 |
| r.data = data; |
| 1261 |
104849 |
| r.filters = filters; |
| 1262 |
104849 |
| r.rx_cache = rx_cache; |
| 1263 |
104849 |
| r.flags = flags; |
| 1264 |
|
| |
| 1265 |
104849 |
▶ | if (eff_stack <= 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eff_stack <= 16) { |
104849 |
0 |
104849 |
|
| 1266 |
104849 |
| r.stack = fix_stack; |
| 1267 |
|
| } else { |
| 1268 |
0 |
| Newx(r.stack, eff_stack, stencil_val); |
| 1269 |
0 |
| heap_stack = 1; |
| 1270 |
|
| } |
| 1271 |
104849 |
▶ | if (eff_frames <= 8) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eff_frames <= 8) { |
104849 |
0 |
104849 |
|
| 1272 |
104849 |
| r.frames = fix_frames; |
| 1273 |
|
| } else { |
| 1274 |
0 |
| Newx(r.frames, eff_frames, stencil_rframe); |
| 1275 |
0 |
| heap_frames = 1; |
| 1276 |
|
| } |
| 1277 |
104849 |
▶ | if (eff_binds <= 16) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (eff_binds <= 16) { |
104849 |
0 |
104849 |
|
| 1278 |
104849 |
| r.binds = fix_binds; |
| 1279 |
|
| } else { |
| 1280 |
0 |
| Newx(r.binds, eff_binds, stencil_rbind); |
| 1281 |
0 |
| heap_binds = 1; |
| 1282 |
|
| } |
| 1283 |
|
| |
| 1284 |
|
| /* profiled_size is a capacity that was once enough (see below), so |
| 1285 |
|
| * it is asked for as-is */ |
| 1286 |
104849 |
| stencil_buf_init(aTHX_ &r.buf, |
| 1287 |
104849 |
▶ | entry_prog->profiled_size |
Branch: 1 branch
| Branch | True | False | Total |
| entry_prog->profiled_size |
100579 |
4270 |
104849 |
|
| 1288 |
|
| ? entry_prog->profiled_size : 64); |
| 1289 |
104849 |
▶ | 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)) |
139 |
104710 |
104849 |
|
| 1290 |
104849 |
▶ | & STENCIL_PROG_SRC_UTF8) |
Branch: 1 branch
| Branch | True | False | Total |
| & STENCIL_PROG_SRC_UTF8) |
1923 |
102926 |
104849 |
|
| 1291 |
1923 |
| r.buf.utf8 = 1; |
| 1292 |
|
| |
| 1293 |
|
| #ifdef STENCIL_HAVE_COMPUTED_GOTO |
| 1294 |
209698 |
| ok = stencil_dispatch.force_switch |
| 1295 |
0 |
| ? stencil_vm_switch(aTHX_ &r) |
| 1296 |
104849 |
▶ | : stencil_vm_goto(aTHX_ &r); |
Branch: 1 branch
| Branch | True | False | Total |
| : stencil_vm_goto(aTHX_ &r); |
0 |
104849 |
104849 |
|
| 1297 |
|
| #else |
| 1298 |
|
| ok = stencil_vm_switch(aTHX_ &r); |
| 1299 |
|
| #endif |
| 1300 |
|
| |
| 1301 |
|
| /* unwind whatever an error path left live */ |
| 1302 |
104908 |
▶ | while (r.sp > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (r.sp > 0) { |
59 |
104849 |
104908 |
|
| 1303 |
59 |
| r.sp--; |
| 1304 |
59 |
| val_release(aTHX_ &r.stack[r.sp]); |
| 1305 |
|
| } |
| 1306 |
104889 |
▶ | while (r.nf > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (r.nf > 0) |
40 |
104849 |
104889 |
|
| 1307 |
40 |
| frame_release(aTHX_ &r.frames[--r.nf]); |
| 1308 |
104988 |
▶ | while (r.nb > 0) |
Branch: 1 branch
| Branch | True | False | Total |
| while (r.nb > 0) |
139 |
104849 |
104988 |
|
| 1309 |
139 |
| SvREFCNT_dec(r.binds[--r.nb].sv); |
| 1310 |
104849 |
| SvREFCNT_dec(r.scratch[0]); |
| 1311 |
104849 |
| SvREFCNT_dec(r.scratch[1]); |
| 1312 |
104849 |
▶ | if (heap_stack) |
Branch: 1 branch
| Branch | True | False | Total |
| if (heap_stack) |
0 |
104849 |
104849 |
|
| 1313 |
0 |
| Safefree(r.stack); |
| 1314 |
104849 |
▶ | if (heap_frames) |
Branch: 1 branch
| Branch | True | False | Total |
| if (heap_frames) |
0 |
104849 |
104849 |
|
| 1315 |
0 |
| Safefree(r.frames); |
| 1316 |
104849 |
▶ | if (heap_binds) |
Branch: 1 branch
| Branch | True | False | Total |
| if (heap_binds) |
0 |
104849 |
104849 |
|
| 1317 |
0 |
| Safefree(r.binds); |
| 1318 |
|
| |
| 1319 |
104849 |
▶ | if (!ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) { |
99 |
104750 |
104849 |
|
| 1320 |
99 |
| SvREFCNT_dec(r.buf.sv); |
| 1321 |
99 |
▶ | if (err) |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) |
99 |
0 |
99 |
|
| 1322 |
99 |
▶ | *err = sv_2mortal(newSVpvf( |
Branch: 1 branch
| Branch | True | False | Total |
| *err = sv_2mortal(newSVpvf( |
99 |
0 |
99 |
|
| 1323 |
|
| "Template::Stencil: %s:%u: %s", |
| 1324 |
|
| r.err_final ? r.err_name : r.unit_name, |
| 1325 |
|
| (unsigned)r.err_line, r.errbuf)); |
| 1326 |
99 |
| return NULL; |
| 1327 |
|
| } |
| 1328 |
|
| { |
| 1329 |
104750 |
| SV *out = stencil_buf_done(&r.buf); |
| 1330 |
104750 |
▶ | if (!(flags & STENCIL_RF_CHARS)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(flags & STENCIL_RF_CHARS)) |
104702 |
48 |
104750 |
|
| 1331 |
104702 |
| SvUTF8_off(out); /* default: wire-ready UTF-8 bytes */ |
| 1332 |
|
| /* Profile the capacity (SvLEN, not SvCUR): the escaper reserves |
| 1333 |
|
| * worst-case space, so a profile of the final length alone would |
| 1334 |
|
| * re-grow on every render with escaped values. Only a render that |
| 1335 |
|
| * had to grow raises it: newSV(n) returns a little more than n, |
| 1336 |
|
| * so recording SvLEN after every render ratcheted the buffer up |
| 1337 |
|
| * two bytes a render, without end. */ |
| 1338 |
104750 |
▶ | if (r.buf.grew && SvLEN(out) > entry_prog->profiled_size) |
Branch: 2 branches
| Branch | True | False | Total |
| if (r.buf.grew && SvLEN(out) > entry_prog->profiled_size) |
367 |
104383 |
104750 |
| if (r.buf.grew && SvLEN(out) > entry_prog->profiled_size) |
367 |
0 |
367 |
|
| 1339 |
367 |
| entry_prog->profiled_size = SvLEN(out); |
| 1340 |
104750 |
| return out; |
| 1341 |
|
| } |
| 1342 |
|
| } |
| 1343 |
|
| |
| 1344 |
100661 |
| SV *stencil_render_run(pTHX_ stencil_program *prog, HV *data, |
| 1345 |
|
| uint32_t flags, const char *tname, SV **err) |
| 1346 |
|
| { |
| 1347 |
100661 |
| return stencil_render_core(aTHX_ prog, NULL, tname, |
| 1348 |
|
| NULL, NULL, NULL, |
| 1349 |
|
| prog->max_stack, prog->max_frames, |
| 1350 |
|
| prog->max_binds, data, NULL, NULL, flags, err); |
| 1351 |
|
| } |