blib/lib/Text/Nunjucks/Environment.pm

filter: Statementstmt 94.1% 258 / 274 Branchbran 81.1% 73 / 90 Conditioncond 65.7% 25 / 38 MC/DCmcdc 45.4% 10 / 22 Subroutinesub 96.6% 57 / 59 Podpod 62.5% 15 / 24 totaltotal 86.3% 438 / 507 CC 128 SCAR 18.2
CC
128
Cov
89%
get_template
16.7
get_template
16.7
init
12.0
CRAP 6.2
« blib/lib/Text/Nunjucks/Template.pm ↑ Summary blib/lib/Text/Nunjucks/Filters.pm »
1 package Text::Nunjucks::Environment;
2 # ABSTRACT: 1:1 port of Nunjucks src/environment.js -- the template Environment
3  
4 15 our $VERSION = '0.001';
5  
6 15 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use builtin qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use feature qw( current_sub );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Scalar::Util qw( blessed weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use Text::Nunjucks::Compiler;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 15 ▶use Text::Nunjucks::Loader;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 15 ▶use Text::Nunjucks::Loader::FileSystem;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 15 ▶use Text::Nunjucks::Filters;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 15 ▶use Text::Nunjucks::Tests;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 15 ▶use Text::Nunjucks::Globals;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 15 ▶use Text::Nunjucks::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 15 ▶use Text::Nunjucks::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 15 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 15 with 'Text::Nunjucks::EmitterObj';
26  
27 # environment.js exports { Environment, Template } and defines Context internally;
28 # the porting map lists Text::Nunjucks::Environment / ::Context / ::Template, all
29 # from this one upstream file. Each is a class in its own file (ADR 0020):
30 # Environment here, Text::Nunjucks::Context and Text::Nunjucks::Template beside
31 # it, both `use`d above. object.js's Obj/EmitterObj become Moo +
32 # the EmitterObj role (ADR 0002); the constructor's `this.init(...args)`
33 # indirection becomes a positional-args BUILDARGS feeding a faithful init() (so
34 # the files can still be reviewed method-for-method against environment.js).
35 #
36 # The compiler-emitted code (k15) calls into $env / $context / a Template through
37 # exactly the method surface built here; $runtime is passed as the class name
38 # 'Text::Nunjucks::Runtime' (class-method-only module), so $runtime->None,
39 # $runtime->Frame->new, $runtime->member_lookup(...) all resolve.
40 #
41 # PORTING-ORDER NOTES -- how init's dependencies arrived:
42 # * environment.js require()s ./filters, ./tests, ./globals and registers them
43 # all in init. All three are plain loads (`use` above): the transitional
44 # guarded require of ADR 0015 is gone since k18. Filters exports($env)
45 # builds the filter bag per Environment, Tests exports the test bag, and
46 # Globals globals() returns a fresh globals bag per Environment.
47 # * init's no-loaders default is upstream's server-side one, a
48 # FileSystemLoader('views') (Text::Nunjucks::Loader::FileSystem, k19).
49 # The WebLoader fallback and window.nunjucksPrecompiled are browser-only
50 # and out of scope (ADR 0018).
51  
52 #### callbackAsap -- upstream defers the callback via asap; Perl core has no event
53 #### loop, so the callback fires synchronously (ADR 0003). Kept as a named seam so
54 #### an event-loop binding can later make it truly async without touching callers.
55 4378 ▶sub _callback_asap {
Subroutine _callback_asap: called CC=1 SCAR=1.2
56 0 ▶ my ( $self, $cb, $err, $res ) = @_;
Subroutine _callback_asap: not called CC=1 SCAR=1.2
57 0 $cb->( $err, $res );
58 0 return;
59 }
60  
61 #### noopTmplSrc -- a no-op template for {% include ignore missing %}
62 my $NOOP_TMPL_SRC = {
63 type => 'code',
64 obj => {
65 10 ▶ root => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
66 10 my ( $env, $context, $frame, $runtime, $cb ) = @_;
67 10 my $ok = eval { $cb->( undef, '' ); 1 };
68 0 ▶ if ( !$ok ) { $cb->( $runtime->handle_error( $@, undef, undef ) ); }
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) 0 10 10
69 }
70 }
71 15 };
72  
73 # init's field set (upstream sets these on `this`). All rw: init assigns them.
74 15 has opts => ( is => 'rw' );
75 13 has loaders => ( is => 'rw' );
76 13 has globals => ( is => 'rw' );
77 13 has filters => ( is => 'rw' );
78 13 has tests => ( is => 'rw' );
79 13 has async_filters => ( is => 'rw' );
80 13 has extensions => ( is => 'rw' );
81 13 has extensions_list => ( is => 'rw' );
82 13 has _init_args => ( is => 'ro' );
83  
84 # Collaborators named once, reached through the invocant (house rule).
85 9274 ▶sub template_class { 'Text::Nunjucks::Template' }
Subroutine template_class: called CC=1 SCAR=0.0
Pod undocumented
86 48447 ▶sub _lib { 'Text::Nunjucks::Lib' }
Subroutine _lib: called CC=1 SCAR=0.0
87 241643 ▶sub _runtime_class { 'Text::Nunjucks::Runtime' }
Subroutine _runtime_class: called CC=1 SCAR=0.0
88 165 ▶sub _error_class { 'Text::Nunjucks::Lib::Error' }
Subroutine _error_class: called CC=1 SCAR=0.0
89 5120 ▶sub filters_module { 'Text::Nunjucks::Filters' }
Subroutine filters_module: called CC=1 SCAR=0.0
Pod undocumented
90 5120 ▶sub tests_module { 'Text::Nunjucks::Tests' }
Subroutine tests_module: called CC=1 SCAR=0.0
Pod undocumented
91 5120 ▶sub globals_module { 'Text::Nunjucks::Globals' }
Subroutine globals_module: called CC=1 SCAR=0.0
Pod undocumented
92 40 ▶sub file_system_loader_class { 'Text::Nunjucks::Loader::FileSystem' }
Subroutine file_system_loader_class: called CC=1 SCAR=0.0
Pod undocumented
93  
94 # constructor(...args) { this.init(...args) } -- positional (loaders, opts).
95 5122 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=0.0
Pod undocumented
96 5122 my ( $class, @args ) = @_;
97 5122 return { _init_args => [@args] };
98 }
99  
100 5122 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.0
Pod undocumented
101 5122 my ($self) = @_;
102 5122 ▶ $self->init( @{ $self->_init_args // [] } );
Condition50%1/2
$self->_init_args // []
l!l
51220
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
Truth table50%1/2
A
A= $self->_init_args
Aresult
11
00
103 5120 delete $self->{_init_args};
104 }
105  
106 5122 ▶sub init {
Subroutine init: called CC=12 SCAR=24.9
Pod undocumented
107 5122 my ( $self, $loaders, $opts ) = @_;
108  
109 # dev flag: the trace shown on errors (default false).
110 5122 ▶ $opts = $self->opts( $opts || {} );
Condition100%2/2
$opts || {}
l!l
9414181
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
111 5122 ▶ $self->opts->{dev} = $self->_runtime_class->_truthy( $opts->{dev} ) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->_runtime_class->_truthy($opts->{'dev'}) ? : 0 5122 5122
112  
113 # autoescape: global autoescaping, default true. `!= null` keeps an explicit
114 # false, defaults only for undefined/null.
115 $self->opts->{autoescape} =
116 ( defined( $opts->{autoescape} ) && !$self->_runtime_class->_is_none( $opts->{autoescape} ) )
117 ? $opts->{autoescape}
118 5122 ▶ : true;
Branch: 1 branch
BranchTrueFalseTotal
defined $opts->{'autoescape'} && !$self->_runtime_class->_is_none($opts->{'autoescape'}) ? : 203 4919 5122
Condition66%2/3
defined $opts->{"autoescape"} and not $self->_runtime_class->_is_none($opts->{"autoescape"})
!ll&&!rl&&r
49190203
MC/DC50%1/2
defined $opts->{"autoescape"} and not $self->_runtime_class->_is_none($opts->{"autoescape"})
defined $opts->{"autoescape"} not $self->_runtime_class->_is_none($opts->{"autoescape"})
Truth table66%2/3
A and not(B)
A= defined $opts->{"autoescape"}B= not $self->_runtime_class->_is_none($opts->{"autoescape"})
ABresult
0X0
100
111
119  
120 5122 ▶ $self->opts->{throwOnUndefined} = $self->_runtime_class->_truthy( $opts->{throwOnUndefined} ) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->_runtime_class->_truthy($opts->{'throwOnUndefined'}) ? : 15 5107 5122
121 5122 ▶ $self->opts->{trimBlocks} = $self->_runtime_class->_truthy( $opts->{trimBlocks} ) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->_runtime_class->_truthy($opts->{'trimBlocks'}) ? : 2 5120 5122
122 5122 ▶ $self->opts->{lstripBlocks} = $self->_runtime_class->_truthy( $opts->{lstripBlocks} ) ? true : false;
Branch: 1 branch
BranchTrueFalseTotal
$self->_runtime_class->_truthy($opts->{'lstripBlocks'}) ? : 0 5122 5122
123  
124 5122 $self->loaders( [] );
125  
126 5122 ▶ if ( !$loaders ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $loaders) { } 40 5082 5122
127 # The filesystem loader is only available server-side -- which the port
128 # always is: upstream's `if (FileSystemLoader) ... else if (WebLoader)`
129 # always takes its first branch under node (ADR 0018).
130 40 $self->loaders( [ $self->file_system_loader_class->new('views') ] );
131 }
132 else {
133 5082 ▶ $self->loaders( $self->_lib->is_array($loaders) ? $loaders : [$loaders] );
Branch: 1 branch
BranchTrueFalseTotal
$self->_lib->is_array($loaders) ? : 4867 215 5082
134 }
135  
136 # window.nunjucksPrecompiled -- browser-only, out of scope.
137  
138 5122 $self->_init_loaders;
139  
140 5120 $self->globals( $self->_load_globals );
141 5120 $self->filters( {} );
142 5120 $self->tests( {} );
143 5120 $self->async_filters( [] );
144 5120 $self->extensions( {} );
145 5120 $self->extensions_list( [] );
146  
147 5120 for my $pair ( @{ $self->_lib->_entries( $self->_load_filters ) } ) {
148 220160 $self->add_filter(@$pair);
149 }
150 5120 for my $pair ( @{ $self->_lib->_entries( $self->_load_tests ) } ) {
151 133120 $self->add_test(@$pair);
152 }
153 }
154  
155 # The built-in bags (see PORTING-ORDER NOTES): filters.js's exports, built for
156 # this Environment; tests.js's exports; globals.js's globals(), a fresh bag
157 # per call.
158 5120 ▶sub _load_filters { $_[0]->filters_module->exports( $_[0] ) }
Subroutine _load_filters: called CC=1 SCAR=0.0
159 5120 ▶sub _load_tests { $_[0]->tests_module->exports }
Subroutine _load_tests: called CC=1 SCAR=0.0
160 5120 ▶sub _load_globals { $_[0]->globals_module->globals }
Subroutine _load_globals: called CC=1 SCAR=0.0
161  
162 5122 ▶sub _init_loaders {
Subroutine _init_loaders: called CC=3 SCAR=11.1
163 5122 my ($self) = @_;
164 5122 for my $loader ( @{ $self->loaders } ) {
165 # Caching and cache busting.
166 5121 $loader->cache( {} );
167 5119 ▶ if ( $loader->can('on') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loader->can("on")) 5119 0 5119
168 # The listeners hold the Environment and the loader weakly, as the
169 # filters hold their Environment: Environment -> loaders -> loader ->
170 # listeners -> Environment / loader would be reference cycles Perl
171 # never frees (JS collects them). A listener runs while its loader
172 # emits, so the loader is there. An Environment dropped by its caller
173 # is freed even while a shared loader lives on, and its own listeners
174 # with it, where node's loader keeps it and them alive -- a difference
175 # the Perl API can see, the worker's interim choice until k96 settles
176 # the cycles (ADR 0018).
177 5119 weaken( my $env = $self );
178 5119 weaken( my $l = $loader );
179 $loader->on(
180 'update',
181 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
182 1 my ( $name, $fullname ) = @_;
183 1 $l->cache->{$name} = undef;
184 1 ▶ $env->emit( 'update', $name, $fullname, $l ) if $env;
Branch: 1 branch
BranchTrueFalseTotal
if $env 1 0 1
185 }
186 5119 );
187 $loader->on(
188 'load',
189 120 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
190 120 my ( $name, $source ) = @_;
191 120 ▶ $env->emit( 'load', $name, $source, $l ) if $env;
Branch: 1 branch
BranchTrueFalseTotal
if $env 120 0 120
192 }
193 5119 );
194 }
195 }
196 }
197  
198 0 ▶sub invalidate_cache {
Subroutine invalidate_cache: not called CC=2 SCAR=17.9
Pod documented
199 0 my ($self) = @_;
200 0 for my $loader ( @{ $self->loaders } ) {
201 0 $loader->cache( {} );
202 }
203 }
204  
205 14 ▶sub add_extension {
Subroutine add_extension: called CC=1 SCAR=0.0
Pod documented
206 14 my ( $self, $name, $extension ) = @_;
207 14 $extension->{__name} = $name; # extension.__name = name (k8/k22: object surface open)
208 14 $self->extensions->{$name} = $extension;
209 14 push @{ $self->extensions_list }, $extension;
210 14 return $self;
211 }
212  
213 1 ▶sub remove_extension {
Subroutine remove_extension: called CC=2 SCAR=7.0
Pod documented
214 1 my ( $self, $name ) = @_;
215 1 my $extension = $self->get_extension($name);
216 1 ▶ return unless $extension;
Branch: 1 branch
BranchTrueFalseTotal
unless $extension 0 1 1
217  
218 1 $self->extensions_list( $self->_lib->without( $self->extensions_list, $extension ) );
219 1 delete $self->extensions->{$name};
220 }
221  
222 16 ▶sub get_extension {
Subroutine get_extension: called CC=1 SCAR=0.0
Pod documented
223 16 my ( $self, $name ) = @_;
224 16 return $self->extensions->{$name};
225 }
226  
227 3 ▶sub has_extension {
Subroutine has_extension: called CC=2 SCAR=6.9
Pod documented
228 3 my ( $self, $name ) = @_;
229 3 ▶ return $self->extensions->{$name} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->extensions->{$name} ? : 1 2 3
230 }
231  
232 600 ▶sub add_global {
Subroutine add_global: called CC=1 SCAR=0.0
Pod documented
233 600 my ( $self, $name, $value ) = @_;
234 600 $self->globals->{$name} = $value;
235 600 return $self;
236 }
237  
238 7 ▶sub get_global {
Subroutine get_global: called CC=2 SCAR=6.9
Pod documented
239 7 my ( $self, $name ) = @_;
240 7 ▶ if ( !defined $self->globals->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->globals->{$name}) 3 4 7
241 3 die $self->_error_class->new( 'global not found: ' . $name );
242 }
243 4 return $self->globals->{$name};
244 }
245  
246 220479 ▶sub add_filter {
Subroutine add_filter: called CC=2 SCAR=6.9
Pod documented
247 220479 my ( $self, $name, $func, $async ) = @_;
248 220479 my $wrapped = $func;
249 220479 ▶ if ( $self->_runtime_class->_truthy($async) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_runtime_class->_truthy($async)) 317 220162 220479
250 317 push @{ $self->async_filters }, $name;
251 }
252 220479 $self->filters->{$name} = $wrapped;
253 220479 return $self;
254 }
255  
256 10971 ▶sub get_filter {
Subroutine get_filter: called CC=2 SCAR=6.9
Pod documented
257 10971 my ( $self, $name ) = @_;
258 10971 ▶ if ( !$self->filters->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->filters->{$name}) 1 10970 10971
259 1 die $self->_error_class->new( 'filter not found: ' . $name );
260 }
261 10970 return $self->filters->{$name};
262 }
263  
264 133121 ▶sub add_test {
Subroutine add_test: called CC=1 SCAR=0.0
Pod documented
265 133121 my ( $self, $name, $func ) = @_;
266 133121 $self->tests->{$name} = $func;
267 133121 return $self;
268 }
269  
270 473 ▶sub get_test {
Subroutine get_test: called CC=2 SCAR=6.9
Pod documented
271 473 my ( $self, $name ) = @_;
272 # this.tests[name] and 'test not found: ' + name read the name's JS
273 # ToString -- the select / reject filters hand it any template value
274 # (oracle: select(null) finds the null test, select(testName='odd') gives
275 # "test not found: [object Object]")
276 473 my $key = $self->_runtime_class->_member_key($name);
277 473 ▶ if ( !$self->tests->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->tests->{$key}) 24 449 473
278 24 die $self->_error_class->new( 'test not found: ' . $key );
279 }
280 449 return $self->tests->{$key};
281 }
282  
283 12335 ▶sub resolve_template {
Subroutine resolve_template: called CC=5 SCAR=16.3
Pod documented
284 12335 my ( $self, $loader, $parent_name, $filename ) = @_;
285 12335 ▶ my $is_relative =
Branch: 1 branch
BranchTrueFalseTotal
$loader->can('is_relative') && $parent_name ? : 10928 1407 12335
Condition66%2/3
$loader->can("is_relative") and $parent_name
!ll&&!rl&&r
0140710928
MC/DC50%1/2
$loader->can("is_relative") and $parent_name
$loader->can("is_relative") $parent_name
Truth table66%2/3
A and B
A= $loader->can("is_relative")B= $parent_name
ABresult
0X0
100
111
286 ( $loader->can('is_relative') && $parent_name ) ? $loader->is_relative($filename) : 0;
287 12335 ▶ return ( $is_relative && $loader->can('resolve') )
Branch: 1 branch
BranchTrueFalseTotal
$is_relative && $loader->can('resolve') ? : 101 12234 12335
Condition66%2/3
$is_relative and $loader->can("resolve")
!ll&&!rl&&r
122340101
MC/DC50%1/2
$is_relative and $loader->can("resolve")
$is_relative $loader->can("resolve")
Truth table66%2/3
A and B
A= $is_relativeB= $loader->can("resolve")
ABresult
0X0
100
111
288 ? $loader->resolve( $parent_name, $filename )
289 : $filename;
290 }
291  
292 8049 ▶sub get_template {
Subroutine get_template: called CC=15 SCAR=28.2
Pod documented
293 8049 my ( $self, $name, $eager_compile, $parent_name, $ignore_missing, $cb ) = @_;
294 8049 my $that = $self;
295 8049 my $tmpl = undef;
296 8049 ▶ if ( $name && ref($name) eq 'HASH' && $name->{raw} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and ref $name eq "HASH" and $name->{"raw"}) 0 8049 8049
Condition50%3/6
$name and ref $name eq "HASH" and $name->{"raw"}
!ll&&!rl&&r
804900
$name and ref $name eq "HASH"
!ll&&!rl&&r
180480
MC/DC0%0/3
$name and ref $name eq "HASH" and $name->{"raw"}
$name ref $name eq "HASH" $name->{"raw"}
Truth table50%2/4
A and B and C
A= $nameB= ref $name eq "HASH"C= $name->{"raw"}
ABCresult
0XX0
10X0
1100
1111
297 # this fixes autoescape for templates referenced in symbols
298 0 $name = $name->{raw};
299 }
300  
301 8049 ▶ if ( $self->_lib->is_function($parent_name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($parent_name)) 0 8049 8049
302 0 $cb = $parent_name;
303 0 $parent_name = undef;
304 0 ▶ $eager_compile = $eager_compile || 0;
Condition0%0/2
$eager_compile || 0
l!l
00
MC/DC0%0/1
$eager_compile || 0
$eager_compile
Truth table0%0/2
A
A= $eager_compile
Aresult
11
00
305 }
306  
307 8049 ▶ if ( $self->_lib->is_function($eager_compile) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($eager_compile)) 327 7722 8049
308 327 $cb = $eager_compile;
309 327 $eager_compile = 0;
310 }
311  
312 8049 ▶ if ( blessed($name) && $name->isa( $self->template_class ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($name) and $name->isa($self->template_class)) { } 5 8044 8049
elsif (not $self->_lib->is_string($name)) { } 0 8044 8044
Condition66%2/3
builtin::blessed($name) and $name->isa($self->template_class)
!ll&&!rl&&r
804405
MC/DC50%1/2
builtin::blessed($name) and $name->isa($self->template_class)
builtin::blessed($name) $name->isa($self->template_class)
Truth table66%2/3
A and B
A= builtin::blessed($name)B= $name->isa($self->template_class)
ABresult
0X0
100
111
313 5 $tmpl = $name;
314 }
315 elsif ( !$self->_lib->is_string($name) ) {
316 0 die $self->_error_class->new( 'template names must be a string: ' . $name );
317 }
318 else {
319 8044 for my $loader ( @{ $self->loaders } ) {
320 8044 $tmpl = $loader->cache->{ $self->resolve_template( $loader, $parent_name, $name ) };
321 8044 ▶ last if $tmpl;
Branch: 1 branch
BranchTrueFalseTotal
if $tmpl 3753 4291 8044
322 }
323 }
324  
325 8049 ▶ if ($tmpl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tmpl) 3758 4291 8049
326 3758 ▶ $tmpl->compile if $eager_compile;
Branch: 1 branch
BranchTrueFalseTotal
if $eager_compile 509 3249 3758
327  
328 3758 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 3756 2 3758
329 3756 $cb->( undef, $tmpl );
330 3756 return undef;
331 }
332 else {
333 2 return $tmpl;
334 }
335 }
336 4291 my $sync_result;
337  
338 2117 ▶ my $create_template = sub {
Subroutine __ANON__: called CC=9 SCAR=22.0
339 2117 my ( $err, $info ) = @_;
340 2117 ▶ if ( !$info && !$err && !$ignore_missing ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info or $err or $ignore_missing) 97 2020 2117
Condition83%5/6
$info or $err or $ignore_missing
l!l&&r!l&&!r
2011997
$info or $err
l!l&&r!l&&!r
20110106
MC/DC66%2/3
$info or $err or $ignore_missing
$info $err $ignore_missing
Truth table75%3/4
A or B or C
A= $infoB= $errC= $ignore_missing
ABCresult
1XX1
01X1
0011
0000
341 97 $err = $self->_error_class->new( 'template not found: ' . $name );
342 }
343  
344 2117 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 97 2020 2117
345 97 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 97 0 97
346 97 $cb->($err);
347 89 return;
348 }
349 else {
350 0 die $err;
351 }
352 }
353 2020 my $new_tmpl;
354 2020 ▶ if ( !$info ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $info) { } 9 2011 2020
355 9 $new_tmpl = $self->template_class->new( $NOOP_TMPL_SRC, $self, '', $eager_compile );
356 }
357 else {
358 $new_tmpl =
359 2011 $self->template_class->new( $info->{src}, $self, $info->{path}, $eager_compile );
360 2011 ▶ if ( !$info->{noCache} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($info->{"noCache"}) 76 1935 2011
361 76 $info->{loader}->cache->{$name} = $new_tmpl;
362 }
363 }
364 2020 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 2014 6 2020
365 2014 $cb->( undef, $new_tmpl );
366 }
367 else {
368 6 $sync_result = $new_tmpl;
369 }
370 4291 };
371  
372 # Port-only (k83): an include, import or extends that loads its template
373 # runs the rest of the including template in this call's continuation, so N
374 # sequential (or N nested) loads enter async_iter N deep -- one JS call per
375 # load upstream as well (ADR 0003). Perl warns at the 100th nested call of a
376 # sub, at the call site; the depth is bounded by memory, not by Perl, so that
377 # warning is off from here to the end of get_template (see Lib async_iter).
378 15 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
379 $self->_lib->async_iter(
380 $self->loaders,
381 2117 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
382 2117 my ( $loader, $i, $next, $done ) = @_;
383 2117 ▶ my $handle = sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
384 2117 my ( $err, $src ) = @_;
385 2117 ▶ if ($err) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err) { } 0 2117 2117
elsif ($src) { } 2011 106 2117
386 0 $done->($err);
387 }
388 elsif ($src) {
389 2011 $src->{loader} = $loader;
390 2011 $done->( undef, $src );
391 }
392 else {
393 106 $next->();
394 }
395 2117 };
396  
397 # Resolve name relative to parentName
398 2117 $name = $that->resolve_template( $loader, $parent_name, $name );
399  
400 2117 ▶ if ( $loader->can('async') && $loader->async ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($loader->can('async') and $loader->async) { } 0 2117 2117
Condition33%1/3
$loader->can("async") and $loader->async
!ll&&!rl&&r
021170
MC/DC0%0/2
$loader->can("async") and $loader->async
$loader->can("async") $loader->async
Truth table33%1/3
A and B
A= $loader->can("async")B= $loader->async
ABresult
0X0
100
111
401 0 $loader->get_source( $name, $handle );
402 }
403 else {
404 2117 $handle->( undef, $loader->get_source($name) );
405 }
406 },
407 4291 $create_template
408 );
409  
410 4267 return $sync_result;
411 }
412  
413 # express(app) -- express-app.js is out of scope (not ported).
414  
415 319 ▶sub render {
Subroutine render: called CC=2 SCAR=6.9
Pod documented
416 319 my ( $self, $name, $ctx, $cb ) = @_;
417 319 ▶ if ( $self->_lib->is_function($ctx) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($ctx)) 1 318 319
418 1 $cb = $ctx;
419 1 $ctx = undef;
420 }
421  
422 # Synchronous API: if nothing defers, the whole thing runs synchronously.
423 319 my $sync_result = undef;
424  
425 $self->get_template(
426 $name,
427 317 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
428 317 my ( $err, $tmpl ) = @_;
429 317 ▶ if ( $err && $cb ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($err and $cb) { } 3 314 317
elsif ($err) { } 4 310 314
Condition100%3/3
$err and $cb
!ll&&!rl&&r
31043
MC/DC100%2/2
$err and $cb
$err $cb
Truth table100%3/3
A and B
A= $errB= $cb
ABresult
0X0
100
111
430 3 $self->_callback_asap( $cb, $err );
431 }
432 elsif ($err) {
433 4 die $err;
434 }
435 else {
436 310 $sync_result = $tmpl->render( $ctx, $cb );
437 }
438 }
439 319 );
440  
441 313 return $sync_result;
442 }
443  
444 4372 ▶sub render_string {
Subroutine render_string: called CC=3 SCAR=11.0
Pod documented
445 4372 my ( $self, $src, $ctx, $opts, $cb ) = @_;
446 4372 ▶ if ( $self->_lib->is_function($opts) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_lib->is_function($opts)) 4077 295 4372
447 4077 $cb = $opts;
448 4077 $opts = {};
449 }
450 4372 ▶ $opts = $opts || {};
Condition100%2/2
$opts || {}
l!l
4078294
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
451  
452 4372 my $tmpl = $self->template_class->new( $src, $self, $opts->{path} );
453 4372 return $tmpl->render( $ctx, $cb );
454 }
455  
456 5083 ▶sub waterfall {
Subroutine waterfall: called CC=1 SCAR=0.1
Pod undocumented
457 5083 my ( $self, $tasks, $callback, $force_async ) = @_;
458 # Minimal a-sync-waterfall: run tasks in sequence, threading each task's
459 # results into the next, first error short-circuits to the final callback.
460 # Synchronous unless a task defers (ADR 0003); forceAsync is a no-op here.
461 # Only compile_include emits waterfall -- no corpus case exercises it yet
462 # (k18/k20 include/extends suites will), so the deep-recursion trampoline of
463 # the real a-sync-waterfall is deferred.
464 5083 my @tasks = @$tasks;
465 5083 my $i = 0;
466  
467 # Port-only (k107): the step hands itself on as __SUB__, never as a captured
468 # $next -- a closure that closes over the variable holding it is a reference
469 # cycle, and Perl frees by reference count: tasks and callback, and through
470 # them the render's Context, Frame and Template, would live forever. $next
471 # only starts the chain.
472 14452 ▶ my $next = sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
473 14454 my ( $err, @args ) = @_;
474 14454 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 14452 14452
475 2 ▶ $callback->($err) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
476 2 return;
477 }
478 14454 ▶ if ( $i >= scalar @tasks ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= scalar @tasks) 3548 10904 14452
479 3550 ▶ $callback->( undef, @args ) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 3548 0 3548
480 3548 return;
481 }
482 10906 my $task = $tasks[ $i++ ];
483 10906 $task->( @args, __SUB__ );
484 5083 };
485 5083 $next->(undef);
486 5071 return;
487 }
488  
489 13 1;
490  
491 __END__
« blib/lib/Text/Nunjucks/Template.pm ↑ Summary blib/lib/Text/Nunjucks/Filters.pm »