blib/lib/Text/Nunjucks/Context.pm

filter: Statementstmt 58.4% 139 / 238 Branchbran 38.6% 17 / 44 Conditioncond 30.0% 9 / 30 MC/DCmcdc 16.6% 3 / 18 Subroutinesub 61.4% 35 / 57 Podpod 0.0% 0 / 24 totaltotal 49.3% 203 / 411 CC 43 SCAR 11.7
CC
43
Cov
53%
get_super
7.7
get_super
7.7
DEMOLISH
6.4
CRAP 3.2
« blib/lib/Text/Nunjucks/Globals.pm ↑ Summary blib/lib/Text/Nunjucks/Tests.pm »
1 package Text::Nunjucks::Context;
2 # ABSTRACT: The per-render bookkeeping of variables and blocks (environment.js class Context)
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 Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Nunjucks::Environment;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use Text::Nunjucks::Runtime::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use Text::Nunjucks::Context::Gone;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # upstream: class Context extends Obj in src/environment.js, which defines it
16 # internally (environment.js exports only Environment and Template). Like
17 # Environment and Template, the constructor's `this.init(...args)` indirection
18 # is a positional-args BUILDARGS feeding a faithful init().
19  
20 15 has env => ( is => 'rw' );
21 15 has ctx => ( is => 'rw' );
22 15 has blocks => ( is => 'rw' );
23 15 has exported => ( is => 'rw' );
24 15 has _init_args => ( is => 'ro' );
25  
26 # Port-only, no upstream counterpart (ADR 0021): what a tracing GC does for
27 # upstream and a reference count has to be told. See _keep_alive, the three
28 # methods for an included template, _track_scope and DEMOLISH at the end of
29 # this file.
30 11977 ▶has _kept => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
31 15 has _includer => ( is => 'rw' );
32 15 has _has_closure => ( is => 'rw' );
33 15 has _has_delivered => ( is => 'rw' );
34 11977 ▶has _context_vars => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
35 11977 ▶has _frame_vars => ( is => 'ro', default => sub { {} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
36  
37 24460 ▶sub _lib { 'Text::Nunjucks::Lib' }
Subroutine _lib: called CC=1 SCAR=0.0
38 0 ▶sub _environment_class { 'Text::Nunjucks::Environment' }
Subroutine _environment_class: not called CC=1 SCAR=6.9
39 0 ▶sub _error_class { 'Text::Nunjucks::Lib::Error' }
Subroutine _error_class: not called CC=1 SCAR=6.9
40 2106 ▶sub _frame_class { 'Text::Nunjucks::Runtime::Frame' }
Subroutine _frame_class: called CC=1 SCAR=0.0
41 778 ▶sub _gone_class { 'Text::Nunjucks::Context::Gone' }
Subroutine _gone_class: called CC=1 SCAR=0.0
42  
43 11977 ▶sub BUILDARGS {
Subroutine BUILDARGS: called CC=1 SCAR=1.2
Pod undocumented
44 0 ▶ my ( $class, @args ) = @_;
Subroutine BUILDARGS: not called CC=1 SCAR=1.2
Pod undocumented
45 0 return { _init_args => [@args] };
46 }
47  
48 11977 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=10.3
Pod undocumented
49 0 ▶ my ($self) = @_;
Subroutine BUILD: not called CC=2 SCAR=10.3
Pod undocumented
50 0 ▶ $self->init( @{ $self->_init_args // [] } );
Condition25%1/4
$self->_init_args // []
l!l
119770
$self->_init_args // []
l!l
00
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
MC/DC0%0/1
$self->_init_args // []
$self->_init_args
Truth table50%1/2
A
A= $self->_init_args
Aresult
11
00
Truth table0%0/2
A
A= $self->_init_args
Aresult
11
00
51 0 delete $self->{_init_args};
52 }
53  
54 11977 ▶sub init {
Subroutine init: called CC=3 SCAR=15.8
Pod undocumented
55 0 ▶ my ( $self, $ctx, $blocks, $env ) = @_;
Subroutine init: not called CC=3 SCAR=15.8
Pod undocumented
56  
57 # Has to be tied to an environment so we can tap into its globals.
58 0 ▶ $self->env( $env || $self->_environment_class->new );
Condition16%1/6
$env || $self->_environment_class->new
l!l&&r!l&&!r
1197700
$env || $self->_environment_class->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$env || $self->_environment_class->new
$env $self->_environment_class->new
MC/DC0%0/2
$env || $self->_environment_class->new
$env $self->_environment_class->new
Truth table33%1/3
A || B
A= $envB= $self->_environment_class->new
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $envB= $self->_environment_class->new
ABresult
1X1
011
000
59  
60 # Make a duplicate of ctx.
61 0 $self->ctx( $self->_lib->extend( {}, $ctx ) );
62  
63 0 $self->blocks( {} );
64 0 $self->exported( [] );
65  
66 0 for my $name ( @{ $self->_lib->keys($blocks) } ) {
67 0 $self->add_block( $name, $blocks->{$name} );
68 }
69 }
70  
71 38147 ▶sub lookup {
Subroutine lookup: called CC=3 SCAR=14.2
Pod undocumented
72 0 ▶ my ( $self, $name ) = @_;
Subroutine lookup: not called CC=3 SCAR=14.2
Pod undocumented
73 # Optimize for the typical case where the name isn't in the globals.
74 0 ▶ if ( exists $self->env->globals->{$name} && !exists $self->ctx->{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->env->globals->{$name} and not exists $self->ctx->{$name}) { } 497 37650 38147
if (exists $self->env->globals->{$name} and not exists $self->ctx->{$name}) { } 0 0 0
Condition50%3/6
exists $self->env->globals->{$name} and not exists $self->ctx->{$name}
!ll&&!rl&&r
376482497
exists $self->env->globals->{$name} and not exists $self->ctx->{$name}
!ll&&!rl&&r
000
MC/DC100%2/2
exists $self->env->globals->{$name} and not exists $self->ctx->{$name}
exists $self->env->globals->{$name} not exists $self->ctx->{$name}
MC/DC0%0/2
exists $self->env->globals->{$name} and not exists $self->ctx->{$name}
exists $self->env->globals->{$name} not exists $self->ctx->{$name}
Truth table100%3/3
A and not(B)
A= exists $self->env->globals->{$name}B= not exists $self->ctx->{$name}
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= exists $self->env->globals->{$name}B= not exists $self->ctx->{$name}
ABresult
0X0
100
111
75 0 return $self->env->globals->{$name};
76 }
77 else {
78 0 return $self->ctx->{$name};
79 }
80 }
81  
82 7068 ▶sub set_variable {
Subroutine set_variable: called CC=1 SCAR=1.2
Pod undocumented
83 0 ▶ my ( $self, $name, $val ) = @_;
Subroutine set_variable: not called CC=1 SCAR=1.2
Pod undocumented
84 0 $self->ctx->{$name} = $val;
85 }
86  
87 5426 ▶sub get_variables {
Subroutine get_variables: called CC=1 SCAR=1.2
Pod undocumented
88 0 ▶ my ($self) = @_;
Subroutine get_variables: not called CC=1 SCAR=1.2
Pod undocumented
89 0 return $self->ctx;
90 }
91  
92 2166 ▶sub add_block {
Subroutine add_block: called CC=2 SCAR=9.2
Pod undocumented
93 0 ▶ my ( $self, $name, $block ) = @_;
Subroutine add_block: not called CC=2 SCAR=9.2
Pod undocumented
94 0 ▶ $self->blocks->{$name} = $self->blocks->{$name} || [];
Condition50%2/4
$self->blocks->{$name} || []
l!l
10471119
$self->blocks->{$name} || []
l!l
00
MC/DC100%1/1
$self->blocks->{$name} || []
$self->blocks->{$name}
MC/DC0%0/1
$self->blocks->{$name} || []
$self->blocks->{$name}
Truth table100%2/2
A
A= $self->blocks->{$name}
Aresult
11
00
Truth table0%0/2
A
A= $self->blocks->{$name}
Aresult
11
00
95 0 push @{ $self->blocks->{$name} }, $block;
96 0 return $self;
97 }
98  
99 1120 ▶sub get_block {
Subroutine get_block: called CC=2 SCAR=11.6
Pod undocumented
100 0 ▶ my ( $self, $name ) = @_;
Subroutine get_block: not called CC=2 SCAR=11.6
Pod undocumented
101 0 ▶ if ( !$self->blocks->{$name} ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($self->blocks->{$name}) 0 1120 1120
unless ($self->blocks->{$name}) 0 0 0
102 0 die $self->_error_class->new( 'unknown block "' . $name . '"' );
103 }
104 0 return $self->blocks->{$name}[0];
105 }
106  
107 506 ▶sub get_super {
Subroutine get_super: called CC=4 SCAR=20.4
Pod undocumented
108 0 ▶ my ( $self, $env, $name, $block, $frame, $runtime, $cb ) = @_;
Subroutine get_super: not called CC=4 SCAR=20.4
Pod undocumented
109 0 ▶ my $idx = $self->_lib->index_of( $self->blocks->{$name} || [], $block );
Condition25%1/4
$self->blocks->{$name} || []
l!l
5060
$self->blocks->{$name} || []
l!l
00
MC/DC0%0/1
$self->blocks->{$name} || []
$self->blocks->{$name}
MC/DC0%0/1
$self->blocks->{$name} || []
$self->blocks->{$name}
Truth table50%1/2
A
A= $self->blocks->{$name}
Aresult
11
00
Truth table0%0/2
A
A= $self->blocks->{$name}
Aresult
11
00
110 0 my $blk = $self->blocks->{$name}[ $idx + 1 ];
111 0 my $context = $self;
112  
113 0 ▶ if ( $idx == -1 || !$blk ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idx == -1 or not $blk) 0 506 506
if ($idx == -1 or not $blk) 0 0 0
Condition16%1/6
$idx == -1 or not $blk
l!l&&r!l&&!r
00506
$idx == -1 or not $blk
l!l&&r!l&&!r
000
MC/DC0%0/2
$idx == -1 or not $blk
$idx == -1 not $blk
MC/DC0%0/2
$idx == -1 or not $blk
$idx == -1 not $blk
Truth table33%1/3
A or not(B)
A= $idx == -1B= not $blk
ABresult
1X1
011
000
Truth table0%0/3
A or not(B)
A= $idx == -1B= not $blk
ABresult
1X1
011
000
114 0 die $self->_error_class->new( 'no super block available for "' . $name . '"' );
115 }
116  
117 # Port-only (k83): the block of a cached parent, called by a template that
118 # includes itself N deep, runs N calls deep, as upstream nests it (ADR 0003).
119 # Perl warns at the 100th nested call of a sub, at the call site; the depth
120 # is bounded by memory, not by Perl (see Lib async_iter).
121 15 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
122 0 $blk->( $env, $context, $frame, $runtime, $cb );
123 }
124  
125 # addExport(name) -- pushes the name; the compiler may pass an extra value arg
126 # (compile_set), which is ignored, exactly as upstream ignores it.
127 5358 ▶sub add_export {
Subroutine add_export: called CC=1 SCAR=1.2
Pod undocumented
128 0 ▶ my ( $self, $name ) = @_;
Subroutine add_export: not called CC=1 SCAR=1.2
Pod undocumented
129 0 push @{ $self->exported }, $name;
130 }
131  
132 1547 ▶sub get_exported {
Subroutine get_exported: called CC=2 SCAR=9.2
Pod undocumented
133 0 ▶ my ($self) = @_;
Subroutine get_exported: not called CC=2 SCAR=9.2
Pod undocumented
134 0 my $exported = {};
135 0 for my $name ( @{ $self->exported } ) {
136 0 $exported->{$name} = $self->ctx->{$name};
137 }
138 0 return $exported;
139 }
140  
141 #### lifetime -- port-only, no upstream counterpart (ADR 0021)
142 #
143 # No compiled sub holds its Context (Runtime _render_scope): the call that
144 # started the render does, and each continuation until it is called (Runtime
145 # _continuation). The rest of what a tracing GC does for upstream is here.
146  
147 # The Context of a template that imported a library keeps the library's
148 # Context: the macros it took from the library call each other and read the
149 # library's variables through it. The compiled `import` and `from ... import`
150 # callbacks call this with the third argument Template _get_exported hands
151 # them. It also keeps the Context of a template it included, if that one has
152 # to outlive its render (_included_by).
153 #
154 # A Context that keeps another one has to outlive its own render too: a macro
155 # of the kept one may be in the hands of whoever included this one.
156 1903 ▶sub _keep_alive {
Subroutine _keep_alive: called CC=2 SCAR=10.0
157 0 ▶ my ( $self, $context ) = @_;
Subroutine _keep_alive: not called CC=2 SCAR=10.0
158 0 ▶ return unless $context;
Branch: 2 branches
BranchTrueFalseTotal
unless $context 0 1903 1903
unless $context 0 0 0
159 0 push @{ $self->_kept }, $context;
160 0 $self->_made_closure;
161 0 return;
162 }
163  
164 # An included template renders in a Context of its own, and nothing holds
165 # that once its render has delivered its result. Upstream's stays reachable
166 # through whatever the included template handed out: a macro or call block it
167 # put into an array of the including template or passed to a function of the
168 # application, an extension that kept the body it was given, a macro of a
169 # library it imported. So the Context of the including template keeps it --
170 # but only if there is such a thing, because a partial included in a loop of
171 # 100,000 rows would otherwise hold 100,000 Contexts until the render ends.
172 #
173 # Template render calls this for the fourth argument a compiled `include`
174 # passes, and uses what it returns as the callback. From here on this Context
175 # holds its includer. When its render delivers, the includer takes it over
176 # (_keep_alive) if it has made a closure; if it has not, the includer does so
177 # at the moment it makes its first one -- an `include` inside `if` or `for`
178 # whose template waits for an async filter delivers after the includer went
179 # on. The includer never keeps a Context that still holds it, so the two are
180 # no cycle.
181 #
182 # Why not take it over the moment the closure is made: until the render
183 # delivers, the include's continuation holds the including Context, and a
184 # macro of the included template that contains an async construct holds that
185 # continuation, as its `$cb`. An included render that never completes would
186 # keep both Contexts for good.
187 #
188 # A render that delivers an error is taken over like any other: code of the
189 # including template still runs after it -- what follows an `if`, a `for` or a
190 # macro call around the include, a sibling asyncAll branch -- and can call a
191 # macro the included template handed out before it failed. That the rest of
192 # the include is never called then, and would hold the includer through the
193 # same `$cb`, is dealt with where the include's callback is compiled
194 # (Compiler compile_include).
195 4960 ▶sub _included_by {
Subroutine _included_by: called CC=1 SCAR=1.2
196 0 ▶ my ( $self, $includer, $cb ) = @_;
Subroutine _included_by: not called CC=1 SCAR=1.2
197 0 $self->_includer($includer);
198 0 weaken( my $included = $self );
199 4962 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
200 0 ▶ $included->_delivered if $included;
Subroutine __ANON__: not called CC=2 SCAR=9.2
Branch: 2 branches
BranchTrueFalseTotal
if $included 4961 1 4962
if $included 0 0 0
201 0 return $cb->(@_);
202 0 };
203 }
204  
205 # The compiled code calls this before it makes a macro or a call block and
206 # before it hands a body to an extension (Compiler _emit_made_closure).
207 4469 ▶sub _made_closure {
Subroutine _made_closure: called CC=3 SCAR=14.2
208 0 ▶ my ($self) = @_;
Subroutine _made_closure: not called CC=3 SCAR=14.2
209 0 ▶ return if $self->_has_closure;
Branch: 2 branches
BranchTrueFalseTotal
if $self->_has_closure 1700 2769 4469
if $self->_has_closure 0 0 0
210 0 $self->_has_closure(1);
211 0 ▶ $self->_hand_over if $self->_has_delivered;
Branch: 2 branches
BranchTrueFalseTotal
if $self->_has_delivered 2 2767 2769
if $self->_has_delivered 0 0 0
212 0 return;
213 }
214  
215 4961 ▶sub _delivered {
Subroutine _delivered: called CC=2 SCAR=9.2
216 0 ▶ my ($self) = @_;
Subroutine _delivered: not called CC=2 SCAR=9.2
217 0 $self->_has_delivered(1);
218 0 ▶ $self->_hand_over if $self->_has_closure;
Branch: 2 branches
BranchTrueFalseTotal
if $self->_has_closure 340 4621 4961
if $self->_has_closure 0 0 0
219 0 return;
220 }
221  
222 342 ▶sub _hand_over {
Subroutine _hand_over: called CC=2 SCAR=9.2
223 0 ▶ my ($self) = @_;
Subroutine _hand_over: not called CC=2 SCAR=9.2
224 0 ▶ my $includer = $self->_includer or return;
Branch: 2 branches
BranchTrueFalseTotal
my $includer = $self->_includer or return 337 5 342
my $includer = $self->_includer or return 0 0 0
225 0 $self->_includer(undef);
226 0 $includer->_keep_alive($self);
227 0 return;
228 }
229  
230 # Every root and block sub registers its `$context` and `$frame` variables
231 # here when it is entered. They are remembered weakly, by address: a variable
232 # no closure captured is gone when its sub returns, one that was captured
233 # lives as long as the closures do.
234 #
235 # The `$frame` variable is a cycle of its own. A macro swaps and restores it
236 # (upstream: `frame = callerFrame`), so the macro captures it; a macro
237 # reachable from the Frame the variable holds -- stored with `set`, imported
238 # into a frame that Frame is a parent of, the `caller` of a macro call that
239 # died before it restored the variable, or simply defined after an `include`,
240 # whose continuation it then keeps -- can never be freed by a reference count.
241 14643 ▶sub _track_scope {
Subroutine _track_scope: called CC=1 SCAR=1.2
242 0 ▶ my ( $self, $context_ref, $frame_ref ) = @_;
Subroutine _track_scope: not called CC=1 SCAR=1.2
243 0 $self->_track_var( $self->_context_vars, $context_ref );
244 0 $self->_track_var( $self->_frame_vars, $frame_ref );
245 0 return;
246 }
247  
248 29286 ▶sub _track_var {
Subroutine _track_var: called CC=2 SCAR=10.3
249 0 ▶ my ( $self, $vars, $ref ) = @_;
Subroutine _track_var: not called CC=2 SCAR=10.3
250 0 my $addr = refaddr($ref);
251 0 ▶ weaken( $vars->{$addr} = $ref ) unless defined $vars->{$addr};
Branch: 2 branches
BranchTrueFalseTotal
unless defined $vars->{$addr} 29286 0 29286
unless defined $vars->{$addr} 0 0 0
252 0 return;
253 }
254  
255 # The Context is freed: nothing of its render is running or waiting to be
256 # called, and nothing else -- an importing or including Context, the hash
257 # Template get_exported returned or a function out of it -- holds it. Code of
258 # that render can still run: a macro the application kept, a continuation
259 # that is called a second time. Every `$context` variable it can see gets the
260 # stand-in that dies with a clear error on first use (instead of the undef the
261 # weak reference would leave), every `$frame` variable an empty Frame: what a
262 # frame variable holds
263 # from here on is only ever saved and put back by a macro, or pushed on by a
264 # call block, so the empty Frame is enough for a macro that reads nothing
265 # outside itself, and the Frame that was there, with the macros it holds, is
266 # freed.
267 11955 ▶sub DEMOLISH {
Subroutine DEMOLISH: called CC=4 SCAR=18.6
Pod undocumented
268 0 ▶ my ( $self, $in_global_destruction ) = @_;
Subroutine DEMOLISH: not called CC=4 SCAR=18.6
Pod undocumented
269 0 ▶ return if $in_global_destruction;
Branch: 2 branches
BranchTrueFalseTotal
if $in_global_destruction 0 11955 11955
if $in_global_destruction 0 0 0
270  
271 0 my @context_refs = grep { defined } values %{ $self->_context_vars };
272 0 for my $context_ref (@context_refs) {
273 0 $$context_ref = $self->_gone_class->instance;
274 }
275  
276 0 my @frame_refs = grep { defined } values %{ $self->_frame_vars };
277 0 for my $frame_ref (@frame_refs) {
278 # the old Frame is let go only after the variable holds the new one:
279 # freeing it can free the macros that hold the variable itself
280 0 my $frame = $$frame_ref;
281 0 $$frame_ref = $self->_frame_class->new;
282 }
283 0 return;
284 }
285  
286 15 1;
287  
288 __END__
« blib/lib/Text/Nunjucks/Globals.pm ↑ Summary blib/lib/Text/Nunjucks/Tests.pm »