| 1 |
|
| MODULE = Punk PACKAGE = Punk::View::Stencil |
| 2 |
|
| |
| 3 |
|
| PROTOTYPES: DISABLE |
| 4 |
|
| |
| 5 |
|
| # The shipped view engine (punk_stencil.h). Two array slots - the options as |
| 6 |
|
| # registered and the Template::Stencil object built from them - and a render |
| 7 |
|
| # that reaches the template VM through Template::Stencil's C ABI, so no Perl |
| 8 |
|
| # frame sits between the dispatcher and the engine. lib/Punk/View/Stencil.pm |
| 9 |
|
| # is documentation only. |
| 10 |
|
| |
| 11 |
|
| # new(\%opts) or new(%opts). Punk::Views (punk_views.h) hands the registered |
| 12 |
|
| # options as one positional hashref; the list form is for anyone constructing |
| 13 |
|
| # the view directly. Builds the Template::Stencil engine here and now, so an |
| 14 |
|
| # option it will not accept fails at boot with the rest of the configuration |
| 15 |
|
| # rather than on the first render. |
| 16 |
|
| SV * |
| 17 |
|
| new(class, ...) |
| 18 |
|
| SV *class |
| 19 |
|
| CODE: |
| 20 |
|
| { |
| 21 |
|
| HV *opts; |
| 22 |
|
| SV *opts_rv; |
| 23 |
|
| AV *av; |
| 24 |
66 |
▶ | if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
65 |
1 |
66 |
| if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
65 |
0 |
65 |
| if (items == 2 && SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
65 |
0 |
65 |
|
| 25 |
65 |
| opts = (HV *)SvRV(ST(1)); |
| 26 |
65 |
| SvREFCNT_inc((SV *)opts); |
| 27 |
|
| } |
| 28 |
|
| else { |
| 29 |
|
| int i; |
| 30 |
1 |
▶ | if (!(items % 2)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(items % 2)) |
0 |
1 |
1 |
|
| 31 |
0 |
| croak("Punk::View::Stencil->new: odd number of options"); |
| 32 |
1 |
| opts = newHV(); |
| 33 |
2 |
▶ | for (i = 1; i + 1 < items; i += 2) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i + 1 < items; i += 2) |
1 |
1 |
2 |
|
| 34 |
1 |
| (void)hv_store_ent(opts, ST(i), newSVsv(ST(i + 1)), 0); |
| 35 |
|
| } |
| 36 |
66 |
| opts_rv = sv_2mortal(newRV_noinc((SV *)opts)); |
| 37 |
|
| |
| 38 |
|
| /* resolve the ABI before building anything, so a missing or too-old |
| 39 |
|
| * Template::Stencil is a boot error naming the version it needs */ |
| 40 |
66 |
| (void)punk_st(aTHX); |
| 41 |
|
| |
| 42 |
65 |
| av = newAV(); |
| 43 |
65 |
| av_extend(av, PVS_ENGINE); |
| 44 |
65 |
| (void)av_store(av, PVS_OPTS, newSVsv(opts_rv)); |
| 45 |
65 |
| (void)av_store(av, PVS_ENGINE, pvs_build_engine(aTHX_ opts_rv)); |
| 46 |
64 |
| RETVAL = sv_bless(newRV_noinc((SV *)av), gv_stashsv(class, GV_ADD)); |
| 47 |
|
| } |
| 48 |
|
| OUTPUT: |
| 49 |
|
| RETVAL |
| 50 |
|
| |
| 51 |
|
| # render($template, \%data, \%opts?) - one indirect call into Template::Stencil |
| 52 |
|
| # and back. The bytes belong to the caller; Punk::Views copies them into the |
| 53 |
|
| # response body rather than letting a lexical steal the buffer. |
| 54 |
|
| SV * |
| 55 |
|
| render(self, template, data = &PL_sv_undef, opts = &PL_sv_undef) |
| 56 |
|
| SV *self |
| 57 |
|
| SV *template |
| 58 |
|
| SV *data |
| 59 |
|
| SV *opts |
| 60 |
|
| CODE: |
| 61 |
2166 |
| RETVAL = pvs_render(aTHX_ self, template, data, opts); |
| 62 |
|
| OUTPUT: |
| 63 |
|
| RETVAL |
| 64 |
|
| |
| 65 |
|
| # The registered options, and the Template::Stencil object itself for anything |
| 66 |
|
| # that wants to reach past the adapter. Both read/write, as the slots were. |
| 67 |
|
| SV * |
| 68 |
|
| opts(self, ...) |
| 69 |
|
| SV *self |
| 70 |
|
| ALIAS: |
| 71 |
|
| engine = 1 |
| 72 |
|
| CODE: |
| 73 |
|
| { |
| 74 |
2 |
| AV *av = pvs_av(aTHX_ self); |
| 75 |
2 |
| I32 slot = ix ? PVS_ENGINE : PVS_OPTS; |
| 76 |
|
| SV **e; |
| 77 |
2 |
▶ | if (items > 1) (void)av_store(av, slot, newSVsv(ST(1))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) (void)av_store(av, slot, newSVsv(ST(1))); |
0 |
2 |
2 |
|
| 78 |
2 |
| e = av_fetch(av, slot, 0); |
| 79 |
2 |
▶ | RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
Branch: 2 branches
| Branch | True | False | Total |
| RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
2 |
0 |
2 |
| RETVAL = (e && *e) ? newSVsv(*e) : newSV(0); |
2 |
0 |
2 |
|
| 80 |
|
| } |
| 81 |
|
| OUTPUT: |
| 82 |
|
| RETVAL |
| 83 |
|
| |
| 84 |
|
| # Whether the C ABI resolved - the guard test drives this with |
| 85 |
|
| # PUNK_FAKE_ST_BAD to prove the boot-time croak, since there is no Perl |
| 86 |
|
| # render path to fall back to. |
| 87 |
|
| IV |
| 88 |
|
| _abi_ok() |
| 89 |
|
| CODE: |
| 90 |
6 |
▶ | RETVAL = punk_st_try(aTHX) ? 1 : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = punk_st_try(aTHX) ? 1 : 0; |
0 |
6 |
6 |
|
| 91 |
|
| OUTPUT: |
| 92 |
|
| RETVAL |