blib/lib/Text/Nunjucks/Template.pm

filter: Statementstmt 94.1% 192 / 204 Branchbran 85.9% 55 / 64 Conditioncond 58.0% 18 / 31 MC/DCmcdc 27.7% 5 / 18 Subroutinesub 92.5% 37 / 40 Podpod 37.5% 3 / 8 totaltotal 84.9% 310 / 365 CC 93 SCAR 18.8
CC
93
Cov
89%
init
19.6
init
19.6
render
8.0
CRAP 6.6
« blib/lib/Text/Nunjucks/Nodes/CallExtension.pm ↑ Summary blib/lib/Text/Nunjucks/Environment.pm »
1 package Text::Nunjucks::Template;
2 # ABSTRACT: A compiled template that renders (environment.js class Template)
3  
4 15 our $VERSION = '0.001';
5  
6 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Scalar::Util qw( refaddr weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Hash::Util::FieldHash qw( fieldhash );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Text::Nunjucks::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Text::Nunjucks::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Text::Nunjucks::Context::Gone;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Text::Nunjucks::Environment;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # upstream: class Template extends Obj in src/environment.js, exported with
19 # Environment. The constructor's `this.init(...args)` indirection is a
20 # positional-args BUILDARGS feeding a faithful init().
21  
22 15 has env => ( is => 'rw' );
23 15 has path => ( is => 'rw' );
24 15 has tmpl_props => ( is => 'rw' );
25 15 has tmpl_str => ( is => 'rw' );
26 15 has compiled => ( is => 'rw' );
27 15 has blocks => ( is => 'rw' );
28 15 has _root_render_func => ( is => 'rw' );
29 15 has _init_args => ( is => 'ro' );
30  
31 # In JS `this.rootRenderFunc = props.root` stores a function and
32 # `parentTemplate.rootRenderFunc(env, ...)` CALLS it -- a callable property.
33 # The compiler emits that same `$parentTemplate->root_render_func($env, ...)`
34 # call shape (compile_root's extends tail), so root_render_func is a method
35 # that INVOKES the stored coderef; the coderef itself lives in the private
36 # _root_render_func slot, set at compile.
37 1052 ▶sub root_render_func {
Subroutine root_render_func: called CC=1 SCAR=0.2
Pod undocumented
38 0 ▶ my ( $self, @args ) = @_;
Subroutine root_render_func: not called CC=1 SCAR=0.2
Pod undocumented
39 # a cached parent of a template that includes itself N deep: N calls deep,
40 # as in render (k83)
41 15 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 0 return $self->_root_render_func->(@args);
43 }
44  
45 52651 ▶sub _lib { 'Text::Nunjucks::Lib' }
Subroutine _lib: called CC=1 SCAR=0.0
46 18529 ▶sub _runtime_class { 'Text::Nunjucks::Runtime' }
Subroutine _runtime_class: called CC=1 SCAR=0.0
47 9279 ▶sub _compiler_class { 'Text::Nunjucks::Compiler' }
Subroutine _compiler_class: called CC=1 SCAR=0.0
48 11977 ▶sub _context_class { 'Text::Nunjucks::Context' }
Subroutine _context_class: called CC=1 SCAR=0.0
49 4 ▶sub _environment_class { 'Text::Nunjucks::Environment' }
Subroutine _environment_class: called CC=1 SCAR=0.0
50 0 ▶sub _gone_class { 'Text::Nunjucks::Context::Gone' }
Subroutine _gone_class: not called CC=1 SCAR=6.9
51  
52 9297 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod undocumented
53 9297 my ( $class, @args ) = @_;
54 9297 return { _init_args => [@args] };
55 }
56  
57 9297 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
58 9297 my ($self) = @_;
59 9297 ▶ $self->init( @{ $self->_init_args // [] } );
Condition50%1/2
$self->_init_args // []
l!l
92970
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
Truth table50%1/2
A
A= $self->_init_args
Aresult
11
00
60 9297 delete $self->{_init_args};
61 }
62  
63 9297 ▶sub init {
Subroutine init: called CC=11 SCAR=29.8
Pod undocumented
64 9297 my ( $self, $src, $env, $path, $eager_compile ) = @_;
65 9297 ▶ $self->env( $env || $self->_environment_class->new );
Condition66%2/3
$env || $self->_environment_class->new
l!l&&r!l&&!r
929340
MC/DC0%0/2
$env || $self->_environment_class->new
$env $self->_environment_class->new
Truth table66%2/3
A || B
A= $envB= $self->_environment_class->new
ABresult
1X1
011
000
66  
67 9297 ▶ if ( $self->_lib->is_object($src) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_lib->is_object($src)) { } 15 9282 9297
elsif ($self->_lib->is_string($src)) { } 9282 0 9282
68 15 my $type = $src->{type};
69 15 ▶ if ( defined($type) && $type eq 'code' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $type and $type eq 'code') { } 15 0 15
elsif (defined $type and $type eq 'string') { } 0 0 0
Condition33%1/3
defined $type and $type eq "code"
!ll&&!rl&&r
0015
MC/DC0%0/2
defined $type and $type eq "code"
defined $type $type eq "code"
Truth table33%1/3
A and B
A= defined $typeB= $type eq "code"
ABresult
0X0
100
111
70 15 $self->tmpl_props( $src->{obj} );
71 }
72 ▶ elsif ( defined($type) && $type eq 'string' ) {
Condition0%0/3
defined $type and $type eq "string"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and $type eq "string"
defined $type $type eq "string"
Truth table0%0/3
A and B
A= defined $typeB= $type eq "string"
ABresult
0X0
100
111
73 0 $self->tmpl_str( $src->{obj} );
74 }
75 else {
76 0 ▶ die $self->_lib_error(
Condition0%0/2
$type // "undefined"
l!l
00
MC/DC0%0/1
$type // "undefined"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
77 "Unexpected template object type " . ( $type // 'undefined' )
78 . "; expected 'code', or 'string'" );
79 }
80 }
81 elsif ( $self->_lib->is_string($src) ) {
82 9282 $self->tmpl_str($src);
83 }
84 else {
85 0 die $self->_lib_error('src must be a string or an object describing the source');
86 }
87  
88 9297 $self->path($path);
89  
90 9297 ▶ if ($eager_compile) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eager_compile) { } 543 8754 9297
91 543 my $ok = eval { $self->_compile; 1 };
92 543 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) 0 543 543
93 0 die $self->_lib->_prettify_error( $self->path, $self->env->opts->{dev}, $@ );
94 }
95 }
96 else {
97 8754 $self->compiled(0);
98 }
99 }
100  
101 0 ▶sub _lib_error {
Subroutine _lib_error: not called CC=1 SCAR=6.9
102 0 my ( $self, $msg ) = @_;
103 0 return Text::Nunjucks::Lib::Error->new($msg);
104 }
105  
106 10407 ▶sub render {
Subroutine render: called CC=8 SCAR=20.8
Pod documented
107 10407 my ( $self, $ctx, $parent_frame, $cb, $including ) = @_;
108 10407 ▶ if ( $self->_lib->is_function($ctx) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_lib->is_function($ctx)) { } 0 10407 10407
elsif ($self->_lib->is_function($parent_frame)) { } 4372 6035 10407
109 0 $cb = $ctx;
110 0 $ctx = {};
111 }
112 elsif ( $self->_lib->is_function($parent_frame) ) {
113 4372 $cb = $parent_frame;
114 4372 $parent_frame = undef;
115 }
116  
117 # A parent frame means we're called from another template's internals; the
118 # sync/async nature is inherited, so force an async callback otherwise.
119 10407 my $force_async = !$parent_frame;
120  
121 # Catch compile errors for async rendering.
122 10407 my $ok = eval { $self->compile; 1 };
123 10407 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 15 10392 10407
124 15 my $err = $self->_lib->_prettify_error( $self->path, $self->env->opts->{dev}, $@ );
125 15 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 13 2 15
126 13 return $self->env->_callback_asap( $cb, $err );
127 }
128 else {
129 2 die $err;
130 }
131 }
132  
133 10392 ▶ my $context = $self->_context_class->new( $ctx || {}, $self->blocks, $self->env );
Condition100%2/2
$ctx || {}
l!l
103866
MC/DC100%1/1
$ctx || {}
$ctx
Truth table100%2/2
A
A= $ctx
Aresult
11
00
134  
135 # Port-only (ADR 0021). A compiled `include` passes its own Context as a
136 # fourth argument, which upstream's render does not have: the including
137 # Context has to keep this one alive if a macro of the included template
138 # outlives this render (Context _included_by).
139 10392 ▶ $cb = $context->_included_by( $including, $cb ) if $including;
Branch: 1 branch
BranchTrueFalseTotal
if $including 4960 5432 10392
140  
141 10392 ▶ my $frame = $parent_frame ? $parent_frame->push(1) : $self->_runtime_class->Frame->new;
Branch: 1 branch
BranchTrueFalseTotal
$parent_frame ? : 4960 5432 10392
142 10392 $frame->top_level(1);
143 10392 my $sync_result = undef;
144 10392 my $did_error = 0;
145  
146 # Port-only (k83): a cached template included N times in a row, or one that
147 # includes itself N deep, runs its root N calls deep, the next include
148 # running in the previous one's continuation -- as upstream nests them
149 # (ADR 0003). Perl warns at the 100th nested call of a sub, at the call site;
150 # the depth is bounded by memory, not by Perl (see Lib async_iter).
151 15 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
152 $self->_root_render_func->(
153 $self->env, $context, $frame, $self->_runtime_class,
154 9945 ▶ sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
155 9945 my ( $err, $res ) = @_;
156 # See upstream note: a waterfall task may fail to propagate up the chain
157 # AND fail to return from the top render; guard against a double cb call.
158 9945 ▶ if ( $did_error && $cb && defined $res ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($did_error and $cb and defined $res) 54 9891 9945
Condition100%6/6
$did_error and $cb and defined $res
!ll&&!rl&&r
98791254
$did_error and $cb
!ll&&!rl&&r
9875466
MC/DC100%3/3
$did_error and $cb and defined $res
$did_error $cb defined $res
Truth table100%4/4
A and B and C
A= $did_errorB= $cbC= defined $res
ABCresult
0XX0
10X0
1100
1111
159 54 return;
160 }
161  
162 9891 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 659 9232 9891
163 659 $err = $self->_lib->_prettify_error( $self->path, $self->env->opts->{dev}, $err );
164 659 $did_error = 1;
165 }
166  
167 9891 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 9135 756 9891
168 9135 ▶ if ($force_async) {
Branch: 1 branch
BranchTrueFalseTotal
if ($force_async) { } 4361 4774 9135
169 4361 $self->env->_callback_asap( $cb, $err, $res );
170 }
171 else {
172 4774 $cb->( $err, $res );
173 }
174 }
175 else {
176 756 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 29 727 756
177 29 die $err;
178 }
179 727 $sync_result = $res;
180 }
181 }
182 10392 );
183  
184 10349 return $sync_result;
185 }
186  
187 # Port-only (ADR 0021). The macros a template exports do not hold the Context
188 # they read their library's variables, globals and sibling macros through
189 # (Runtime _render_scope); whoever takes them has to. A compiled `import` does
190 # that itself and calls _get_exported, upstream's getExported. This is the
191 # method a Perl caller uses. The Context lives as long as the hash the
192 # callback gets (_hold_context), and as long as any function that was a value
193 # of that hash: each of those is a wrapper that holds the Context, so a macro
194 # taken out of the hash works for as long as the caller keeps it. The same
195 # function exported under two names gets one wrapper.
196 #
197 # What that does not reach: a function inside an exported array or object,
198 # taken out and called after the hash and every function that was a value of
199 # it are gone.
200 17 ▶sub get_exported {
Subroutine get_exported: called CC=3 SCAR=11.0
Pod documented
201 17 my ( $self, @args ) = @_;
202  
203 # the callback _get_exported is going to call: the second argument if that
204 # is a function, else the first if that is one, else the third
205 17 my ($at) = grep { $self->_lib->is_function( $args[$_] ) } 1, 0;
206 17 ▶ $at //= 2;
Condition100%2/2
$at //= 2
l!l
152
MC/DC100%1/1
$at //= 2
$at
Truth table100%2/2
A
A= $at
Aresult
11
00
207  
208 17 ▶ if ( $self->_lib->is_function( $args[$at] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($args[$at])) 16 1 17
209 16 my $cb = $args[$at];
210 16 ▶ $args[$at] = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
211 # an error goes on with the one or two arguments upstream passes: one
212 # for a compile error, two for a render error, which comes with the
213 # Context as a third one that is for a compiled `import` only
214 16 ▶ return $cb->(@_) if @_ < 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ < 3 1 15 16
215 15 my ( $err, $exported, $context ) = @_;
216 15 ▶ return $cb->( $err, $exported ) if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 2 13 15
217 13 return $cb->( $err, $self->_hold_context( $exported, $context ) );
218 16 };
219 }
220  
221 17 return $self->_get_exported(@args);
222 }
223  
224 # The Context of each hash get_exported handed out, by that hash: the entry
225 # goes when the hash is freed (Hash::Util::FieldHash), and the Context with it
226 # unless a wrapper still holds it. It is not an attribute of anything because
227 # nothing of ours outlives the hash for sure -- not this Template, which may
228 # be a temporary, and not the Environment -- and it cannot be an entry of the
229 # hash itself, which is the exported names and nothing else.
230 15 fieldhash my %context_of;
231  
232 13 ▶sub _hold_context {
Subroutine _hold_context: called CC=5 SCAR=16.2
233 13 my ( $self, $exported, $context ) = @_;
234 13 ▶ return $exported unless $context;
Branch: 1 branch
BranchTrueFalseTotal
unless $context 0 13 13
235  
236 13 $context_of{$exported} = $context;
237  
238 13 my %holder;
239 13 for my $name ( keys %$exported ) {
240 51 my $func = $exported->{$name};
241 51 ▶ next unless $self->_lib->is_function($func);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_lib->is_function($func) 33 18 51
242 13 ▶ $exported->{$name} = $holder{ refaddr($func) } //= sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
243 13 my $held = $context; # naming it is what makes this sub hold it
244 13 return $func->(@_);
245 18 ▶ };
Condition66%2/3
$holder{builtin::refaddr($func)} //= sub { my $held = $context; return &$func(@_); }
l!l&&r!l&&!r
1170
MC/DC0%0/2
$holder{builtin::refaddr($func)} //= sub { my $held = $context; return &$func(@_); }
$holder{builtin::refaddr($func)} sub { my $held = $context; return &$func(@_); }
Truth table66%2/3
A //= B
A= $holder{builtin::refaddr($func)}B= sub { my $held = $context; return &$func(@_); }
ABresult
1X1
011
000
246 }
247 13 return $exported;
248 }
249  
250 # upstream getExported. Port-only: the callback gets the Context the template
251 # ran in as a third argument, for the caller to keep alive (ADR 0021) -- with
252 # a render error too, because a template that failed may have handed a macro
253 # out before it did (Compiler _keep_alive_statement).
254 1586 ▶sub _get_exported {
Subroutine _get_exported: called CC=7 SCAR=19.6
255 1586 my ( $self, $ctx, $parent_frame, $cb ) = @_;
256 1586 ▶ if ( $self->_lib->is_function($ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($ctx)) 1107 479 1586
257 1107 $cb = $ctx;
258 1107 $ctx = {};
259 }
260  
261 1586 ▶ if ( $self->_lib->is_function($parent_frame) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($parent_frame)) 14 1572 1586
262 14 $cb = $parent_frame;
263 14 $parent_frame = undef;
264 }
265  
266 # Catch compile errors for async rendering.
267 1586 my $ok = eval { $self->compile; 1 };
268 1586 ▶ if ( !$ok ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ok) 1 1585 1586
269 1 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 1 0 1
270 1 return $cb->($@);
271 }
272 else {
273 0 die $@;
274 }
275 }
276  
277 1585 ▶ my $frame = $parent_frame ? $parent_frame->push : $self->_runtime_class->Frame->new;
Branch: 1 branch
BranchTrueFalseTotal
$parent_frame ? : 465 1120 1585
278 1585 $frame->top_level(1);
279  
280 # Run the rootRenderFunc to populate the context with exported vars.
281 1585 ▶ my $context = $self->_context_class->new( $ctx || {}, $self->blocks, $self->env );
Condition50%1/2
$ctx || {}
l!l
15850
MC/DC0%0/1
$ctx || {}
$ctx
Truth table50%1/2
A
A= $ctx
Aresult
11
00
282  
283 # Port-only (ADR 0021). The compiled code does not hold its Context
284 # (Runtime _render_scope), and neither may this callback: a macro of the
285 # template can hold the callback. The Context is alive when the template is
286 # done -- this call or a continuation holds it -- and whoever takes the
287 # exports keeps it from then on.
288 1585 weaken( my $exporting = $context );
289  
290 # N sequential imports of a cached template: N calls deep, as in render (k83)
291 15 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
292 $self->_root_render_func->(
293 $self->env, $context, $frame, $self->_runtime_class,
294 1204 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
295 1204 my ($err) = @_;
296 1204 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) { } 41 1163 1204
297 41 $cb->( $err, undef, $exporting );
298 }
299 else {
300 1163 my $held = $exporting;
301 1163 ▶ my $context = $held // $self->_gone_class->instance;
Condition33%1/3
$held // $self->_gone_class->instance
l!l&&r!l&&!r
116300
MC/DC0%0/2
$held // $self->_gone_class->instance
$held $self->_gone_class->instance
Truth table33%1/3
A // B
A= $heldB= $self->_gone_class->instance
ABresult
1X1
011
000
302 1163 $cb->( undef, $context->get_exported, $held );
303 }
304 }
305 1585 );
306 }
307  
308 12512 ▶sub compile {
Subroutine compile: called CC=2 SCAR=6.9
Pod documented
309 12512 my ($self) = @_;
310 12512 ▶ if ( !$self->compiled ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->compiled) 8747 3765 12512
311 8747 $self->_compile;
312 }
313 }
314  
315 9290 ▶sub _compile {
Subroutine _compile: called CC=2 SCAR=6.9
316 9290 my ($self) = @_;
317 9290 my $props;
318  
319 9290 ▶ if ( $self->tmpl_props ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tmpl_props) { } 11 9279 9290
320 11 $props = $self->tmpl_props;
321 }
322 else {
323 9279 my $source = $self->_compiler_class->compile(
324 $self->tmpl_str,
325 $self->env->async_filters,
326 $self->env->extensions_list,
327 $self->path,
328 $self->env->opts
329 );
330  
331 9263 $props = $self->_eval_source($source); # new Function(source)(); returns props
332 }
333  
334 9274 $self->blocks( $self->_get_blocks($props) );
335 9274 $self->_root_render_func( $props->{root} );
336 9274 $self->compiled(1);
337 }
338  
339 # new Function(source) -- eval the emitted Perl source, which ends in a
340 # +{ root => \&, b_<name> => \&, ... } hashref. Compiled template bodies are
341 # sloppy-mode in JS (new Function is non-strict); eval'ing without strict/
342 # warnings matches that and avoids spurious undef-concat warnings from the
343 # generated code (values flow through suppress_value/_js_to_string).
344 9263 ▶sub _eval_source {
Subroutine _eval_source: called CC=2 SCAR=6.9
345 9263 my ( $self, $source ) = @_;
346 9263 my $props = do {
347 15 ▶ no strict; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
348 15 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
349 9263 my $p = eval $source; ## no critic (ProhibitStringyEval)
350 9263 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 9263 9263
351 9263 $p;
352 };
353 9263 return $props;
354 }
355  
356 9274 ▶sub _get_blocks {
Subroutine _get_blocks: called CC=3 SCAR=11.0
357 9274 my ( $self, $props ) = @_;
358 9274 my $blocks = {};
359  
360 9274 for my $k ( @{ $self->_lib->keys($props) } ) {
361 10445 ▶ if ( substr( $k, 0, 2 ) eq 'b_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($k, 0, 2) eq "b_") 1171 9274 10445
362 1171 $blocks->{ substr( $k, 2 ) } = $props->{$k};
363 }
364 }
365  
366 9274 return $blocks;
367 }
368  
369 15 1;
370  
371 __END__
« blib/lib/Text/Nunjucks/Nodes/CallExtension.pm ↑ Summary blib/lib/Text/Nunjucks/Environment.pm »