| 1 |
|
| /* punk_views.h - the C view-engine registry. |
| 2 |
|
| * |
| 3 |
|
| * Punk::Views is entirely XS: the object is a blessed IV-ref to this struct. |
| 4 |
|
| * new() resolves each `views Name => \%opts` to Punk::View::Name (or the |
| 5 |
|
| * class as written for '+Full::Class'), loads and instantiates it (the boot |
| 6 |
|
| * -time Perl calls), and stores the engine object keyed by name; the first is |
| 7 |
|
| * the default. render() - the hot path - looks the engine up, calls its |
| 8 |
|
| * render, and assembles the PSGI triplet in C, so a rendered page crosses the |
| 9 |
|
| * Perl boundary only for the engine's own render (which is itself XS in |
| 10 |
|
| * Template::Stencil). lib/Punk/Views.pm is documentation only. |
| 11 |
|
| * |
| 12 |
|
| * Perl headers and punk_response.h must be included before this file. |
| 13 |
|
| */ |
| 14 |
|
| |
| 15 |
|
| #ifndef PUNK_VIEWS_H |
| 16 |
|
| #define PUNK_VIEWS_H |
| 17 |
|
| |
| 18 |
|
| typedef struct { |
| 19 |
|
| HV *engines; /* engine name -> the engine object SV */ |
| 20 |
|
| SV *deflt; /* the default engine's name (the first registered) */ |
| 21 |
|
| } pv_views; |
| 22 |
|
| |
| 23 |
2234 |
| static pv_views *pv_of(pTHX_ SV *self) { |
| 24 |
2234 |
▶ | if (!SvROK(self) || !SvIOK(SvRV(self))) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
2234 |
0 |
2234 |
| if (!SvROK(self) || !SvIOK(SvRV(self))) |
0 |
2234 |
2234 |
|
| 25 |
0 |
| croak("Punk::Views: not a view registry"); |
| 26 |
2234 |
| return (pv_views *)INT2PTR(void *, SvIV(SvRV(self))); |
| 27 |
|
| } |
| 28 |
|
| |
| 29 |
70 |
| static pv_views *pv_new(pTHX) { |
| 30 |
|
| pv_views *v; |
| 31 |
70 |
| Newxz(v, 1, pv_views); |
| 32 |
70 |
| v->engines = newHV(); |
| 33 |
70 |
| v->deflt = NULL; |
| 34 |
70 |
| return v; |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
68 |
| static void pv_free(pTHX_ pv_views *v) { |
| 38 |
68 |
▶ | if (!v) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v) return; |
0 |
68 |
68 |
|
| 39 |
68 |
▶ | if (v->engines) SvREFCNT_dec((SV *)v->engines); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->engines) SvREFCNT_dec((SV *)v->engines); |
68 |
0 |
68 |
|
| 40 |
68 |
▶ | if (v->deflt) SvREFCNT_dec(v->deflt); |
Branch: 1 branch
| Branch | True | False | Total |
| if (v->deflt) SvREFCNT_dec(v->deflt); |
68 |
0 |
68 |
|
| 41 |
68 |
| Safefree(v); |
| 42 |
|
| } |
| 43 |
|
| |
| 44 |
|
| /* Resolve, load and instantiate one engine, storing it under its name; the |
| 45 |
|
| * first registered becomes the default. Mirrors the old Perl Punk::Views::new |
| 46 |
|
| * exactly, including its croaks. */ |
| 47 |
72 |
| static void pv_load_engine(pTHX_ pv_views *v, SV *name_sv, SV *opts) { |
| 48 |
|
| STRLEN nl; |
| 49 |
72 |
| const char *name = SvPV_const(name_sv, nl); |
| 50 |
72 |
▶ | int plus = (nl > 0 && name[0] == '+'); |
Branch: 2 branches
| Branch | True | False | Total |
| int plus = (nl > 0 && name[0] == '+'); |
72 |
0 |
72 |
| int plus = (nl > 0 && name[0] == '+'); |
8 |
64 |
72 |
|
| 51 |
72 |
▶ | const char *key = plus ? name + 1 : name; |
Branch: 1 branch
| Branch | True | False | Total |
| const char *key = plus ? name + 1 : name; |
8 |
64 |
72 |
|
| 52 |
72 |
▶ | STRLEN keyl = plus ? nl - 1 : nl; |
Branch: 1 branch
| Branch | True | False | Total |
| STRLEN keyl = plus ? nl - 1 : nl; |
8 |
64 |
72 |
|
| 53 |
72 |
| SV *classsv = plus ? sv_2mortal(newSVpvn(name + 1, nl - 1)) |
| 54 |
72 |
▶ | : sv_2mortal(newSVpvf("Punk::View::%.*s", (int)nl, name)); |
Branch: 1 branch
| Branch | True | False | Total |
| : sv_2mortal(newSVpvf("Punk::View::%.*s", (int)nl, name)); |
8 |
64 |
72 |
|
| 55 |
|
| STRLEN cl; |
| 56 |
72 |
| const char *class = SvPV_const(classsv, cl); |
| 57 |
72 |
| HV *stash = gv_stashpvn(class, cl, 0); |
| 58 |
|
| SV *engine; |
| 59 |
|
| |
| 60 |
72 |
▶ | if (!stash || !gv_fetchmethod_autoload(stash, "render", 0)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!stash || !gv_fetchmethod_autoload(stash, "render", 0)) { |
70 |
2 |
72 |
| if (!stash || !gv_fetchmethod_autoload(stash, "render", 0)) { |
0 |
70 |
70 |
|
| 61 |
2 |
| SV *req = pk_require_src(aTHX_ class); |
| 62 |
1 |
| eval_sv(req, G_SCALAR); |
| 63 |
1 |
▶ | if (SvTRUE(ERRSV)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvTRUE(ERRSV)) |
1 |
0 |
1 |
| if (SvTRUE(ERRSV)) |
1 |
0 |
1 |
|
| 64 |
2 |
▶ | croak("Punk::Views: engine '%.*s' (%s) failed to load: %s", |
Branch: 1 branch
| Branch | True | False | Total |
| croak("Punk::Views: engine '%.*s' (%s) failed to load: %s", |
1 |
0 |
1 |
|
| 65 |
|
| (int)keyl, key, class, SvPV_nolen(ERRSV)); |
| 66 |
0 |
| stash = gv_stashpvn(class, cl, 0); |
| 67 |
|
| } |
| 68 |
70 |
▶ | if (!stash || !gv_fetchmethod_autoload(stash, "new", 0)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!stash || !gv_fetchmethod_autoload(stash, "new", 0)) |
70 |
0 |
70 |
| if (!stash || !gv_fetchmethod_autoload(stash, "new", 0)) |
0 |
70 |
70 |
|
| 69 |
0 |
| croak("Punk::Views: engine '%.*s' (%s) does not implement new", |
| 70 |
|
| (int)keyl, key, class); |
| 71 |
70 |
▶ | if (!gv_fetchmethod_autoload(stash, "render", 0)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!gv_fetchmethod_autoload(stash, "render", 0)) |
0 |
70 |
70 |
|
| 72 |
0 |
| croak("Punk::Views: engine '%.*s' (%s) does not implement render", |
| 73 |
|
| (int)keyl, key, class); |
| 74 |
70 |
▶ | if (hv_exists(v->engines, key, (I32)keyl)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (hv_exists(v->engines, key, (I32)keyl)) |
0 |
70 |
70 |
|
| 75 |
0 |
| croak("Punk::Views: engine '%.*s' registered twice", (int)keyl, key); |
| 76 |
|
| |
| 77 |
|
| { |
| 78 |
70 |
| dSP; |
| 79 |
|
| int count; |
| 80 |
70 |
| ENTER; SAVETMPS; |
| 81 |
70 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
70 |
70 |
|
| 82 |
70 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
70 |
70 |
|
| 83 |
70 |
| PUSHs(classsv); |
| 84 |
70 |
▶ | PUSHs((opts && SvOK(opts)) ? opts |
Branch: 2 branches
| Branch | True | False | Total |
| PUSHs((opts && SvOK(opts)) ? opts |
70 |
0 |
70 |
| PUSHs((opts && SvOK(opts)) ? opts |
0 |
70 |
70 |
|
| 85 |
|
| : sv_2mortal(newRV_noinc((SV *)newHV()))); |
| 86 |
70 |
| PUTBACK; |
| 87 |
70 |
| count = call_method("new", G_SCALAR); |
| 88 |
70 |
| SPAGAIN; |
| 89 |
70 |
▶ | engine = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
Branch: 1 branch
| Branch | True | False | Total |
| engine = count > 0 ? SvREFCNT_inc(POPs) : &PL_sv_undef; |
70 |
0 |
70 |
|
| 90 |
70 |
| PUTBACK; |
| 91 |
70 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
70 |
0 |
70 |
|
| 92 |
|
| } |
| 93 |
70 |
| (void)hv_store(v->engines, key, (I32)keyl, engine, 0); |
| 94 |
70 |
▶ | if (!v->deflt) v->deflt = newSVpvn(key, keyl); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!v->deflt) v->deflt = newSVpvn(key, keyl); |
68 |
2 |
70 |
|
| 95 |
70 |
| } |
| 96 |
|
| |
| 97 |
|
| /* The named (or default) engine object; unknown names croak listing what is |
| 98 |
|
| * registered. */ |
| 99 |
2164 |
| static SV *pv_engine(pTHX_ pv_views *v, SV *name_sv) { |
| 100 |
|
| const char *name; |
| 101 |
|
| STRLEN nl; |
| 102 |
|
| SV **e; |
| 103 |
2169 |
▶ | if (name_sv && SvOK(name_sv)) name = SvPV_const(name_sv, nl); |
Branch: 2 branches
| Branch | True | False | Total |
| if (name_sv && SvOK(name_sv)) name = SvPV_const(name_sv, nl); |
5 |
2159 |
2164 |
| if (name_sv && SvOK(name_sv)) name = SvPV_const(name_sv, nl); |
5 |
0 |
5 |
|
| 104 |
4318 |
▶ | else if (v->deflt) name = SvPV_const(v->deflt, nl); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (v->deflt) name = SvPV_const(v->deflt, nl); |
2159 |
0 |
2159 |
|
| 105 |
0 |
| else croak("Punk::Views: no view engine configured - add a views keyword"); |
| 106 |
2164 |
| e = hv_fetch(v->engines, name, (I32)nl, 0); |
| 107 |
2164 |
▶ | if (e && *e) return *e; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e && *e) return *e; |
2163 |
1 |
2164 |
| if (e && *e) return *e; |
2163 |
0 |
2163 |
|
| 108 |
|
| { |
| 109 |
1 |
| SV *have = sv_2mortal(newSVpvs("")); |
| 110 |
|
| HE *he; |
| 111 |
1 |
| int first = 1; |
| 112 |
1 |
| hv_iterinit(v->engines); |
| 113 |
2 |
▶ | while ((he = hv_iternext(v->engines))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(v->engines))) { |
1 |
1 |
2 |
|
| 114 |
1 |
▶ | if (!first) sv_catpvs(have, ", "); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!first) sv_catpvs(have, ", "); |
0 |
1 |
1 |
|
| 115 |
1 |
| sv_catsv(have, hv_iterkeysv(he)); |
| 116 |
1 |
| first = 0; |
| 117 |
|
| } |
| 118 |
1 |
| croak("Punk::Views: no view engine '%.*s' registered (have: %s)", |
| 119 |
|
| (int)nl, name, SvPV_nolen(have)); |
| 120 |
|
| } |
| 121 |
|
| return NULL; /* not reached */ |
| 122 |
|
| } |
| 123 |
|
| |
| 124 |
|
| /* ---- before_render ------------------------------------------------------- */ |
| 125 |
|
| |
| 126 |
|
| /* Run the application's before_render chain over the data a template is about |
| 127 |
|
| * to be rendered with. Each hook gets ($c, $template, $data) and whatever it |
| 128 |
|
| * returns is dropped: the hook's business is the hashref, and a hook that |
| 129 |
|
| * could replace the response would be a second dispatcher. |
| 130 |
|
| * |
| 131 |
|
| * The chain hangs off the app rather than the request state because a render |
| 132 |
|
| * is reached through a context, which has no route to what the dispatcher is |
| 133 |
|
| * carrying. Absent unless one was registered, so the common render costs one |
| 134 |
|
| * hv_fetch that misses. |
| 135 |
|
| */ |
| 136 |
2162 |
| static void pv_before_render(pTHX_ SV *c, SV *template, SV *data) { |
| 137 |
|
| SV *app, **slot; |
| 138 |
|
| AV *chain; |
| 139 |
|
| SSize_t i, n; |
| 140 |
|
| |
| 141 |
2162 |
▶ | if (!(c && SvROK(c))) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!(c && SvROK(c))) return; |
2162 |
0 |
2162 |
| if (!(c && SvROK(c))) return; |
0 |
2162 |
2162 |
|
| 142 |
2162 |
▶ | if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
2162 |
0 |
2162 |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
2162 |
0 |
2162 |
| if (!(data && SvROK(data) && SvTYPE(SvRV(data)) == SVt_PVHV)) return; |
0 |
2162 |
2162 |
|
| 143 |
2162 |
| app = pcx_get(aTHX_ pcx_av(aTHX_ c), PCX_APP); |
| 144 |
2162 |
▶ | if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; |
2162 |
0 |
2162 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; |
2162 |
0 |
2162 |
| if (!(app && SvROK(app) && SvTYPE(SvRV(app)) == SVt_PVHV)) return; |
0 |
2162 |
2162 |
|
| 145 |
2162 |
| slot = hv_fetchs((HV *)SvRV(app), K_BEFORE_RENDER_C, 0); |
| 146 |
2162 |
▶ | if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
Branch: 4 branches
| Branch | True | False | Total |
| if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
11 |
2151 |
2162 |
| if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
11 |
0 |
11 |
| if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
11 |
0 |
11 |
| if (!(slot && *slot && SvROK(*slot) && SvTYPE(SvRV(*slot)) == SVt_PVAV)) |
0 |
11 |
11 |
|
| 147 |
2151 |
| return; |
| 148 |
|
| |
| 149 |
11 |
| chain = (AV *)SvRV(*slot); |
| 150 |
11 |
| n = av_len(chain) + 1; |
| 151 |
24 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
13 |
11 |
24 |
|
| 152 |
13 |
| SV **cv = av_fetch(chain, i, 0); |
| 153 |
13 |
| dSP; |
| 154 |
13 |
▶ | if (!(cv && *cv && SvROK(*cv))) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!(cv && *cv && SvROK(*cv))) continue; |
13 |
0 |
13 |
| if (!(cv && *cv && SvROK(*cv))) continue; |
13 |
0 |
13 |
| if (!(cv && *cv && SvROK(*cv))) continue; |
0 |
13 |
13 |
|
| 155 |
13 |
| ENTER; SAVETMPS; |
| 156 |
13 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
13 |
13 |
|
| 157 |
13 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
13 |
13 |
|
| 158 |
13 |
| PUSHs(c); |
| 159 |
13 |
| PUSHs(template); |
| 160 |
13 |
| PUSHs(data); |
| 161 |
13 |
| PUTBACK; |
| 162 |
13 |
| call_sv(*cv, G_VOID | G_DISCARD); |
| 163 |
13 |
| SPAGAIN; |
| 164 |
13 |
| PUTBACK; |
| 165 |
13 |
▶ | FREETMPS; LEAVE; |
Branch: 1 branch
| Branch | True | False | Total |
| FREETMPS; LEAVE; |
0 |
13 |
13 |
|
| 166 |
|
| } |
| 167 |
|
| } |
| 168 |
|
| |
| 169 |
|
| #endif /* PUNK_VIEWS_H */ |