| 1 |
|
| /* punk_stencil.h - Punk::View::Stencil, the shipped view engine, in C. |
| 2 |
|
| * |
| 3 |
|
| * The adapter over Template::Stencil is two array slots and no Perl: the |
| 4 |
|
| * options as registered, and the Template::Stencil object built from them. |
| 5 |
|
| * Rendering goes through Template::Stencil's C ABI (st_abi.h, reached through |
| 6 |
|
| * ExtUtils::Depends), so the path from the dispatcher to the template VM is |
| 7 |
|
| * |
| 8 |
|
| * punk_views.h -> this XSUB -> abi->render |
| 9 |
|
| * |
| 10 |
|
| * with no Perl frame anywhere in it. The engine object is built once, at boot, |
| 11 |
|
| * through Template::Stencil->new - the one Perl call, in the one place where a |
| 12 |
|
| * bad option should still fail with the rest of the configuration. |
| 13 |
|
| * |
| 14 |
|
| * Perl headers and punk_static.h (punk_closure is not used here, but the file |
| 15 |
|
| * ordering in Punk.xs puts this after it) must be in scope first. |
| 16 |
|
| */ |
| 17 |
|
| |
| 18 |
|
| #ifndef PUNK_STENCIL_H |
| 19 |
|
| #define PUNK_STENCIL_H |
| 20 |
|
| |
| 21 |
|
| #include "st_abi.h" |
| 22 |
|
| |
| 23 |
|
| enum { PVS_OPTS = 0, PVS_ENGINE = 1 }; /* the object's slots */ |
| 24 |
|
| |
| 25 |
|
| static const st_abi *PUNK_ST = NULL; |
| 26 |
|
| static int PUNK_ST_TRIED = 0; |
| 27 |
|
| |
| 28 |
|
| /* Resolve (once) Template::Stencil's ABI table, or NULL. PUNK_FAKE_ST_BAD |
| 29 |
|
| * simulates a version mismatch for the guard test. */ |
| 30 |
2880 |
| static const st_abi *punk_st_try(pTHX) { |
| 31 |
2880 |
▶ | if (!PUNK_ST_TRIED) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!PUNK_ST_TRIED) { |
54 |
2826 |
2880 |
|
| 32 |
54 |
| dSP; int count, ok; IV p = 0; |
| 33 |
54 |
| PUNK_ST_TRIED = 1; |
| 34 |
54 |
| ok = pk_require_once(aTHX_ "Template::Stencil", FALSE); |
| 35 |
54 |
| SPAGAIN; /* the require may have reallocated the value stack */ |
| 36 |
54 |
▶ | if (ok) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ok) { |
54 |
0 |
54 |
|
| 37 |
54 |
▶ | ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
Branch: 1 branch
| Branch | True | False | Total |
| ENTER; SAVETMPS; PUSHMARK(SP); PUTBACK; |
0 |
54 |
54 |
|
| 38 |
54 |
| count = call_pv("Template::Stencil::_abi_ptr", G_SCALAR | G_EVAL); |
| 39 |
54 |
| SPAGAIN; |
| 40 |
54 |
▶ | if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
54 |
0 |
54 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
54 |
0 |
54 |
| if (!SvTRUE(ERRSV) && count > 0) p = POPi; |
54 |
0 |
54 |
|
| 41 |
0 |
▶ | else if (count > 0) (void)POPs; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (count > 0) (void)POPs; |
0 |
0 |
0 |
|
| 42 |
54 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
54 |
0 |
54 |
|
| 43 |
54 |
▶ | if (p) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (p) { |
54 |
0 |
54 |
|
| 44 |
54 |
| const st_abi *a = INT2PTR(const st_abi *, p); |
| 45 |
54 |
▶ | if (a && !getenv("PUNK_FAKE_ST_BAD") |
Branch: 2 branches
| Branch | True | False | Total |
| if (a && !getenv("PUNK_FAKE_ST_BAD") |
54 |
0 |
54 |
| if (a && !getenv("PUNK_FAKE_ST_BAD") |
53 |
1 |
54 |
|
| 46 |
53 |
▶ | && a->abi_version >= ST_ABI_VERSION) |
Branch: 1 branch
| Branch | True | False | Total |
| && a->abi_version >= ST_ABI_VERSION) |
53 |
0 |
53 |
|
| 47 |
53 |
| PUNK_ST = a; |
| 48 |
|
| } |
| 49 |
|
| } |
| 50 |
|
| } |
| 51 |
2880 |
| return PUNK_ST; |
| 52 |
|
| } |
| 53 |
|
| |
| 54 |
|
| /* The table, croaking if it is missing or too old. Template::Stencil 0.02+ is |
| 55 |
|
| * a hard prerequisite, so this is a boot-environment error rather than a |
| 56 |
|
| * per-request fallback - there is no Perl render path to fall back to. */ |
| 57 |
2874 |
| static const st_abi *punk_st(pTHX) { |
| 58 |
2874 |
| const st_abi *a = punk_st_try(aTHX); |
| 59 |
2874 |
▶ | if (!a) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!a) |
1 |
2873 |
2874 |
|
| 60 |
1 |
| croak("Punk::View::Stencil: needs Template::Stencil with a compatible " |
| 61 |
|
| "C ABI (ST_ABI_VERSION %d); upgrade Template::Stencil to 0.02+", |
| 62 |
|
| ST_ABI_VERSION); |
| 63 |
2873 |
| return a; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
2168 |
| static AV *pvs_av(pTHX_ SV *self) { |
| 67 |
2168 |
▶ | if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) |
2168 |
0 |
2168 |
| if (!SvROK(self) || SvTYPE(SvRV(self)) != SVt_PVAV) |
0 |
2168 |
2168 |
|
| 68 |
0 |
| croak("Punk::View::Stencil: not a view object"); |
| 69 |
2168 |
| return (AV *)SvRV(self); |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
2166 |
| static SV *pvs_slot(pTHX_ SV *self, I32 slot) { |
| 73 |
2166 |
| SV **e = av_fetch(pvs_av(aTHX_ self), slot, 0); |
| 74 |
2166 |
▶ | return (e && *e && SvOK(*e)) ? *e : NULL; |
Branch: 3 branches
| Branch | True | False | Total |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
2165 |
1 |
2166 |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
2165 |
0 |
2165 |
| return (e && *e && SvOK(*e)) ? *e : NULL; |
2165 |
0 |
2165 |
|
| 75 |
|
| } |
| 76 |
|
| |
| 77 |
|
| /* Template::Stencil->new(\%opts) - the single Perl call, at boot. Building the |
| 78 |
|
| * engine here rather than on first render is the point: an option Stencil will |
| 79 |
|
| * not accept fails with the rest of the configuration, not on a request. */ |
| 80 |
65 |
| static SV *pvs_build_engine(pTHX_ SV *opts) { |
| 81 |
65 |
| dSP; |
| 82 |
|
| SV *obj; |
| 83 |
|
| int count; |
| 84 |
65 |
| ENTER; SAVETMPS; |
| 85 |
65 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
65 |
65 |
|
| 86 |
65 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
65 |
65 |
|
| 87 |
65 |
| mPUSHs(newSVpvs("Template::Stencil")); |
| 88 |
65 |
▶ | PUSHs(opts && SvOK(opts) ? opts |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHs(opts && SvOK(opts) ? opts |
65 |
0 |
65 |
| PUSHs(opts && SvOK(opts) ? opts |
0 |
65 |
65 |
|
| 89 |
|
| : sv_2mortal(newRV_noinc((SV *)newHV()))); |
| 90 |
65 |
| PUTBACK; |
| 91 |
65 |
| count = call_method("new", G_SCALAR); |
| 92 |
64 |
| SPAGAIN; |
| 93 |
64 |
▶ | obj = count > 0 ? newSVsv(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| obj = count > 0 ? newSVsv(POPs) : &PL_sv_undef; |
64 |
0 |
64 |
|
| 94 |
64 |
▶ | PUTBACK; FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| PUTBACK; FREETMPS; LEAVE; |
64 |
0 |
64 |
|
| 95 |
64 |
▶ | if (!SvROK(obj)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(obj)) |
0 |
64 |
64 |
|
| 96 |
0 |
| croak("Punk::View::Stencil: Template::Stencil->new returned no object"); |
| 97 |
64 |
| return obj; |
| 98 |
|
| } |
| 99 |
|
| |
| 100 |
|
| /* One render, entirely in C. Returns the bytes SV (caller owns) or croaks with |
| 101 |
|
| * the template error, which is what the render method did and what Punk::Views |
| 102 |
|
| * turns into a 500 further up. */ |
| 103 |
2166 |
| static SV *pvs_render(pTHX_ SV *self, SV *tmpl, SV *data, SV *opts) { |
| 104 |
2166 |
| const st_abi *abi = punk_st(aTHX); |
| 105 |
2166 |
| SV *engine_sv = pvs_slot(aTHX_ self, PVS_ENGINE); |
| 106 |
|
| void *engine; |
| 107 |
2166 |
| SV *err = NULL, *out; |
| 108 |
2166 |
| HV *dhv = NULL; |
| 109 |
|
| |
| 110 |
2166 |
▶ | if (!engine_sv) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!engine_sv) |
1 |
2165 |
2166 |
|
| 111 |
1 |
| croak("Punk::View::Stencil: this view has no engine"); |
| 112 |
|
| /* engine_of on every render, not a cached pointer: an ithread clone drops |
| 113 |
|
| * its engine and rebuilds lazily, so a cached one would dangle. The lookup |
| 114 |
|
| * is a walk of the object's magic chain. */ |
| 115 |
2165 |
| engine = abi->engine_of(aTHX_ engine_sv); |
| 116 |
2165 |
▶ | if (!engine) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!engine) |
0 |
2165 |
2165 |
|
| 117 |
0 |
| croak("Punk::View::Stencil: the engine is not a Template::Stencil " |
| 118 |
|
| "object"); |
| 119 |
|
| |
| 120 |
2165 |
▶ | if (data && SvOK(data)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (data && SvOK(data)) { |
2165 |
0 |
2165 |
| if (data && SvOK(data)) { |
2163 |
2 |
2165 |
|
| 121 |
2163 |
▶ | if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) |
2162 |
1 |
2163 |
| if (!SvROK(data) || SvTYPE(SvRV(data)) != SVt_PVHV) |
0 |
2162 |
2162 |
|
| 122 |
1 |
| croak("Punk::View::Stencil: render data must be a hashref"); |
| 123 |
2162 |
| dhv = (HV *)SvRV(data); |
| 124 |
|
| } |
| 125 |
4328 |
▶ | out = abi->render(aTHX_ engine, tmpl, dhv, |
Branch: 1 branch
| Branch | True | False | Total |
| out = abi->render(aTHX_ engine, tmpl, dhv, |
2164 |
0 |
2164 |
|
| 126 |
2164 |
▶ | (opts && SvOK(opts)) ? opts : NULL, &err); |
Branch: 1 branch
| Branch | True | False | Total |
| (opts && SvOK(opts)) ? opts : NULL, &err); |
14 |
2150 |
2164 |
|
| 127 |
2161 |
▶ | if (!out) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!out) |
4 |
2157 |
2161 |
|
| 128 |
4 |
▶ | croak_sv(err ? err : sv_2mortal(newSVpvs( |
Branch: 1 branch
| Branch | True | False | Total |
| croak_sv(err ? err : sv_2mortal(newSVpvs( |
0 |
4 |
4 |
|
| 129 |
|
| "Punk::View::Stencil: render failed"))); |
| 130 |
2157 |
| return out; |
| 131 |
|
| } |
| 132 |
|
| |
| 133 |
|
| #endif /* PUNK_STENCIL_H */ |