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