blib/lib/Text/Nunjucks/Compiler/Compiler.pm

filter: Statementstmt 78.3% 764 / 975 Branchbran 65.2% 171 / 262 Conditioncond 46.1% 36 / 78 MC/DCmcdc 34.6% 17 / 49 Subroutinesub 75.1% 112 / 149 Podpod 0.0% 0 / 72 totaltotal 69.4% 1100 / 1585 CC 267 SCAR 23.1
CC
267
Cov
74%
compile_call_extension
70.8
compile_call_extension
70.8
compile_literal
18.3
CRAP 10.1
« blib/lib/Text/Nunjucks/Compiler.pm ↑ Summary blib/lib/Text/Nunjucks/Loader/FileSystem.pm »
1 package Text::Nunjucks::Compiler::Compiler;
2 # ABSTRACT: The code generator, emitting Perl source (compiler.js class Compiler)
3  
4 16 our $VERSION = '0.001';
5  
6 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 16 ▶use builtin qw( is_bool created_as_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
8 16 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 16 ▶use Text::Nunjucks::Nodes;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 16 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 16 ▶use Text::Nunjucks::Lib::TemplateError;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 16 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 16 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # upstream: class Compiler extends Obj in compiler.js (ADR 0002: a Moo class).
18 #
19 # THE ONE STRUCTURAL DIFFERENCE FROM EVERY OTHER PORTED FILE: compiler.js emits
20 # *JavaScript* source; this compiler emits *Perl* source (ADR 0003 keeps the CPS
21 # shape). So a compile* method's control flow follows upstream, but the strings
22 # it emits are Perl. The systematic JS->Perl translations are:
23 # * every emitted local is `$name` (JS bare `name`); the root shape is
24 # `$root = sub { my ($env,$context,$frame,$runtime,$cb) = @_; ... }`
25 # and blocks are `$b_<name> = sub {...}`, assigned to forward-declared
26 # lexicals; inside its own body a block sub names itself with a per-call
27 # `my $b_<name> = CORE::__SUB__;` (what JS's `function b_<name>` gives for
28 # free), which is what super() passes on -- see compile_root. getCode()'s
29 # string eval()s to the { root => \&, b_<name> => \& } hashref (upstream:
30 # new Function()).
31 # * try/catch -> eval {} + `if (my $e = $@)`; `cb(...)` -> `$cb->(...)`.
32 # * EVERY operator goes through a Runtime op_* call, never a native Perl
33 # operator (Perl's +,==,... lack JS semantics) -- compileAdd -> op_add,
34 # compileCompare -> op_eq/op_lt/... , compileNot -> op_not, &c. This is the
35 # runtime surface k14 built for exactly this (ADR 0007 / value model).
36 # * every JS-truthiness test on a template value is $runtime->_truthy(...)
37 # (compileIf/compileInlineIf/compileFor's if(arr)); internal booleans
38 # (err, frame.topLevel, parentTemplate) stay native Perl truthiness.
39 # * container output goes through $runtime->_js_to_string(...) so numbers use
40 # ADR-0007 number_to_string, not Perl's %.15g (forward note from k14 audit).
41 # * template dict literals build a $runtime->make_dict(...) (Hash::Ordered,
42 # ADR 0011) to keep insertion order; kwargs stay a plain hashref.
43 # * every string that reaches emitted code goes through _quote (one routine):
44 # a Perl single-quoted literal, so `$`, `@`, `\`, `'`, newlines are safe.
45 #
46 # Open touches, encapsulated and reported (not decided here): regex literals
47 # (k7 / ADR 0013 -- emitted via a single $runtime->make_regex seam that runtime
48 # does not implement yet); extensions / compileCallExtension (k8 / k22 -- the
49 # extension object surface is still open); the Environment/Context method
50 # surface the emitted code calls (get_filter/get_test/get_template/waterfall,
51 # lookup/set_variable/get_block/... ) lands with k16.
52  
53 # init(templateName, throwOnUndefined) -- Moo attributes are the field set.
54 16 has template_name => ( is => 'ro' );
55 9364 ▶has codebuf => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
56 9364 ▶has last_id => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
57 9364 ▶has buffer => ( is => 'rw', default => sub { undef } );
Subroutine __ANON__: called CC=1 SCAR=0.0
58 9364 ▶has buffer_stack => ( is => 'ro', default => sub { [] } );
Subroutine __ANON__: called CC=1 SCAR=0.0
59 9364 ▶has _scope_closers => ( is => 'rw', default => sub { '' } );
Subroutine __ANON__: called CC=1 SCAR=0.0
60 9364 ▶has in_block => ( is => 'rw', default => sub { 0 } );
Subroutine __ANON__: called CC=1 SCAR=0.0
61 16 has throw_on_undefined => ( is => 'ro' );
62  
63 # Collaborators named once, reached through the invocant (house rule).
64 520586 ▶sub node { 'Text::Nunjucks::Nodes::' . $_[1] }
Subroutine node: called CC=1 SCAR=0.0
Pod undocumented
65 11874 ▶sub frame_class { 'Text::Nunjucks::Runtime::Frame' }
Subroutine frame_class: called CC=1 SCAR=0.0
Pod undocumented
66 5 ▶sub template_error_class { 'Text::Nunjucks::Lib::TemplateError' }
Subroutine template_error_class: called CC=1 SCAR=0.0
Pod undocumented
67 438 ▶sub error_class { 'Text::Nunjucks::Lib::Error' }
Subroutine error_class: called CC=1 SCAR=0.0
Pod undocumented
68  
69 #### string quoting -- the one routine every emitted string goes through
70 # A Perl single-quoted literal: escape \ then ', leave everything else
71 # (including $, @, newlines, tabs, unicode) verbatim. Single quotes do not
72 # interpolate, so this is safe for arbitrary template text and values.
73 76437 ▶sub _quote {
Subroutine _quote: called CC=2 SCAR=10.1
74 0 ▶ my ( $self, $str ) = @_;
Subroutine _quote: not called CC=2 SCAR=10.1
75 0 ▶ $str = '' unless defined $str;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $str 0 76437 76437
unless defined $str 0 0 0
76 0 $str =~ s/\\/\\\\/g;
77 0 $str =~ s/'/\\'/g;
78 0 return "'" . $str . "'";
79 }
80  
81 # A number for emission: lineno/colno, or `undef` for a missing position.
82 24876 ▶sub _num_or_undef {
Subroutine _num_or_undef: called CC=2 SCAR=10.5
83 0 ▶ my ( $self, $v ) = @_;
Subroutine _num_or_undef: not called CC=2 SCAR=10.5
84 0 ▶ return 'undef' unless defined $v;
Branch: 2 branches
BranchTrueFalseTotal
unless defined $v 0 24876 24876
unless defined $v 0 0 0
85 0 return "$v";
86 }
87  
88 5 ▶sub fail {
Subroutine fail: called CC=3 SCAR=16.5
Pod undocumented
89 0 ▶ my ( $self, $msg, $lineno, $colno ) = @_;
Subroutine fail: not called CC=3 SCAR=16.5
Pod undocumented
90  
91 0 ▶ $lineno = $lineno + 1 if defined $lineno;
Branch: 2 branches
BranchTrueFalseTotal
if defined $lineno 5 0 5
if defined $lineno 0 0 0
92 0 ▶ $colno = $colno + 1 if defined $colno;
Branch: 2 branches
BranchTrueFalseTotal
if defined $colno 5 0 5
if defined $colno 0 0 0
93  
94 0 die $self->template_error_class->new( $msg, $lineno, $colno );
95 }
96  
97 1482 ▶sub _push_buffer {
Subroutine _push_buffer: called CC=1 SCAR=1.2
98 0 ▶ my ($self) = @_;
Subroutine _push_buffer: not called CC=1 SCAR=1.2
99 0 my $id = $self->_tmpid;
100 0 push @{ $self->buffer_stack }, $self->buffer;
101 0 $self->buffer($id);
102 0 $self->_emit( 'my $' . $self->buffer . ' = "";' );
103 0 return $id;
104 }
105  
106 1482 ▶sub _pop_buffer {
Subroutine _pop_buffer: called CC=1 SCAR=1.2
107 0 ▶ my ($self) = @_;
Subroutine _pop_buffer: not called CC=1 SCAR=1.2
108 0 $self->buffer( pop @{ $self->buffer_stack } );
109 }
110  
111 581958 ▶sub _emit {
Subroutine _emit: called CC=1 SCAR=1.2
112 0 ▶ my ( $self, $code ) = @_;
Subroutine _emit: not called CC=1 SCAR=1.2
113 0 push @{ $self->codebuf }, $code;
114 }
115  
116 386441 ▶sub _emit_line {
Subroutine _emit_line: called CC=1 SCAR=1.2
117 0 ▶ my ( $self, $code ) = @_;
Subroutine _emit_line: not called CC=1 SCAR=1.2
118 0 $self->_emit( $code . "\n" );
119 }
120  
121 1418 ▶sub _emit_lines {
Subroutine _emit_lines: called CC=2 SCAR=9.2
122 0 ▶ my ( $self, @lines ) = @_;
Subroutine _emit_lines: not called CC=2 SCAR=9.2
123 0 $self->_emit_line($_) for @lines;
124 }
125  
126 10528 ▶sub _emit_func_begin {
Subroutine _emit_func_begin: called CC=1 SCAR=1.2
127 0 ▶ my ( $self, $node, $name ) = @_;
Subroutine _emit_func_begin: not called CC=1 SCAR=1.2
128 0 $self->buffer('output');
129 0 $self->_scope_closers('');
130 # $<name> = sub { ... } -- assigned to a forward-declared lexical
131 0 $self->_emit_line( '$' . $name . ' = sub {' );
132 0 $self->_emit_line('my ($env, $context, $frame, $runtime, $cb) = @_;');
133  
134 # Port-only (ADR 0021). In upstream everything in this function closes over
135 # `context` -- every continuation callback, every macro -- and the Context
136 # holds the macros: cycles a tracing GC collects and a reference count never
137 # frees. So no root or block sub holds its Context: this line weakens the
138 # sub's $context variable, which is the one variable all its nested subs
139 # capture, and registers it and $frame with the Context (Runtime
140 # _render_scope). What holds the Context is what is still going to run: the
141 # call that started the render, and each continuation until it is called
142 # (_continuation_open).
143 0 $self->_emit_line('$runtime->_render_scope(\$context, \$frame);');
144  
145 0 $self->_emit_line( 'my $lineno = ' . $self->_num_or_undef( $node->lineno ) . ';' );
146 0 $self->_emit_line( 'my $colno = ' . $self->_num_or_undef( $node->colno ) . ';' );
147 0 $self->_emit_line( 'my $' . $self->buffer . ' = "";' );
148 0 $self->_emit_line('eval {');
149 }
150  
151 10522 ▶sub _emit_func_end {
Subroutine _emit_func_end: called CC=2 SCAR=9.2
152 0 ▶ my ( $self, $no_return ) = @_;
Subroutine _emit_func_end: not called CC=2 SCAR=9.2
153 0 ▶ if ( !$no_return ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($no_return) 1175 9347 10522
unless ($no_return) 0 0 0
154 0 $self->_emit_line( '$cb->(undef, $' . $self->buffer . ');' );
155 }
156  
157 0 $self->_close_scope_levels;
158 0 $self->_emit_line('};'); # close eval {
159 0 $self->_emit_line('if (my $e = $@) {');
160 0 $self->_emit_line(' $cb->($runtime->handle_error($e, $lineno, $colno));');
161 0 $self->_emit_line('}');
162 0 $self->_emit_line('};'); # close the sub ( $<name> = sub {...}; )
163 0 $self->buffer(undef);
164 }
165  
166 # A scope level is a continuation (_make_callback, or the waterfall callback
167 # of compile_include). A continuation is an argument of
168 # $runtime->_continuation( -- hence the second parenthesis, which upstream's
169 # '})' does not have -- unless it was made plain (see _make_callback), which
170 # closes like upstream's. With two kinds of closers they have to come in the
171 # reverse order of opening, so the newest goes in front.
172 7746 ▶sub _add_scope_level {
Subroutine _add_scope_level: called CC=2 SCAR=9.2
173 0 ▶ my ( $self, $plain ) = @_;
Subroutine _add_scope_level: not called CC=2 SCAR=9.2
174 0 ▶ $self->_scope_closers( ( $plain ? '})' : '}))' ) . $self->_scope_closers );
Branch: 2 branches
BranchTrueFalseTotal
$plain ? : 1442 6304 7746
$plain ? : 0 0 0
175 }
176  
177 15817 ▶sub _close_scope_levels {
Subroutine _close_scope_levels: called CC=1 SCAR=1.2
178 0 ▶ my ($self) = @_;
Subroutine _close_scope_levels: not called CC=1 SCAR=1.2
179 0 $self->_emit_line( $self->_scope_closers . ';' );
180 0 $self->_scope_closers('');
181 }
182  
183 5295 ▶sub _with_scoped_syntax {
Subroutine _with_scoped_syntax: called CC=1 SCAR=1.2
184 0 ▶ my ( $self, $func ) = @_;
Subroutine _with_scoped_syntax: not called CC=1 SCAR=1.2
185 0 my $scope_closers = $self->_scope_closers;
186 0 $self->_scope_closers('');
187  
188 0 $func->();
189  
190 0 $self->_close_scope_levels;
191 0 $self->_scope_closers($scope_closers);
192 }
193  
194 # Port-only (ADR 0021): what upstream emits as `function(...) {` for a
195 # continuation -- a callback handed to something that calls it when it is
196 # done. The compiled subs do not hold their Context (_emit_func_begin); a
197 # continuation that is still waiting to be called has to, or the Context of a
198 # render would be gone before an async filter calls back. Runtime
199 # _continuation wraps the sub so that it holds the Context from here until it
200 # is called. It is closed with '}))', see _add_scope_level.
201 11620 ▶sub _continuation_open {
Subroutine _continuation_open: called CC=1 SCAR=1.2
202 0 ▶ my ($self) = @_;
Subroutine _continuation_open: not called CC=1 SCAR=1.2
203 0 return '$runtime->_continuation($context, sub {';
204 }
205  
206 # `$runtime->_continuation($context, sub { my ($t_err[, $res]) = @_;
207 # if ($t_err) { $cb->($t_err); return; }` -- an unclosed continuation, closed
208 # by a later scope closer.
209 #
210 # Port-only (ADR 0021): with $plain it is upstream's bare `sub {`, which holds
211 # nothing. That is for the callback handed to a block (compile_block,
212 # compile_super): the block runs in the same Context, so whatever of the block
213 # is still going to run holds that Context already, and the block calls this
214 # callback from inside it. A hold of its own would be one a macro of the block
215 # keeps for good: a macro whose body contains an async construct captures the
216 # block's `$cb`, which is this callback, and the block's frame holds the
217 # macro -- a block that never finishes would keep its Context for ever.
218 #
219 # Port-only (ADR 0021): $on_error is a statement that goes in front of the
220 # `$cb->($t_err)` of the error branch, see compile_include and
221 # _keep_alive_statement.
222 10404 ▶sub _make_callback {
Subroutine _make_callback: called CC=4 SCAR=17.9
223 0 ▶ my ( $self, $res, $plain, $on_error ) = @_;
Subroutine _make_callback: not called CC=4 SCAR=17.9
224 0 my $err = $self->_tmpid;
225  
226 return
227 0 ▶ ( $plain ? 'sub {' : $self->_continuation_open ) . "\n"
Branch: 4 branches
BranchTrueFalseTotal
$plain ? : 1442 8962 10404
$res ? : 10390 14 10404
$plain ? : 0 0 0
$res ? : 0 0 0
Condition50%2/4
$on_error // ""
l!l
37516653
$on_error // ""
l!l
00
MC/DC100%1/1
$on_error // ""
$on_error
MC/DC0%0/1
$on_error // ""
$on_error
Truth table100%2/2
A
A= $on_error
Aresult
11
00
Truth table0%0/2
A
A= $on_error
Aresult
11
00
228 . 'my ($' . $err . ( $res ? ', $' . $res : '' ) . ') = @_;' . "\n"
229 . 'if($' . $err . ') { ' . ( $on_error // '' ) . '$cb->($' . $err . '); return; }';
230 }
231  
232 32318 ▶sub _tmpid {
Subroutine _tmpid: called CC=1 SCAR=1.2
233 0 ▶ my ($self) = @_;
Subroutine _tmpid: not called CC=1 SCAR=1.2
234 0 $self->last_id( $self->last_id + 1 );
235 0 return 't_' . $self->last_id;
236 }
237  
238 4325 ▶sub _template_name {
Subroutine _template_name: called CC=2 SCAR=9.2
239 0 ▶ my ($self) = @_;
Subroutine _template_name: not called CC=2 SCAR=9.2
240 0 ▶ return defined $self->template_name
Branch: 2 branches
BranchTrueFalseTotal
defined $self->template_name ? : 4027 298 4325
defined $self->template_name ? : 0 0 0
241 ? $self->_quote( $self->template_name )
242 : 'undef';
243 }
244  
245 16032 ▶sub _compile_children {
Subroutine _compile_children: called CC=2 SCAR=9.2
246 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine _compile_children: not called CC=2 SCAR=9.2
247 0 for my $child ( @{ $node->children } ) {
248 0 $self->compile( $child, $frame );
249 }
250 }
251  
252 15018 ▶sub _compile_aggregate {
Subroutine _compile_aggregate: called CC=7 SCAR=27.3
253 0 ▶ my ( $self, $node, $frame, $start_char, $end_char ) = @_;
Subroutine _compile_aggregate: not called CC=7 SCAR=27.3
254 0 ▶ $self->_emit($start_char) if defined $start_char && length $start_char;
Branch: 2 branches
BranchTrueFalseTotal
if defined $start_char and length $start_char 4496 10522 15018
if defined $start_char and length $start_char 0 0 0
Condition33%2/6
defined $start_char and length $start_char
!ll&&!rl&&r
1052204496
defined $start_char and length $start_char
!ll&&!rl&&r
000
MC/DC50%1/2
defined $start_char and length $start_char
defined $start_char length $start_char
MC/DC0%0/2
defined $start_char and length $start_char
defined $start_char length $start_char
Truth table66%2/3
A and B
A= defined $start_charB= length $start_char
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $start_charB= length $start_char
ABresult
0X0
100
111
255  
256 0 my $children = $node->children;
257 0 for my $i ( 0 .. $#$children ) {
258 0 ▶ $self->_emit(', ') if $i > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $i > 0 10715 13909 24624
if $i > 0 0 0 0
259 0 $self->compile( $children->[$i], $frame );
260 }
261  
262 0 ▶ $self->_emit($end_char) if defined $end_char && length $end_char;
Branch: 2 branches
BranchTrueFalseTotal
if defined $end_char and length $end_char 4496 10518 15014
if defined $end_char and length $end_char 0 0 0
Condition33%2/6
defined $end_char and length $end_char
!ll&&!rl&&r
1051804496
defined $end_char and length $end_char
!ll&&!rl&&r
000
MC/DC50%1/2
defined $end_char and length $end_char
defined $end_char length $end_char
MC/DC0%0/2
defined $end_char and length $end_char
defined $end_char length $end_char
Truth table66%2/3
A and B
A= defined $end_charB= length $end_char
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $end_charB= length $end_char
ABresult
0X0
100
111
263 }
264  
265 17241 ▶sub _compile_expression {
Subroutine _compile_expression: called CC=1 SCAR=1.2
266 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine _compile_expression: not called CC=1 SCAR=1.2
267 $self->assert_type(
268 $node,
269 0 map { $self->node($_) } qw(
270 Literal Symbol Group Array Dict FunCall Caller Filter LookupVal
271 Compare InlineIf In Is And Or Not Add Concat Sub Mul Div FloorDiv
272 Mod Pow Neg Pos NodeList
273 )
274 );
275 0 $self->compile( $node, $frame );
276 }
277  
278 28377 ▶sub assert_type {
Subroutine assert_type: called CC=2 SCAR=11.6
Pod undocumented
279 0 ▶ my ( $self, $node, @types ) = @_;
Subroutine assert_type: not called CC=2 SCAR=11.6
Pod undocumented
280 0 ▶ unless ( grep { $node->isa($_) } @types ) {
Branch: 2 branches
BranchTrueFalseTotal
unless (grep {$node->isa($_);} @types) 0 28377 28377
unless (grep {$node->isa($_);} @types) 0 0 0
281 0 $self->fail( 'assertType: invalid type: ' . $node->typename,
282 $node->lineno, $node->colno );
283 }
284 }
285  
286 14 ▶sub compile_call_extension {
Subroutine compile_call_extension: called CC=18 SCAR=42.6
Pod undocumented
287 0 ▶ my ( $self, $node, $frame, $async ) = @_;
Subroutine compile_call_extension: not called CC=18 SCAR=42.6
Pod undocumented
288 # k8/k22: the extension object surface is still open. Ported 1:1 in shape;
289 # get_extension(name)->prop(context, ...) assumes a blessed extension whose
290 # tag handler is a method (mirrors ext.getExtension(name)[prop](context,...)).
291 0 my $args = $node->args;
292 0 my $content_args = $node->contentArgs;
293 0 ▶ my $autoescape =
Branch: 2 branches
BranchTrueFalseTotal
defined $node->autoescape && builtin::is_bool($node->autoescape) ? : 1 13 14
defined $node->autoescape && builtin::is_bool($node->autoescape) ? : 0 0 0
Condition33%2/6
defined $node->autoescape and builtin::is_bool($node->autoescape)
!ll&&!rl&&r
1301
defined $node->autoescape and builtin::is_bool($node->autoescape)
!ll&&!rl&&r
000
MC/DC50%1/2
defined $node->autoescape and builtin::is_bool($node->autoescape)
defined $node->autoescape builtin::is_bool($node->autoescape)
MC/DC0%0/2
defined $node->autoescape and builtin::is_bool($node->autoescape)
defined $node->autoescape builtin::is_bool($node->autoescape)
Truth table66%2/3
A and B
A= defined $node->autoescapeB= builtin::is_bool($node->autoescape)
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= defined $node->autoescapeB= builtin::is_bool($node->autoescape)
ABresult
0X0
100
111
294 ( defined( $node->autoescape ) && is_bool( $node->autoescape ) )
295 ? $node->autoescape
296 : 1;
297 0 ▶ my $ae = $autoescape ? '1' : '0';
Branch: 2 branches
BranchTrueFalseTotal
$autoescape ? : 13 1 14
$autoescape ? : 0 0 0
298  
299 # Port-only (ADR 0021): a content argument is a function the extension can
300 # keep and call later, see _emit_made_closure.
301 0 ▶ $self->_emit_made_closure if grep { $_ } @$content_args;
Branch: 2 branches
BranchTrueFalseTotal
if grep {$_;} @$content_args 10 4 14
if grep {$_;} @$content_args 0 0 0
302  
303 # `buffer += ...` is Runtime js_string_append throughout: JS's buffer joins
304 # code units, so it re-pairs two halves of a surrogate pair (ADR 0012).
305 0 ▶ if ( !$async ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($async) 13 1 14
unless ($async) 0 0 0
306 0 $self->_emit( '$runtime->js_string_append($' . $self->buffer
307 . ', $runtime->_js_to_string($runtime->suppress_value(' );
308 }
309  
310 0 $self->_emit( '$env->get_extension(' . $self->_quote( $node->extName )
311 . ')->' . $node->prop . '(' );
312 0 $self->_emit('$context');
313  
314 0 ▶ if ( $args || @$content_args ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args or @$content_args) 14 0 14
if ($args or @$content_args) 0 0 0
Condition16%1/6
$args or @$content_args
l!l&&r!l&&!r
1400
$args or @$content_args
l!l&&r!l&&!r
000
MC/DC0%0/2
$args or @$content_args
$args @$content_args
MC/DC0%0/2
$args or @$content_args
$args @$content_args
Truth table33%1/3
A or B
A= $argsB= @$content_args
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $argsB= @$content_args
ABresult
1X1
011
000
315 0 $self->_emit(', ');
316 }
317  
318 0 ▶ if ($args) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args) 14 0 14
if ($args) 0 0 0
319 0 ▶ if ( !$args->isa( $self->node('NodeList') ) ) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($args->isa($self->node("NodeList"))) 0 14 14
unless ($args->isa($self->node("NodeList"))) 0 0 0
320 0 $self->fail( 'compileCallExtension: arguments must be a NodeList, '
321 . 'use `parser.parseSignature`' );
322 }
323  
324 0 my $arg_children = $args->children;
325 0 for my $i ( 0 .. $#$arg_children ) {
326 0 $self->_compile_expression( $arg_children->[$i], $frame );
327  
328 0 ▶ if ( $i != $#$arg_children || @$content_args ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i != $arg_children->$#* or @$content_args) 3 4 7
if ($i != $arg_children->$#* or @$content_args) 0 0 0
Condition50%3/6
$i != $arg_children->$#* or @$content_args
l!l&&r!l&&!r
124
$i != $arg_children->$#* or @$content_args
l!l&&r!l&&!r
000
MC/DC100%2/2
$i != $arg_children->$#* or @$content_args
$i != $arg_children->$#* @$content_args
MC/DC0%0/2
$i != $arg_children->$#* or @$content_args
$i != $arg_children->$#* @$content_args
Truth table100%3/3
A or B
A= $i != $arg_children->$#*B= @$content_args
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= $i != $arg_children->$#*B= @$content_args
ABresult
1X1
011
000
329 0 $self->_emit(', ');
330 }
331 }
332 }
333  
334 0 ▶ if (@$content_args) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$content_args) 10 4 14
if (@$content_args) 0 0 0
335 0 for my $i ( 0 .. $#$content_args ) {
336 0 my $arg = $content_args->[$i];
337 0 ▶ $self->_emit(', ') if $i > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $i > 0 2 10 12
if $i > 0 0 0 0
338  
339 0 ▶ if ($arg) {
Branch: 2 branches
BranchTrueFalseTotal
if ($arg) { } 11 1 12
if ($arg) { } 0 0 0
340 0 $self->_emit_line('sub { my ($cb) = @_;');
341 0 $self->_emit_line(
342 'if(!$cb) { $cb = sub { my ($err) = @_; if($err) { die $err; } } }');
343 0 my $id = $self->_push_buffer;
344  
345 $self->_with_scoped_syntax(
346 11 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
347 0 ▶ $self->compile( $arg, $frame );
Subroutine __ANON__: not called CC=1 SCAR=1.2
348 0 $self->_emit_line( '$cb->(undef, $' . $id . ');' );
349 }
350 0 );
351  
352 0 $self->_pop_buffer;
353 0 $self->_emit_line( 'return $' . $id . ';' );
354 0 $self->_emit_line('}');
355 }
356 else {
357 0 $self->_emit('undef');
358 }
359 }
360 }
361  
362 0 ▶ if ($async) {
Branch: 2 branches
BranchTrueFalseTotal
if ($async) { } 1 13 14
if ($async) { } 0 0 0
363 0 my $res = $self->_tmpid;
364 0 $self->_emit_line( ', ' . $self->_make_callback($res) );
365 0 $self->_emit_line( '$runtime->js_string_append($' . $self->buffer
366 . ', $runtime->_js_to_string($runtime->suppress_value($'
367 . $res . ', ' . $ae . ' && $env->opts->{autoescape})));' );
368 0 $self->_add_scope_level;
369 }
370 else {
371 0 $self->_emit(')');
372 0 $self->_emit( ', ' . $ae . " && \$env->opts->{autoescape})));\n" );
373 }
374 }
375  
376 1 ▶sub compile_call_extension_async {
Subroutine compile_call_extension_async: called CC=1 SCAR=1.2
Pod undocumented
377 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_call_extension_async: not called CC=1 SCAR=1.2
Pod undocumented
378 0 $self->compile_call_extension( $node, $frame, 1 );
379 }
380  
381 6679 ▶sub compile_node_list {
Subroutine compile_node_list: called CC=1 SCAR=1.2
Pod undocumented
382 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_node_list: not called CC=1 SCAR=1.2
Pod undocumented
383 0 $self->_compile_children( $node, $frame );
384 }
385  
386 42959 ▶sub compile_literal {
Subroutine compile_literal: called CC=8 SCAR=29.1
Pod undocumented
387 0 ▶ my ( $self, $node ) = @_;
Subroutine compile_literal: not called CC=8 SCAR=29.1
Pod undocumented
388 0 my $v = $node->value;
389  
390 0 ▶ if ( !defined $v ) {
Branch: 8 branches
BranchTrueFalseTotal
if (not defined $v) { } 216 42743 42959
elsif (builtin::is_bool($v)) { } 808 41935 42743
elsif (created_as_number $v) { } 14172 27763 41935
elsif (ref $v eq 'HASH' and exists $v->{'regex'}) { } 151 27612 27763
if (not defined $v) { } 0 0 0
elsif (builtin::is_bool($v)) { } 0 0 0
elsif (created_as_number $v) { } 0 0 0
elsif (ref $v eq 'HASH' and exists $v->{'regex'}) { } 0 0 0
391 0 $self->_emit('$runtime->None'); # JS null
392 }
393 elsif ( is_bool($v) ) {
394 0 ▶ $self->_emit( $v ? '!!1' : '!!0' ); # JS boolean (is_bool-flagged)
Branch: 2 branches
BranchTrueFalseTotal
$v ? : 659 149 808
$v ? : 0 0 0
395 }
396 elsif ( created_as_number($v) ) {
397 # node.value.toString(): ADR 0007's shortest round-trip form, not Perl's
398 # "$v" (which collapses e.g. 0.30000000000000004 to 0.3). The result is a
399 # valid Perl numeric literal for every finite double a number literal can
400 # hold (NaN/Infinity cannot reach here from the lexer).
401 0 $self->_emit( Text::Nunjucks::Lib->number_to_string($v) );
402 }
403 ▶ elsif ( ref($v) eq 'HASH' && exists $v->{regex} ) {
Condition33%2/6
ref $v eq "HASH" and exists $v->{"regex"}
!ll&&!rl&&r
276120151
ref $v eq "HASH" and exists $v->{"regex"}
!ll&&!rl&&r
000
MC/DC50%1/2
ref $v eq "HASH" and exists $v->{"regex"}
ref $v eq "HASH" exists $v->{"regex"}
MC/DC0%0/2
ref $v eq "HASH" and exists $v->{"regex"}
ref $v eq "HASH" exists $v->{"regex"}
Truth table66%2/3
A and B
A= ref $v eq "HASH"B= exists $v->{"regex"}
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= ref $v eq "HASH"B= exists $v->{"regex"}
ABresult
0X0
100
111
404 # regex literal -- k7 / ADR 0013 open, runtime support not wired yet.
405 $self->_emit( '$runtime->make_regex('
406 . $self->_quote( $v->{regex} ) . ', '
407 0 ▶ . $self->_quote( defined $v->{flags} ? $v->{flags} : '' )
Branch: 2 branches
BranchTrueFalseTotal
defined $v->{'flags'} ? : 151 0 151
defined $v->{'flags'} ? : 0 0 0
408 . ')' );
409 }
410 else {
411 0 $self->_emit( $self->_quote($v) ); # string
412 }
413 }
414  
415 16323 ▶sub compile_symbol {
Subroutine compile_symbol: called CC=2 SCAR=9.2
Pod undocumented
416 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_symbol: not called CC=2 SCAR=9.2
Pod undocumented
417 0 my $name = $node->value;
418 0 my $v = $frame->lookup($name);
419  
420 0 ▶ if ($v) {
Branch: 2 branches
BranchTrueFalseTotal
if ($v) { } 3238 13085 16323
if ($v) { } 0 0 0
421 0 $self->_emit( '$' . $v );
422 }
423 else {
424 0 $self->_emit( '$runtime->context_or_frame_lookup($context, $frame, '
425 . $self->_quote($name) . ')' );
426 }
427 }
428  
429 563 ▶sub compile_group {
Subroutine compile_group: called CC=1 SCAR=1.2
Pod undocumented
430 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_group: not called CC=1 SCAR=1.2
Pod undocumented
431 0 $self->_compile_aggregate( $node, $frame, '(', ')' );
432 }
433  
434 2064 ▶sub compile_array {
Subroutine compile_array: called CC=1 SCAR=1.2
Pod undocumented
435 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_array: not called CC=1 SCAR=1.2
Pod undocumented
436 0 $self->_compile_aggregate( $node, $frame, '[', ']' );
437 }
438  
439 # Template dict literals build a $runtime->make_dict(k1, v1, ...), a
440 # Hash::Ordered, so insertion order survives (ADR 0011). This diverges from
441 # upstream's `{k: v}` JS-object literal only in the representation.
442 792 ▶sub compile_dict {
Subroutine compile_dict: called CC=3 SCAR=14.2
Pod undocumented
443 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_dict: not called CC=3 SCAR=14.2
Pod undocumented
444 0 $self->_emit('$runtime->make_dict(');
445 0 my $children = $node->children;
446 0 for my $i ( 0 .. $#$children ) {
447 0 ▶ $self->_emit(', ') if $i > 0;
Branch: 2 branches
BranchTrueFalseTotal
if $i > 0 400 745 1145
if $i > 0 0 0 0
448 0 $self->compile( $children->[$i], $frame );
449 }
450 0 $self->_emit(')');
451 }
452  
453 2341 ▶sub compile_pair {
Subroutine compile_pair: called CC=4 SCAR=18.6
Pod undocumented
454 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_pair: not called CC=4 SCAR=18.6
Pod undocumented
455 0 my $key = $node->key;
456 0 my $val = $node->value;
457  
458 0 ▶ if ( $key->isa( $self->node('Symbol') ) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($key->isa($self->node('Symbol'))) { } 1612 729 2341
elsif (not $key->isa($self->node('Literal')) && 'Text::Nunjucks::Lib'->is_string($key->value)) { } 5 724 729
if ($key->isa($self->node('Symbol'))) { } 0 0 0
elsif (not $key->isa($self->node('Literal')) && 'Text::Nunjucks::Lib'->is_string($key->value)) { } 0 0 0
459 0 $key = $self->node('Literal')->new( $key->lineno, $key->colno, $key->value );
460 }
461 ▶ elsif (
Condition33%2/6
$key->isa($self->node('Literal')) && 'Text::Nunjucks::Lib'->is_string($key->value)
!ll&&!rl&&r
05724
$key->isa($self->node('Literal')) && 'Text::Nunjucks::Lib'->is_string($key->value)
!ll&&!rl&&r
000
MC/DC50%1/2
$key->isa($self->node('Literal')) && 'Text::Nunjucks::Lib'->is_string($key->value)
$key->isa($self->node('Literal')) 'Text::Nunjucks::Lib'->is_string($key->value)
MC/DC0%0/2
$key->isa($self->node('Literal')) && 'Text::Nunjucks::Lib'->is_string($key->value)
$key->isa($self->node('Literal')) 'Text::Nunjucks::Lib'->is_string($key->value)
Truth table66%2/3
A && B
A= $key->isa($self->node('Literal'))B= 'Text::Nunjucks::Lib'->is_string($key->value)
ABresult
0X0
100
111
Truth table0%0/3
A && B
A= $key->isa($self->node('Literal'))B= 'Text::Nunjucks::Lib'->is_string($key->value)
ABresult
0X0
100
111
462 !( $key->isa( $self->node('Literal') )
463 && Text::Nunjucks::Lib->is_string( $key->value ) )
464 )
465 {
466 0 $self->fail( 'compilePair: Dict keys must be strings or names',
467 $key->lineno, $key->colno );
468 }
469  
470 0 $self->compile( $key, $frame );
471 0 $self->_emit(', '); # was ': ' -- key, value pairs for make_dict / hashref
472 0 $self->_compile_expression( $val, $frame );
473 }
474  
475 140 ▶sub compile_inline_if {
Subroutine compile_inline_if: called CC=2 SCAR=9.2
Pod undocumented
476 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_inline_if: not called CC=2 SCAR=9.2
Pod undocumented
477 0 $self->_emit('($runtime->_truthy(');
478 0 $self->compile( $node->cond, $frame );
479 0 $self->_emit(') ? ');
480 0 $self->compile( $node->body, $frame );
481 0 $self->_emit(' : ');
482 0 ▶ if ( defined $node->else_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $node->else_) { } 37 103 140
if (defined $node->else_) { } 0 0 0
483 0 $self->compile( $node->else_, $frame );
484 }
485 else {
486 0 $self->_emit("''");
487 }
488 0 $self->_emit(')');
489 }
490  
491 43 ▶sub compile_in {
Subroutine compile_in: called CC=1 SCAR=1.2
Pod undocumented
492 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_in: not called CC=1 SCAR=1.2
Pod undocumented
493 0 $self->_emit('$runtime->in_operator(');
494 0 $self->compile( $node->left, $frame );
495 0 $self->_emit(', ');
496 0 $self->compile( $node->right, $frame );
497 0 $self->_emit(')');
498 }
499  
500 278 ▶sub compile_is {
Subroutine compile_is: called CC=5 SCAR=22.2
Pod undocumented
501 0 ▶ my ( $self, $node, $frame ) = @_;
Subroutine compile_is: not called CC=5 SCAR=22.2
Pod undocumented
502 # name of the test function: the callable's name (a Filter/FunCall), else
503 # the operand value. JS reads a missing property as undefined (hence the
504 # can() guard: a bare Symbol has no name/args accessor) and then coerces the
505 # value into the test-name string -- crucially `x is none` parses `none` as
506 # a Literal(null), and `"env.getTest(\"" + null + "\")"` yields getTest("null")
507 # (upstream registers the test under `null`). _literal_to_string reproduces
508 # that JS ToString (null -> "null", numbers/bools too).
509 0 ▶ my $right_val =
Branch: 2 branches
BranchTrueFalseTotal
$node->right->can('name') && $node->right->name ? : 52 226 278
$node->right->can('name') && $node->right->name ? : 0 0 0
Condition33%2/6
$node->right->can("name") and $node->right->name
!ll&&!rl&&r
226052
$node->right->can("name") and $node->right->name
!ll&&!rl&&r
000
MC/DC50%1/2
$node->right->can("name") and $node->right->name
$node->right->can("name") $node->right->name
MC/DC0%0/2
$node->right->can("name") and $node->right->name
$node->right->can("name") $node->right->name
Truth table66%2/3
A and B
A= $node->right->can("name")B= $node->right->name
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $node->right->can("name")B= $node->right->name
ABresult
0X0
100
111
510 ( $node->right->can('name') && $node->right->name )
511 ? $node->right->name->value
512 : $node->right->value;
513 0 my $right = $self->_literal_to_string($right_val);
514 # env.getTest(name).call(context, left[, args]) === true. Per ADR 0009 the
515 # Perl test receives (@args) with no `this`, and === is op_seq.
516 0 $self->_emit(
517 '$runtime->op_seq($env->get_test(' . $self->_quote($right) . ')->(' );
518 0 $self->compile( $node->left, $frame );
519 0 ▶ if ( $node->right->can('args') && $node->right->args ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->right->can("args") and $node->right->args) 52 225 277
if ($node->right->can("args") and $node->right->args) 0 0 0
Condition33%2/6
$node->right->can("args") and $node->right->args
!ll&&!rl&&r
225052
$node->right->can("args") and $node->right->args
!ll&&!rl&&r
000
MC/DC50%1/2
$node->right->can("args") and $node->right->args
$node->right->can("args") $node->right->args
MC/DC0%0/2
$node->right->can("args") and $node->right->args
$node->right->can("args") $node->right->args
Truth table66%2/3
A and B
A= $node->right->can("args")B= $node->right->args
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= $node->right->can("args")B= $node->right->args
ABresult
0X0
100
111
520 0 $self->_emit(', ');
521 0 $self->compile( $node->right->args, $frame );
522 }
523 0 $self->_emit('), !!1)');
524 }
525  
526 # Every operator is emitted as a Runtime op_* call (never a native Perl
527 # operator, which lacks JS semantics). The AST already encodes precedence, and
528 # each operand is a full sub-expression inside the call's parens, so no
529 # parenthesisation is needed. compileCompare is the exception (see below).
530 2681 ▶sub _bin_op_emitter {
Subroutine _bin_op_emitter: called CC=1 SCAR=1.2
531 0 ▶ my ( $self, $node, $frame, $op ) = @_;
Subroutine _bin_op_emitter: not called CC=1 SCAR=1.2
532 0 $self->_emit( '$runtime->' . $op . '(' );
533 0 $self->compile( $node->left, $frame );
534 0 $self->_emit(', ');
535 0 $self->compile( $node->right, $frame );
536 0 $self->_emit(')');
537 }
538  
539 11 ▶sub compile_or { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_or' ) }
Subroutine compile_or: called CC=1 SCAR=0.0
Pod undocumented
540 7 ▶sub compile_and { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_and' ) }
Subroutine compile_and: called CC=1 SCAR=0.0
Pod undocumented
541 71 ▶sub compile_add { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_add' ) }
Subroutine compile_add: called CC=1 SCAR=0.0
Pod undocumented
542 135 ▶sub compile_concat { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_concat' ) }
Subroutine compile_concat: called CC=1 SCAR=0.0
Pod undocumented
543 1922 ▶sub compile_sub { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_sub' ) }
Subroutine compile_sub: called CC=1 SCAR=0.0
Pod undocumented
544 62 ▶sub compile_mul { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_mul' ) }
Subroutine compile_mul: called CC=1 SCAR=0.0
Pod undocumented
545 404 ▶sub compile_div { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_div' ) }
Subroutine compile_div: called CC=1 SCAR=0.0
Pod undocumented
546 37 ▶sub compile_mod { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_mod' ) }
Subroutine compile_mod: called CC=1 SCAR=0.0
Pod undocumented
547  
548 123 ▶sub compile_not {
Subroutine compile_not: called CC=1 SCAR=0.0
Pod undocumented
549 123 my ( $self, $node, $frame ) = @_;
550 123 $self->_emit('$runtime->op_not(');
551 123 $self->compile( $node->target, $frame );
552 122 $self->_emit(')');
553 }
554  
555 4 ▶sub compile_floor_div { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_floordiv' ) }
Subroutine compile_floor_div: called CC=1 SCAR=0.0
Pod undocumented
556 28 ▶sub compile_pow { $_[0]->_bin_op_emitter( $_[1], $_[2], 'op_pow' ) }
Subroutine compile_pow: called CC=1 SCAR=0.0
Pod undocumented
557  
558 389 ▶sub compile_neg {
Subroutine compile_neg: called CC=1 SCAR=0.0
Pod undocumented
559 389 my ( $self, $node, $frame ) = @_;
560 389 $self->_emit('$runtime->op_neg(');
561 389 $self->compile( $node->target, $frame );
562 389 $self->_emit(')');
563 }
564  
565 0 ▶sub compile_pos {
Subroutine compile_pos: not called CC=1 SCAR=6.9
Pod undocumented
566 0 my ( $self, $node, $frame ) = @_;
567 0 $self->_emit('$runtime->op_pos(');
568 0 $self->compile( $node->target, $frame );
569 0 $self->_emit(')');
570 }
571  
572 16 my %CMP_OP = (
573 '==' => 'op_eq', '===' => 'op_seq',
574 '!=' => 'op_ne', '!==' => 'op_sne',
575 '<' => 'op_lt', '>' => 'op_gt',
576 '<=' => 'op_le', '>=' => 'op_ge',
577 );
578 # JS operator precedence: relational (< > <= >=) binds tighter than equality
579 # (== != === !==), both left-associative.
580 16 my %CMP_PREC = (
581 '==' => 1, '===' => 1, '!=' => 1, '!==' => 1,
582 '<' => 2, '>' => 2, '<=' => 2, '>=' => 2,
583 );
584  
585 # Upstream emits native infix comparison operators and relies on JS precedence
586 # (`2 == 1 < 5` -> `2 == (1 < 5)`). op_* runtime calls have no infix form, so
587 # we rebuild that precedence as nested calls via precedence climbing. A single
588 # comparison (the overwhelmingly common case) reduces to one op_* call.
589 1982 ▶sub compile_compare {
Subroutine compile_compare: called CC=2 SCAR=6.9
Pod undocumented
590 1982 my ( $self, $node, $frame ) = @_;
591 1982 my @toks = ( { operand => $node->expr } );
592 1982 for my $op ( @{ $node->ops } ) {
593 1988 push @toks, { op => $op->type }, { operand => $op->expr };
594 }
595 1982 my $pos = 0;
596 1982 my $tree = $self->_compare_climb( \@toks, \$pos, 0 );
597 1982 $self->_emit_compare( $tree, $frame );
598 }
599  
600 3970 ▶sub _compare_climb {
Subroutine _compare_climb: called CC=4 SCAR=13.9
601 3970 my ( $self, $toks, $pos, $min_prec ) = @_;
602 3970 my $lhs = { leaf => $toks->[$$pos]{operand} };
603 3970 $$pos++;
604 3970 ▶ while ( $$pos < @$toks
Branch: 1 branch
BranchTrueFalseTotal
if ($$pos < @$toks and exists $$toks[$$pos]{"op"} and $CMP_PREC{$$toks[$$pos]{"op"}} >= $min_prec) 1988 3970 5958
Condition83%5/6
$$pos < @$toks and exists $$toks[$$pos]{"op"} and $CMP_PREC{$$toks[$$pos]{"op"}} >= $min_prec
!ll&&!rl&&r
396821988
$$pos < @$toks and exists $$toks[$$pos]{"op"}
!ll&&!rl&&r
396801990
MC/DC66%2/3
$$pos < @$toks and exists $$toks[$$pos]{"op"} and $CMP_PREC{$$toks[$$pos]{"op"}} >= $min_prec
$$pos < @$toks exists $$toks[$$pos]{"op"} $CMP_PREC{$$toks[$$pos]{"op"}} >= $min_prec
Truth table75%3/4
A and B and C
A= $$pos < @$toksB= exists $$toks[$$pos]{"op"}C= $CMP_PREC{$$toks[$$pos]{"op"}} >= $min_prec
ABCresult
0XX0
10X0
1100
1111
605 && exists $toks->[$$pos]{op}
606 && $CMP_PREC{ $toks->[$$pos]{op} } >= $min_prec )
607 {
608 1988 my $op = $toks->[$$pos]{op};
609 1988 $$pos++; # consume operator
610 1988 my $rhs = $self->_compare_climb( $toks, $pos, $CMP_PREC{$op} + 1 );
611 1988 $lhs = { op => $op, left => $lhs, right => $rhs };
612 }
613 3970 return $lhs;
614 }
615  
616 5958 ▶sub _emit_compare {
Subroutine _emit_compare: called CC=2 SCAR=6.9
617 5958 my ( $self, $tree, $frame ) = @_;
618 5958 ▶ if ( exists $tree->{leaf} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $tree->{"leaf"}) 3970 1988 5958
619 3970 $self->compile( $tree->{leaf}, $frame );
620 3970 return;
621 }
622 1988 $self->_emit( '$runtime->' . $CMP_OP{ $tree->{op} } . '(' );
623 1988 $self->_emit_compare( $tree->{left}, $frame );
624 1988 $self->_emit(', ');
625 1988 $self->_emit_compare( $tree->{right}, $frame );
626 1988 $self->_emit(')');
627 }
628  
629 1522 ▶sub compile_lookup_val {
Subroutine compile_lookup_val: called CC=3 SCAR=11.0
Pod undocumented
630 1522 my ( $self, $node, $frame, $seam ) = @_;
631 # ADR 0016: the seam is the property-read one by default
632 # ($runtime->member_lookup); FunCall hands us 'method' so a blessed-object
633 # member comes back as a callable for call_wrap. compile_fun_call is the
634 # only caller that flips the seam.
635 1522 ▶ my $sym = ( $seam && $seam eq 'method' )
Branch: 1 branch
BranchTrueFalseTotal
$seam && $seam eq 'method' ? : 838 684 1522
Condition66%2/3
$seam and $seam eq "method"
!ll&&!rl&&r
6840838
MC/DC50%1/2
$seam and $seam eq "method"
$seam $seam eq "method"
Truth table66%2/3
A and B
A= $seamB= $seam eq "method"
ABresult
0X0
100
111
636 ? '$runtime->member_lookup_method(('
637 : '$runtime->member_lookup((';
638 1522 $self->_emit($sym);
639 1522 $self->_compile_expression( $node->target, $frame );
640 1522 $self->_emit('), ');
641 1522 $self->_compile_expression( $node->val, $frame );
642 1522 $self->_emit(')');
643 }
644  
645 3581 ▶sub _get_node_name {
Subroutine _get_node_name: called CC=5 SCAR=16.1
646 3581 my ( $self, $node ) = @_;
647 3581 my $t = $node->typename;
648 3581 ▶ if ( $t eq 'Symbol' ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($t eq 'Symbol') { } 1628 1953 3581
elsif ($t eq 'FunCall') { } 10 1943 1953
elsif ($t eq 'LookupVal') { } 851 1092 1943
elsif ($t eq 'Literal') { } 927 165 1092
649 1628 return $node->value;
650 }
651 elsif ( $t eq 'FunCall' ) {
652 10 return 'the return value of (' . $self->_get_node_name( $node->name ) . ')';
653 }
654 elsif ( $t eq 'LookupVal' ) {
655 return
656 851 $self->_get_node_name( $node->target ) . '["'
657 . $self->_get_node_name( $node->val ) . '"]';
658 }
659 elsif ( $t eq 'Literal' ) {
660 927 return $self->_literal_to_string( $node->value ); # node.value.toString()
661 }
662 else {
663 165 return '--expression--';
664 }
665 }
666  
667 1204 ▶sub _literal_to_string {
Subroutine _literal_to_string: called CC=6 SCAR=18.3
668 1204 my ( $self, $v ) = @_;
669 1204 ▶ return 'null' if !defined $v; # avoid a null.toString() crash (rare)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 15 1189 1204
670 1189 ▶ return ( $v ? 'true' : 'false' ) if is_bool($v);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($v) 0 1189 1189
$v ? : 0 0 0
671 1189 ▶ return Text::Nunjucks::Lib->number_to_string($v) if created_as_number($v);
Branch: 1 branch
BranchTrueFalseTotal
if created_as_number $v 4 1185 1189
672 1185 ▶ return $v unless ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 1161 24 1185
673 24 return '[object Object]';
674 }
675  
676 1869 ▶sub compile_fun_call {
Subroutine compile_fun_call: called CC=2 SCAR=6.9
Pod undocumented
677 1869 my ( $self, $node, $frame ) = @_;
678 # Keep line/col at runtime: a JS `(x, y, z)` returns z; the Perl equivalent
679 # that runs the assignments then returns the last value in any context is
680 # `do { ...; EXPR }`.
681 1869 $self->_emit( 'do { $lineno = '
682 . $self->_num_or_undef( $node->lineno )
683 . '; $colno = '
684 . $self->_num_or_undef( $node->colno )
685 . '; ' );
686  
687 1869 $self->_emit('$runtime->call_wrap(');
688  
689 # ADR 0016: the callable argument comes from the method-call seam
690 # (member_lookup_method), not the property-read one -- otherwise a
691 # blessed object's `obj.foo` would render its value rather than hand
692 # call_wrap something to invoke. A bare Symbol (foo()) goes through the
693 # normal expression compile (Frame/Context lookup yields the coderef).
694 1869 ▶ if ( $node->name->typename eq 'LookupVal' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->name->typename eq 'LookupVal') { } 838 1031 1869
695 838 $self->compile_lookup_val( $node->name, $frame, 'method' );
696 } else {
697 1031 $self->_compile_expression( $node->name, $frame );
698 }
699  
700 # Name of what we're calling, for friendly errors on a failed lookup.
701 1869 $self->_emit(
702 ', ' . $self->_quote( $self->_get_node_name( $node->name ) ) . ', $context, ' );
703  
704 1869 $self->_compile_aggregate( $node->args, $frame, '[', ']' );
705  
706 1869 $self->_emit(')'); # close call_wrap
707 1869 $self->_emit(' }'); # close do
708 }
709  
710 10406 ▶sub compile_filter {
Subroutine compile_filter: called CC=1 SCAR=0.0
Pod undocumented
711 10406 my ( $self, $node, $frame ) = @_;
712 10406 my $name = $node->name;
713 10406 $self->assert_type( $name, $self->node('Symbol') );
714 # ADR 0009: a sync filter receives (@args), no `this`/context.
715 10406 $self->_emit( '$env->get_filter(' . $self->_quote( $name->value ) . ')->(' );
716 10406 $self->_compile_aggregate( $node->args, $frame );
717 10402 $self->_emit(')');
718 }
719  
720 116 ▶sub compile_filter_async {
Subroutine compile_filter_async: called CC=1 SCAR=0.0
Pod undocumented
721 116 my ( $self, $node, $frame ) = @_;
722 116 my $name = $node->name;
723 116 my $symbol = $node->symbol->value;
724  
725 116 $self->assert_type( $name, $self->node('Symbol') );
726  
727 116 $frame->set( $symbol, $symbol );
728  
729 116 $self->_emit( '$env->get_filter(' . $self->_quote( $name->value ) . ')->(' );
730 116 $self->_compile_aggregate( $node->args, $frame );
731 116 $self->_emit_line( ', ' . $self->_make_callback($symbol) );
732  
733 116 $self->_add_scope_level;
734 }
735  
736 # kwargs are looked up by name, never iterated in order, so they stay a plain
737 # hashref (KwArgs), not the ordered dict compileDict builds (ADR 0011).
738 926 ▶sub compile_keyword_args {
Subroutine compile_keyword_args: called CC=3 SCAR=11.0
Pod undocumented
739 926 my ( $self, $node, $frame ) = @_;
740 926 $self->_emit('$runtime->make_keyword_args({');
741 926 my $children = $node->children;
742 926 for my $i ( 0 .. $#$children ) {
743 1196 ▶ $self->_emit(', ') if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 270 926 1196
744 1196 $self->compile( $children->[$i], $frame );
745 }
746 926 $self->_emit('})');
747 }
748  
749 2878 ▶sub compile_set {
Subroutine compile_set: called CC=6 SCAR=17.9
Pod undocumented
750 2878 my ( $self, $node, $frame ) = @_;
751 2878 my @ids;
752  
753 # Look up the variable names for each identifier, minting new ones if needed.
754 2878 for my $target ( @{ $node->targets } ) {
755 2885 my $name = $target->value;
756 2885 my $id = $frame->lookup($name);
757  
758 2885 ▶ if ( !defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $id) 2885 0 2885
759 2885 $id = $self->_tmpid;
760 # In JS `var id;` hoists to function scope even when created inside an
761 # `if`; the Perl tmpid never escapes its own block here, so a plain
762 # `my` at this point is correct.
763 2885 $self->_emit_line( 'my $' . $id . ';' );
764 }
765  
766 2885 push @ids, $id;
767 }
768  
769 2878 my $lhs = join( ' = ', map { '$' . $_ } @ids );
770  
771 2878 ▶ if ( $node->value ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->value) { } 2853 25 2878
772 2853 $self->_emit( $lhs . ' = ' );
773 2853 $self->_compile_expression( $node->value, $frame );
774 2853 $self->_emit_line(';');
775 }
776 else {
777 25 $self->_emit( $lhs . ' = ' );
778 25 $self->compile( $node->body, $frame );
779 25 $self->_emit_line(';');
780 }
781  
782 2878 my $targets = $node->targets;
783 2878 for my $i ( 0 .. $#$targets ) {
784 2885 my $id = $ids[$i];
785 2885 my $name = $targets->[$i]->value;
786  
787 2885 $self->_emit_line( '$frame->set(' . $self->_quote($name) . ', $' . $id . ', 1);' );
788  
789 2885 $self->_emit_line('if($frame->top_level) {');
790 2885 $self->_emit_line( '$context->set_variable(' . $self->_quote($name) . ', $' . $id . ');' );
791 2885 $self->_emit_line('}');
792  
793 2885 ▶ if ( substr( $name, 0, 1 ) ne '_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($name, 0, 1) ne "_") 2690 195 2885
794 2690 $self->_emit_line('if($frame->top_level) {');
795 2690 $self->_emit_line( '$context->add_export(' . $self->_quote($name) . ', $' . $id . ');' );
796 2690 $self->_emit_line('}');
797 }
798 }
799 }
800  
801 # JS `switch` matches with === and falls through until a break. Perl has no
802 # such construct, so this emulates it with a run-once bare block: op_seq for
803 # ===, a matched-flag for fall-through, and `last` for break. `default` sits
804 # last and runs whenever execution reaches it (no break stopped us), exactly
805 # like JS. (Structural divergence: Perl has no switch/fall-through.)
806 13 ▶sub compile_switch {
Subroutine compile_switch: called CC=4 SCAR=13.9
Pod undocumented
807 13 my ( $self, $node, $frame ) = @_;
808 13 my $expr = $self->_tmpid;
809 13 my $matched = $self->_tmpid;
810  
811 13 $self->_emit_line('{');
812 13 $self->_emit( 'my $' . $expr . ' = ' );
813 13 $self->compile( $node->expr, $frame );
814 13 $self->_emit_line(';');
815 13 $self->_emit_line( 'my $' . $matched . ' = 0;' );
816  
817 13 for my $c ( @{ $node->cases } ) {
818 24 $self->_emit( 'if ($' . $matched . ' || $runtime->op_seq($' . $expr . ', ' );
819 24 $self->compile( $c->cond, $frame );
820 24 $self->_emit_line(')) {');
821 24 $self->_emit_line( '$' . $matched . ' = 1;' );
822 24 $self->compile( $c->body, $frame );
823 # preserve fall-throughs
824 24 ▶ if ( @{ $c->body->children } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$c->body->children;}) 19 5 24
825 19 $self->_emit_line('last;');
826 }
827 24 $self->_emit_line('}');
828 }
829  
830 13 ▶ if ( $node->default ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->default) 7 6 13
831 7 $self->compile( $node->default, $frame );
832 }
833  
834 13 $self->_emit_line('}');
835 }
836  
837 2048 ▶sub compile_if {
Subroutine compile_if: called CC=3 SCAR=11.0
Pod undocumented
838 2048 my ( $self, $node, $frame, $async ) = @_;
839 2048 $self->_emit('if($runtime->_truthy(');
840 2048 $self->_compile_expression( $node->cond, $frame );
841 2048 $self->_emit_line(')) {');
842  
843 $self->_with_scoped_syntax(
844 2048 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
845 2048 $self->compile( $node->body, $frame );
846  
847 2048 ▶ $self->_emit('$cb->();') if $async;
Branch: 1 branch
BranchTrueFalseTotal
if $async 14 2034 2048
848 }
849 2048 );
850  
851 2048 ▶ if ( $node->else_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($node->else_) { } 44 2004 2048
elsif ($async) { } 3 2001 2004
852 44 $self->_emit_line("}\nelse {");
853  
854 $self->_with_scoped_syntax(
855 44 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
856 44 $self->compile( $node->else_, $frame );
857  
858 44 ▶ $self->_emit('$cb->();') if $async;
Branch: 1 branch
BranchTrueFalseTotal
if $async 11 33 44
859 }
860 44 );
861 }
862 elsif ($async) {
863 3 $self->_emit_line("}\nelse {");
864 3 $self->_emit('$cb->();');
865 }
866  
867 2048 $self->_emit_line('}');
868 }
869  
870 14 ▶sub compile_if_async {
Subroutine compile_if_async: called CC=1 SCAR=0.0
Pod undocumented
871 14 my ( $self, $node, $frame ) = @_;
872 14 $self->_emit('(sub { my ($cb) = @_;');
873 14 $self->compile_if( $node, $frame, 1 );
874 14 $self->_emit( '})->(' . $self->_make_callback() );
875 14 $self->_add_scope_level;
876 }
877  
878 1731 ▶sub _emit_loop_bindings {
Subroutine _emit_loop_bindings: called CC=2 SCAR=6.9
879 1731 my ( $self, $node, $arr, $i, $len ) = @_;
880 1731 my @bindings = (
881 [ 'index', '$' . $i . ' + 1' ],
882 [ 'index0', '$' . $i ],
883 [ 'revindex', '$' . $len . ' - $' . $i ],
884 [ 'revindex0', '$' . $len . ' - $' . $i . ' - 1' ],
885 [ 'first', '($' . $i . ' == 0)' ],
886 [ 'last', '($' . $i . ' == $' . $len . ' - 1)' ],
887 [ 'length', '$' . $len ],
888 );
889  
890 1731 for my $b (@bindings) {
891 12117 $self->_emit_line(
892 '$frame->set("loop.' . $b->[0] . '", ' . $b->[1] . ');' );
893 }
894 }
895  
896 808 ▶sub compile_for {
Subroutine compile_for: called CC=4 SCAR=13.9
Pod undocumented
897 808 my ( $self, $node, $frame ) = @_;
898  
899 808 my $i = $self->_tmpid;
900 808 my $len = $self->_tmpid;
901 808 my $arr = $self->_tmpid;
902 808 $frame = $frame->push;
903  
904 808 $self->_emit_line('$frame = $frame->push;');
905  
906 808 $self->_emit( 'my $' . $arr . ' = ' );
907 808 $self->_compile_expression( $node->arr, $frame );
908 806 $self->_emit_line(';');
909  
910 # `len` is used by the else_ check after the if(arr) block; JS var-hoists it,
911 # Perl needs it declared here so it survives past the branch.
912 806 $self->_emit_line( 'my $' . $len . ';' );
913  
914 # forward note (k14 audit): JS-truthiness guard, not `if defined`, so a null
915 # iterable never reaches keys()/deref.
916 806 $self->_emit( 'if($runtime->_truthy($' . $arr . ')) {' );
917 806 $self->_emit_line( '$' . $arr . ' = $runtime->from_iterator($' . $arr . ');' );
918  
919 806 ▶ if ( $node->name->isa( $self->node('Array') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->name->isa($self->node('Array'))) { } 170 636 806
920 170 $self->_emit_line( 'my $' . $i . ';' );
921  
922 # The object could be an array or object. Note that the body of the loop
923 # is duplicated for each condition, but we are optimizing for speed.
924 170 $self->_emit_line( 'if($runtime->is_array($' . $arr . ')) {' );
925 170 $self->_emit_line( '$' . $len . ' = scalar @{$' . $arr . '};' );
926 170 $self->_emit_line( 'for($' . $i . '=0; $' . $i . ' < scalar @{$' . $arr . '}; $' . $i . '++) {' );
927  
928 # Bind each declared var. arr is isArray-guarded, so arr[i] is a safe
929 # deref; the inner [u] goes through member_lookup (element may be short).
930 170 my $name_children = $node->name->children;
931 170 for my $u ( 0 .. $#$name_children ) {
932 344 my $child = $name_children->[$u];
933 344 my $tid = $self->_tmpid;
934 344 $self->_emit_line( 'my $' . $tid . ' = $runtime->member_lookup($' . $arr . '->[$' . $i . '], ' . $u . ');' );
935 344 $self->_emit_line( '$frame->set(' . $self->_quote( $child->value ) . ', $runtime->member_lookup($' . $arr . '->[$' . $i . '], ' . $u . '));' );
936 344 $frame->set( $name_children->[$u]->value, $tid );
937 }
938  
939 170 $self->_emit_loop_bindings( $node, $arr, $i, $len );
940 170 ▶ $self->_with_scoped_syntax( sub { $self->compile( $node->body, $frame ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
941 170 $self->_emit_line('}');
942  
943 170 $self->_emit_line('} else {');
944 # Iterate over the key/values of an object.
945 170 my ( $key, $val ) = @{ $node->name->children };
946 170 my $k = $self->_tmpid;
947 170 my $v = $self->_tmpid;
948 170 my $keys_id = $self->_tmpid;
949 170 $frame->set( $key->value, $k );
950 170 $frame->set( $val->value, $v );
951  
952 170 $self->_emit_line( '$' . $i . ' = -1;' );
953 170 $self->_emit_line( 'my $' . $keys_id . ' = $runtime->keys($' . $arr . ');' );
954 170 $self->_emit_line( '$' . $len . ' = scalar @{$' . $keys_id . '};' );
955 170 $self->_emit_line( 'for my $' . $k . ' (@{$' . $keys_id . '}) {' );
956 170 $self->_emit_line( '$' . $i . '++;' );
957 170 $self->_emit_line( 'my $' . $v . ' = $runtime->member_lookup($' . $arr . ', $' . $k . ');' );
958 170 $self->_emit_line( '$frame->set(' . $self->_quote( $key->value ) . ', $' . $k . ');' );
959 170 $self->_emit_line( '$frame->set(' . $self->_quote( $val->value ) . ', $' . $v . ');' );
960  
961 170 $self->_emit_loop_bindings( $node, $arr, $i, $len );
962 170 ▶ $self->_with_scoped_syntax( sub { $self->compile( $node->body, $frame ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
963 170 $self->_emit_line('}');
964  
965 170 $self->_emit_line('}');
966 }
967 else {
968 # Typical single-var iteration. Base may be an array or a string; length
969 # and element go through member_lookup so a non-array does not crash
970 # (JS `.length`/`[i]` tolerate it; a Perl deref would not).
971 636 my $v = $self->_tmpid;
972 636 $frame->set( $node->name->value, $v );
973  
974 # A string is indexed through its code units, split once rather than
975 # measured per element (ADR 0012); the loop below keeps upstream's shape.
976 636 $self->_emit_line( '$' . $arr . ' = $runtime->_for_code_units($' . $arr . ');' );
977 636 $self->_emit_line( '$' . $len . ' = $runtime->member_lookup($' . $arr . ', "length");' );
978 636 $self->_emit_line( 'for(my $' . $i . '=0; defined($' . $len . ') && $' . $i . ' < $' . $len . '; $' . $i . '++) {' );
979 636 $self->_emit_line( 'my $' . $v . ' = $runtime->member_lookup($' . $arr . ', $' . $i . ');' );
980 636 $self->_emit_line( '$frame->set(' . $self->_quote( $node->name->value ) . ', $' . $v . ');' );
981  
982 636 $self->_emit_loop_bindings( $node, $arr, $i, $len );
983  
984 636 ▶ $self->_with_scoped_syntax( sub { $self->compile( $node->body, $frame ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
985  
986 636 $self->_emit_line('}');
987 }
988  
989 806 $self->_emit_line('}');
990 806 ▶ if ( $node->else_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->else_) 10 796 806
991 10 $self->_emit_line( 'if (!$' . $len . ') {' );
992 10 $self->compile( $node->else_, $frame );
993 10 $self->_emit_line('}');
994 }
995  
996 806 $self->_emit_line('$frame = $frame->pop;');
997 }
998  
999 755 ▶sub _compile_async_loop {
Subroutine _compile_async_loop: called CC=7 SCAR=19.5
1000 755 my ( $self, $node, $frame, $parallel ) = @_;
1001 # Shares some code with compileFor. Iterates an object asynchronously.
1002  
1003 755 my $i = $self->_tmpid;
1004 755 my $len = $self->_tmpid;
1005 755 my $arr = $self->_tmpid;
1006 755 ▶ my $async_method = $parallel ? 'async_all' : 'async_each';
Branch: 1 branch
BranchTrueFalseTotal
$parallel ? : 53 702 755
1007 755 $frame = $frame->push;
1008  
1009 755 $self->_emit_line('$frame = $frame->push;');
1010  
1011 755 $self->_emit( 'my $' . $arr . ' = $runtime->from_iterator(' );
1012 755 $self->_compile_expression( $node->arr, $frame );
1013 755 $self->_emit_line(');');
1014  
1015 755 ▶ if ( $node->name->isa( $self->node('Array') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->name->isa($self->node('Array'))) { } 295 460 755
1016 295 my $array_len = scalar @{ $node->name->children };
1017 295 $self->_emit( '$runtime->' . $async_method . '($' . $arr . ', ' . $array_len . ', sub {' );
1018 295 $self->_emit('my (');
1019  
1020 295 for my $name ( @{ $node->name->children } ) {
1021 608 $self->_emit( '$' . $name->value . ', ' );
1022 }
1023  
1024 295 $self->_emit( '$' . $i . ', $' . $len . ', $next) = @_;' );
1025  
1026 295 for my $name ( @{ $node->name->children } ) {
1027 608 my $id = $name->value;
1028 608 $frame->set( $id, $id );
1029 608 $self->_emit_line( '$frame->set(' . $self->_quote($id) . ', $' . $id . ');' );
1030 }
1031 }
1032 else {
1033 460 my $id = $node->name->value;
1034 460 $self->_emit_line( '$runtime->' . $async_method . '($' . $arr . ', 1, sub { my ($' . $id . ', $' . $i . ', $' . $len . ', $next) = @_;' );
1035 460 $self->_emit_line( '$frame->set(' . $self->_quote($id) . ', $' . $id . ');' );
1036 460 $frame->set( $id, $id );
1037 }
1038  
1039 755 $self->_emit_loop_bindings( $node, $arr, $i, $len );
1040  
1041 $self->_with_scoped_syntax(
1042 755 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1043 755 my $buf;
1044 755 ▶ if ($parallel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parallel) 53 702 755
1045 53 $buf = $self->_push_buffer;
1046 }
1047  
1048 755 $self->compile( $node->body, $frame );
1049 755 ▶ $self->_emit_line(
Branch: 1 branch
BranchTrueFalseTotal
$buf ? : 53 702 755
1050 '$next->($' . $i . ( $buf ? ', $' . $buf : '' ) . ');' );
1051  
1052 755 ▶ if ($parallel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parallel) 53 702 755
1053 53 $self->_pop_buffer;
1054 }
1055 }
1056 755 );
1057  
1058 755 my $output = $self->_tmpid;
1059 755 $self->_emit_line( '}, ' . $self->_make_callback($output) );
1060 755 $self->_add_scope_level;
1061  
1062 755 ▶ if ($parallel) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parallel) 53 702 755
1063 53 $self->_emit_line( '$runtime->js_string_append($' . $self->buffer . ', $' . $output . ');' );
1064 }
1065  
1066 755 ▶ if ( $node->else_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($node->else_) 6 749 755
1067 # !arr.length via member_lookup, crash-safe for non-array iterables.
1068 6 $self->_emit_line( 'if (!$runtime->member_lookup($' . $arr . ', "length")) {' );
1069 6 $self->compile( $node->else_, $frame );
1070 6 $self->_emit_line('}');
1071 }
1072  
1073 755 $self->_emit_line('$frame = $frame->pop;');
1074 }
1075  
1076 702 ▶sub compile_async_each {
Subroutine compile_async_each: called CC=1 SCAR=0.0
Pod undocumented
1077 702 my ( $self, $node, $frame ) = @_;
1078 702 $self->_compile_async_loop( $node, $frame );
1079 }
1080  
1081 53 ▶sub compile_async_all {
Subroutine compile_async_all: called CC=1 SCAR=0.0
Pod undocumented
1082 53 my ( $self, $node, $frame ) = @_;
1083 53 $self->_compile_async_loop( $node, $frame, 1 );
1084 }
1085  
1086 # Port-only (ADR 0021), the statement in front of everything a template makes
1087 # that can be called after the render it was made in has delivered its result:
1088 # a macro, a call block (_compile_macro) and the body an extension is handed
1089 # (compile_call_extension). It tells the Context, so that the Context of a
1090 # template that included this one keeps it (Context _made_closure).
1091 1428 ▶sub _emit_made_closure {
Subroutine _emit_made_closure: called CC=1 SCAR=0.0
1092 1428 my ($self) = @_;
1093 1428 $self->_emit_line('$context->_made_closure;');
1094 }
1095  
1096 1418 ▶sub _compile_macro {
Subroutine _compile_macro: called CC=12 SCAR=24.8
1097 1418 my ( $self, $node, $frame ) = @_;
1098 1418 my @args;
1099 my $kwargs;
1100 1418 my $func_id = 'macro_' . $self->_tmpid;
1101 1418 my $keep_frame = defined($frame);
1102  
1103 # Type check the definition of the args.
1104 1418 my $arg_children = $node->args->children;
1105 1418 for my $i ( 0 .. $#$arg_children ) {
1106 636 my $arg = $arg_children->[$i];
1107 636 ▶ if ( $i == $#$arg_children && $arg->isa( $self->node('Dict') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == $arg_children->$#* and $arg->isa($self->node('Dict'))) { } 22 614 636
Condition100%3/3
$i == $arg_children->$#* and $arg->isa($self->node("Dict"))
!ll&&!rl&&r
24536922
MC/DC100%2/2
$i == $arg_children->$#* and $arg->isa($self->node("Dict"))
$i == $arg_children->$#* $arg->isa($self->node("Dict"))
Truth table100%3/3
A and B
A= $i == $arg_children->$#*B= $arg->isa($self->node("Dict"))
ABresult
0X0
100
111
1108 22 $kwargs = $arg;
1109 }
1110 else {
1111 614 $self->assert_type( $arg, $self->node('Symbol') );
1112 614 push @args, $arg;
1113 }
1114 }
1115  
1116 1418 my @real_names = ( ( map { 'l_' . $_->value } @args ), 'kwargs' );
1117  
1118 # Quoted argument names.
1119 1418 my @arg_names = map { $self->_quote( $_->value ) } @args;
1120 34 my @kwarg_names = map { $self->_quote( $_->key->value ) }
1121 1418 ▶ @{ ( $kwargs && $kwargs->children ) || [] };
Condition80%4/5
$kwargs && $kwargs->children || []
l!l
221396
$kwargs and $kwargs->children
!ll&&!rl&&r
1396022
MC/DC50%1/2
$kwargs && $kwargs->children || []
$kwargs $kwargs->children
Truth table66%2/3
A and B
A= $kwargsB= $kwargs->children
ABresult
111
0X0
100
1122  
1123 # A function passed to makeMacro destructures the arguments, so positional
1124 # args can be set with keyword args and keyword args passed positionally
1125 # (default values). See runtime.pm.
1126 1418 ▶ my $curr_frame =
Branch: 1 branch
BranchTrueFalseTotal
$keep_frame ? : 75 1343 1418
1127 $keep_frame ? $frame->push(1) : $self->frame_class->new;
1128  
1129 1418 $self->_emit_made_closure;
1130 $self->_emit_lines(
1131 'my $' . $func_id . ' = $runtime->make_macro(',
1132 '[' . join( ', ', @arg_names ) . '], ',
1133 '[' . join( ', ', @kwarg_names ) . '], ',
1134 'sub {',
1135 1418 ▶ 'my (' . join( ', ', map { '$' . $_ } @real_names ) . ') = @_;',
Branch: 1 branch
BranchTrueFalseTotal
$keep_frame ? : 75 1343 1418
1136 'my $callerFrame = $frame;',
1137 '$frame = '
1138 . ( $keep_frame ? '$frame->push(1);' : '$runtime->Frame->new;' ),
1139 '$kwargs = $kwargs || {};',
1140 'if (exists $kwargs->{\'caller\'}) {',
1141 '$frame->set("caller", $kwargs->{\'caller\'}); }'
1142 );
1143  
1144 # Expose the arguments to the template. No random names needed -- the sub
1145 # creates a new run-time scope.
1146 1418 for my $arg (@args) {
1147 614 $self->_emit_line(
1148 '$frame->set(' . $self->_quote( $arg->value ) . ', $l_' . $arg->value . ');' );
1149 614 $curr_frame->set( $arg->value, 'l_' . $arg->value );
1150 }
1151  
1152 # Expose the keyword arguments.
1153 1418 ▶ if ($kwargs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($kwargs) 22 1396 1418
1154 22 for my $pair ( @{ $kwargs->children } ) {
1155 34 my $name = $pair->key->value;
1156 34 $self->_emit( '$frame->set(' . $self->_quote($name) . ', ' );
1157 34 $self->_emit( 'exists $kwargs->{' . $self->_quote($name) . '}' );
1158 34 $self->_emit( ' ? $kwargs->{' . $self->_quote($name) . '} : ' );
1159 34 $self->_compile_expression( $pair->value, $curr_frame );
1160 34 $self->_emit(');');
1161 }
1162 }
1163  
1164 1418 my $buffer_id = $self->_push_buffer;
1165  
1166 1418 ▶ $self->_with_scoped_syntax( sub { $self->compile( $node->body, $curr_frame ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1167  
1168 1418 ▶ $self->_emit_line(
Branch: 1 branch
BranchTrueFalseTotal
$keep_frame ? : 75 1343 1418
1169 '$frame = ' . ( $keep_frame ? '$frame->pop;' : '$callerFrame;' ) );
1170 1418 $self->_emit_line( 'return $runtime->SafeString->new($' . $buffer_id . ');' );
1171 1418 $self->_emit_line('});');
1172 1418 $self->_pop_buffer;
1173  
1174 1418 return $func_id;
1175 }
1176  
1177 1343 ▶sub compile_macro {
Subroutine compile_macro: called CC=3 SCAR=11.0
Pod undocumented
1178 1343 my ( $self, $node, $frame ) = @_;
1179 1343 my $func_id = $self->_compile_macro($node);
1180  
1181 # Expose the macro to the templates.
1182 1343 my $name = $node->name->value;
1183 1343 $frame->set( $name, $func_id );
1184  
1185 1343 ▶ if ( $frame->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->parent) { } 8 1335 1343
1186 8 $self->_emit_line( '$frame->set(' . $self->_quote($name) . ', $' . $func_id . ');' );
1187 }
1188 else {
1189 1335 ▶ if ( substr( $node->name->value, 0, 1 ) ne '_' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($node->name->value, 0, 1) ne "_") 1330 5 1335
1190 1330 $self->_emit_line( '$context->add_export(' . $self->_quote($name) . ');' );
1191 }
1192 1335 $self->_emit_line( '$context->set_variable(' . $self->_quote($name) . ', $' . $func_id . ');' );
1193 }
1194 }
1195  
1196 75 ▶sub compile_caller {
Subroutine compile_caller: called CC=1 SCAR=0.0
Pod undocumented
1197 75 my ( $self, $node, $frame ) = @_;
1198 # Basically an anonymous "macro expression".
1199 75 $self->_emit('(sub {');
1200 75 my $func_id = $self->_compile_macro( $node, $frame );
1201 75 $self->_emit( 'return $' . $func_id . ';})->()' );
1202 }
1203  
1204 4325 ▶sub _compile_get_template {
Subroutine _compile_get_template: called CC=3 SCAR=11.0
1205 4325 my ( $self, $node, $frame, $eager_compile, $ignore_missing ) = @_;
1206 4325 my $parent_template_id = $self->_tmpid;
1207 4325 my $parent_name = $self->_template_name;
1208 4325 my $cb = $self->_make_callback($parent_template_id);
1209 4325 ▶ my $eager = $eager_compile ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
$eager_compile ? : 574 3751 4325
1210 4325 ▶ my $ignore = $ignore_missing ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
$ignore_missing ? : 11 4314 4325
1211 4325 $self->_emit('$env->get_template(');
1212 4325 $self->_compile_expression( $node->template, $frame );
1213 4325 $self->_emit_line( ', ' . $eager . ', ' . $parent_name . ', ' . $ignore . ', ' . $cb );
1214 4325 return $parent_template_id;
1215 }
1216  
1217 # Port-only (ADR 0021), a statement of an import's getExported callback, in
1218 # both branches of its error check. The macros of the imported template do not
1219 # hold the Context they were made in (see _emit_func_begin); the importing
1220 # Context does, from the third callback argument (Template _get_exported).
1221 #
1222 # A library that failed is kept like one that did not: it may have handed a
1223 # macro out before it failed -- into an array of the importing template, to a
1224 # function of the application -- and the importing template goes on after the
1225 # error with whatever follows the `if`, the `for`, the macro call or the `set`
1226 # block the import stands in.
1227 2186 ▶sub _keep_alive_statement {
Subroutine _keep_alive_statement: called CC=1 SCAR=0.0
1228 2186 my ($self) = @_;
1229 2186 return '$context->_keep_alive($_[2]);';
1230 }
1231  
1232 1093 ▶sub _emit_keep_alive {
Subroutine _emit_keep_alive: called CC=1 SCAR=0.0
1233 1093 my ($self) = @_;
1234 1093 $self->_emit_line( $self->_keep_alive_statement );
1235 }
1236  
1237 662 ▶sub compile_import {
Subroutine compile_import: called CC=3 SCAR=11.0
Pod undocumented
1238 662 my ( $self, $node, $frame ) = @_;
1239 662 my $target = $node->target->value;
1240 662 my $id = $self->_compile_get_template( $node, $frame, 0, 0 );
1241 662 $self->_add_scope_level;
1242  
1243 662 ▶ $self->_emit_line( '$' . $id . '->_get_exported('
Branch: 1 branch
BranchTrueFalseTotal
$node->withContext ? : 233 429 662
1244 . ( $node->withContext ? '$context->get_variables(), $frame, ' : '' )
1245 . $self->_make_callback( $id, 0, $self->_keep_alive_statement . ' ' ) );
1246 662 $self->_emit_keep_alive;
1247 662 $self->_add_scope_level;
1248  
1249 662 $frame->set( $target, $id );
1250  
1251 662 ▶ if ( $frame->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->parent) { } 2 660 662
1252 2 $self->_emit_line( '$frame->set(' . $self->_quote($target) . ', $' . $id . ');' );
1253 }
1254 else {
1255 660 $self->_emit_line( '$context->set_variable(' . $self->_quote($target) . ', $' . $id . ');' );
1256 }
1257 }
1258  
1259 431 ▶sub compile_from_import {
Subroutine compile_from_import: called CC=5 SCAR=16.1
Pod undocumented
1260 431 my ( $self, $node, $frame ) = @_;
1261 431 my $imported_id = $self->_compile_get_template( $node, $frame, 0, 0 );
1262 431 $self->_add_scope_level;
1263  
1264 431 ▶ $self->_emit_line( '$' . $imported_id . '->_get_exported('
Branch: 1 branch
BranchTrueFalseTotal
$node->withContext ? : 12 419 431
1265 . ( $node->withContext ? '$context->get_variables(), $frame, ' : '' )
1266 . $self->_make_callback( $imported_id, 0, $self->_keep_alive_statement . ' ' ) );
1267 431 $self->_emit_keep_alive;
1268 431 $self->_add_scope_level;
1269  
1270 431 for my $name_node ( @{ $node->names->children } ) {
1271 435 my ( $name, $alias );
1272 435 my $id = $self->_tmpid;
1273  
1274 435 ▶ if ( $name_node->isa( $self->node('Pair') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name_node->isa($self->node('Pair'))) { } 4 431 435
1275 4 $name = $name_node->key->value;
1276 4 $alias = $name_node->value->value;
1277 }
1278 else {
1279 431 $name = $name_node->value;
1280 431 $alias = $name;
1281 }
1282  
1283 # `var id` is used past the if/else, so hoist the my (JS var-hoist).
1284 435 $self->_emit_line( 'my $' . $id . ';' );
1285 435 $self->_emit_line( 'if(exists $' . $imported_id . '->{' . $self->_quote($name) . '}) {' );
1286 435 $self->_emit_line( '$' . $id . ' = $' . $imported_id . '->{' . $self->_quote($name) . '};' );
1287 435 $self->_emit_line('} else {');
1288 435 $self->_emit_line( '$cb->(' . $self->error_class . '->new(' . $self->_quote( "cannot import '" . $name . "'" ) . ')); return;' );
1289 435 $self->_emit_line('}');
1290  
1291 435 $frame->set( $alias, $id );
1292  
1293 435 ▶ if ( $frame->parent ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame->parent) { } 9 426 435
1294 9 $self->_emit_line( '$frame->set(' . $self->_quote($alias) . ', $' . $id . ');' );
1295 }
1296 else {
1297 426 $self->_emit_line( '$context->set_variable(' . $self->_quote($alias) . ', $' . $id . ');' );
1298 }
1299 }
1300 }
1301  
1302 1176 ▶sub compile_block {
Subroutine compile_block: called CC=3 SCAR=11.0
Pod undocumented
1303 1176 my ( $self, $node ) = @_;
1304 1176 my $id = $self->_tmpid;
1305  
1306 # Outside a block (a top-level block), don't execute its code -- it would
1307 # run twice (once here, once when the parent renders). See upstream comment.
1308 1176 ▶ if ( !$self->in_block ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->in_block) 1174 2 1176
1309 1174 $self->_emit(
1310 '($parentTemplate ? sub { my (undef, undef, undef, undef, $cb) = @_; $cb->(undef, ""); } : ' );
1311 }
1312 1176 $self->_emit( '$context->get_block(' . $self->_quote( $node->name->value ) . ')' );
1313 1176 ▶ if ( !$self->in_block ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->in_block) 1174 2 1176
1314 1174 $self->_emit(')');
1315 }
1316 1176 $self->_emit_line( '->($env, $context, $frame, $runtime, ' . $self->_make_callback( $id, 1 ) );
1317 1176 $self->_emit_line( '$runtime->js_string_append($' . $self->buffer . ', $' . $id . ');' );
1318 1176 $self->_add_scope_level(1);
1319 }
1320  
1321 266 ▶sub compile_super {
Subroutine compile_super: called CC=1 SCAR=0.0
Pod undocumented
1322 266 my ( $self, $node, $frame ) = @_;
1323 266 my $name = $node->blockName->value;
1324 266 my $id = $node->symbol->value;
1325  
1326 266 my $cb = $self->_make_callback( $id, 1 );
1327 266 $self->_emit_line( '$context->get_super($env, ' . $self->_quote($name) . ', $b_' . $name . ', $frame, $runtime, ' . $cb );
1328 266 $self->_emit_line( '$' . $id . ' = $runtime->mark_safe($' . $id . ');' );
1329 266 $self->_add_scope_level(1);
1330 266 $frame->set( $id, $id );
1331 }
1332  
1333 574 ▶sub compile_extends {
Subroutine compile_extends: called CC=1 SCAR=0.0
Pod undocumented
1334 574 my ( $self, $node, $frame ) = @_;
1335 574 my $k = $self->_tmpid;
1336  
1337 574 my $parent_template_id = $self->_compile_get_template( $node, $frame, 1, 0 );
1338  
1339 # extends is dynamic and can occur within a block like `if`, so capture the
1340 # parent template in the top-level scope.
1341 574 $self->_emit_line( '$parentTemplate = $' . $parent_template_id . ';' );
1342  
1343 574 $self->_emit_line( 'for my $' . $k . ' (keys %{$parentTemplate->blocks}) {' );
1344 574 $self->_emit_line( '$context->add_block($' . $k . ', $parentTemplate->blocks->{$' . $k . '});' );
1345 574 $self->_emit_line('}');
1346  
1347 574 $self->_add_scope_level;
1348 }
1349  
1350 2658 ▶sub compile_include {
Subroutine compile_include: called CC=1 SCAR=0.0
Pod undocumented
1351 2658 my ( $self, $node, $frame ) = @_;
1352 2658 $self->_emit_line('my @tasks;');
1353 2658 $self->_emit_line('push @tasks, sub {');
1354 2658 $self->_emit_line('my ($callback) = @_;');
1355 2658 my $id = $self->_compile_get_template( $node, $frame, 0, $node->ignoreMissing );
1356 2658 $self->_emit_line( '$callback->(undef, $' . $id . ');}));' );
1357 2658 $self->_emit_line('};');
1358  
1359 # Port-only (ADR 0021): the fourth argument of render, after the callback's
1360 # closers. The included template gets a Context of its own, and nothing
1361 # holds that once its render has delivered -- but a macro it made can be in
1362 # the hands of this template by then. So the included Context is told who
1363 # included it, and this Context takes it over when it has to (Template
1364 # render, Context _included_by).
1365 #
1366 # Port-only (ADR 0021) as well: `undef $callback;` in the error branch of
1367 # that callback. An error goes to `$cb` and not on through the waterfall, so
1368 # the waterfall's last callback -- the rest of this template, which holds
1369 # this Context until it is called -- is never called; upstream's is garbage
1370 # then. Here a macro of the included template can hold this callback (it is
1371 # behind the included template's `$cb`, which a macro containing an async
1372 # construct captures), and this Context keeps the included one, so the two
1373 # Contexts would hold each other for good. Once the callback has seen an
1374 # error it never needs `$callback` again: Template render drops every result
1375 # that comes after an error, and a second error takes this branch again.
1376 # The statement comes first because `$cb` may die.
1377 2658 my $id2 = $self->_tmpid;
1378 2658 $self->_emit_line('push @tasks, sub {');
1379 2658 $self->_emit_line('my ($template, $callback) = @_;');
1380 2658 $self->_emit_line( '$template->render($context->get_variables(), $frame, '
1381 . $self->_make_callback( $id2, 0, 'undef $callback; ' ) );
1382 2658 $self->_emit_line( '$callback->(undef, $' . $id2 . ');}), $context);' );
1383 2658 $self->_emit_line('};');
1384  
1385 2658 $self->_emit_line('push @tasks, sub {');
1386 2658 $self->_emit_line('my ($result, $callback) = @_;');
1387 2658 $self->_emit_line( '$runtime->js_string_append($' . $self->buffer . ', $result);' );
1388 2658 $self->_emit_line('$callback->(undef);');
1389 2658 $self->_emit_line('};');
1390 2658 $self->_emit_line( '$env->waterfall(\@tasks, ' . $self->_continuation_open );
1391 2658 $self->_add_scope_level;
1392 }
1393  
1394 0 ▶sub compile_template_data {
Subroutine compile_template_data: not called CC=1 SCAR=6.9
Pod undocumented
1395 0 my ( $self, $node, $frame ) = @_;
1396 0 $self->compile_literal( $node, $frame );
1397 }
1398  
1399 43 ▶sub compile_capture {
Subroutine compile_capture: called CC=1 SCAR=0.0
Pod undocumented
1400 43 my ( $self, $node, $frame ) = @_;
1401 # Temporarily override the buffer id as 'output' so the set block writes to
1402 # the capture output instead of the buffer.
1403 43 my $buffer = $self->buffer;
1404 43 $self->buffer('output');
1405 43 $self->_emit_line('(sub {');
1406 43 $self->_emit_line('my $output = "";');
1407 43 ▶ $self->_with_scoped_syntax( sub { $self->compile( $node->body, $frame ) } );
Subroutine __ANON__: called CC=1 SCAR=0.0
1408 43 $self->_emit_line('return $output;');
1409 43 $self->_emit_line('})->()');
1410 # Revert back to the old buffer id.
1411 43 $self->buffer($buffer);
1412 }
1413  
1414 27504 ▶sub compile_output {
Subroutine compile_output: called CC=7 SCAR=19.5
Pod undocumented
1415 27504 my ( $self, $node, $frame ) = @_;
1416 27504 my $buffer = $self->buffer;
1417 27504 for my $child ( @{ $node->children } ) {
1418 # TemplateData is never autoescaped, so simply output it for optimization.
1419 27504 ▶ if ( $child->isa( $self->node('TemplateData') ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($child->isa($self->node('TemplateData'))) { } 14840 12664 27504
1420 14840 my $val = $child->value;
1421 14840 ▶ if ( defined($val) && length($val) ) { # JS if(child.value): "" falsy, "0" truthy
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and length $val) 14837 3 14840
Condition66%2/3
defined $val and length $val
!ll&&!rl&&r
0314837
MC/DC50%1/2
defined $val and length $val
defined $val length $val
Truth table66%2/3
A and B
A= defined $valB= length $val
ABresult
0X0
100
111
1422 14837 $self->_emit( '$runtime->js_string_append($' . $buffer . ', ' );
1423 14837 $self->compile_literal($child);
1424 14837 $self->_emit_line(');');
1425 }
1426 }
1427 else {
1428 # forward note (k14 audit): the append goes through _js_to_string so a
1429 # raw value from suppress_value (autoescape off) uses number_to_string.
1430 # `buffer += ...` is js_string_append: it joins in code units (ADR 0012).
1431 12664 $self->_emit( '$runtime->js_string_append($' . $buffer
1432 . ', $runtime->_js_to_string($runtime->suppress_value(' );
1433 12664 ▶ $self->_emit('$runtime->ensure_defined(') if $self->throw_on_undefined;
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_on_undefined 41 12623 12664
1434 12664 $self->compile( $child, $frame );
1435 12660 ▶ $self->_emit( ', '
Branch: 1 branch
BranchTrueFalseTotal
if $self->throw_on_undefined 41 12619 12660
1436 . $self->_num_or_undef( $node->lineno ) . ', '
1437 . $self->_num_or_undef( $node->colno ) . ')' )
1438 if $self->throw_on_undefined;
1439 12660 $self->_emit( ", \$env->opts->{autoescape})));\n" );
1440 }
1441 }
1442 }
1443  
1444 9356 ▶sub compile_root {
Subroutine compile_root: called CC=6 SCAR=17.9
Pod undocumented
1445 9356 my ( $self, $node, $frame ) = @_;
1446 9356 ▶ if ($frame) {
Branch: 1 branch
BranchTrueFalseTotal
if ($frame) 0 9356 9356
1447 0 $self->fail("compileRoot: root node can't have frame");
1448 }
1449  
1450 9356 $frame = $self->frame_class->new;
1451  
1452 # Collect block names up front: they double as the forward-declared lexicals
1453 # ($b_<name>) the block subs are assigned to and the returned hashref reads.
1454 # The dedup check is upstream's ("defined more than once"), moved ahead of
1455 # emission so the forward declaration is well-formed (it throws either way,
1456 # same message).
1457 9356 my $blocks = $node->find_all( $self->node('Block') );
1458 9356 my @block_names;
1459 my %seen;
1460 9356 for my $block (@$blocks) {
1461 1181 my $name = $block->name->value;
1462 1181 ▶ if ( $seen{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{$name}) 3 1178 1181
1463 3 die $self->error_class->new(
1464 'Block "' . $name . '" defined more than once.' );
1465 }
1466 1178 $seen{$name} = 1;
1467 1178 push @block_names, $name;
1468 }
1469  
1470 $self->_emit_line( 'my ($root'
1471 9353 . join( '', map { ', $b_' . $_ } @block_names ) . ');' );
1472  
1473 9353 $self->_emit_func_begin( $node, 'root' );
1474 9353 $self->_emit_line('my $parentTemplate;');
1475 9353 $self->_compile_children( $node, $frame );
1476 9347 $self->_emit_line('if($parentTemplate) {');
1477 9347 $self->_emit_line('$parentTemplate->root_render_func($env, $context, $frame, $runtime, $cb);');
1478 9347 $self->_emit_line('} else {');
1479 9347 $self->_emit_line( '$cb->(undef, $' . $self->buffer . ');' );
1480 9347 $self->_emit_line('}');
1481 9347 $self->_emit_func_end(1);
1482  
1483 9347 $self->in_block(1);
1484  
1485 9347 for my $block (@$blocks) {
1486 1175 my $name = $block->name->value;
1487  
1488 1175 $self->_emit_func_begin( $block, 'b_' . $name );
1489  
1490 # Port-only (k109). Upstream's `function b_<name>(...)` is a named
1491 # function: inside its body the name is the function itself, and
1492 # compile_super passes it to get_super, which finds the block by identity.
1493 # A Perl anonymous sub has no name, and reading the file-level $b_<name>
1494 # from inside the sub would make the sub capture the variable that holds
1495 # it -- a reference cycle that keeps the compiled code of the template
1496 # alive for the life of the process. So every block sub binds its own name
1497 # per call; whatever the body emits as $b_<name>, at any closure depth,
1498 # reads this lexical. CORE:: because the source is eval'd with no feature
1499 # bundle in scope, and must not depend on who evals it.
1500 1175 $self->_emit_line( 'my $b_' . $name . ' = CORE::__SUB__;' );
1501  
1502 1175 my $tmp_frame = $self->frame_class->new;
1503 1175 $self->_emit_line('$frame = $frame->push(1);');
1504 1175 $self->compile( $block->body, $tmp_frame );
1505 1175 $self->_emit_func_end;
1506 }
1507  
1508 9347 $self->_emit_line('+{');
1509  
1510 9347 for my $block (@$blocks) {
1511 1175 my $block_name = 'b_' . $block->name->value;
1512 1175 $self->_emit_line( $block_name . ' => $' . $block_name . ',' );
1513 }
1514  
1515 9347 $self->_emit_line("root => \$root\n};");
1516 }
1517  
1518 # typename (CamelCase) -> compile_<snake_case> method name.
1519 127125 ▶sub _method_for {
Subroutine _method_for: called CC=1 SCAR=0.0
1520 127125 my ( $self, $typename ) = @_;
1521 127125 my $s = $typename;
1522 127125 $s =~ s/([a-z0-9])([A-Z])/$1_$2/g;
1523 127125 return 'compile_' . lc($s);
1524 }
1525  
1526 127125 ▶sub compile {
Subroutine compile: called CC=2 SCAR=7.4
Pod undocumented
1527 127125 my ( $self, $node, $frame ) = @_;
1528 127125 my $method = $self->_method_for( $node->typename );
1529 127125 ▶ if ( $self->can($method) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->can($method)) { } 127125 0 127125
1530 127125 $self->$method( $node, $frame );
1531 }
1532 else {
1533 0 $self->fail( 'compile: Cannot compile node: ' . $node->typename,
1534 $node->lineno, $node->colno );
1535 }
1536 }
1537  
1538 9347 ▶sub get_code {
Subroutine get_code: called CC=1 SCAR=0.0
Pod undocumented
1539 9347 my ($self) = @_;
1540 9347 return join( '', @{ $self->codebuf } );
1541 }
1542  
1543 16 1;
1544  
1545 __END__
« blib/lib/Text/Nunjucks/Compiler.pm ↑ Summary blib/lib/Text/Nunjucks/Loader/FileSystem.pm »