blib/lib/Text/Nunjucks/Runtime.pm

filter: Statementstmt 81.5% 659 / 808 Branchbran 78.2% 454 / 580 Conditioncond 71.7% 168 / 234 MC/DCmcdc 65.4% 91 / 139 Subroutinesub 88.5% 147 / 166 Podpod 0.0% 0 / 66 totaltotal 76.2% 1519 / 1993 CC 571 SCAR 30.1
CC
571
Cov
79%
member_lookup
137.6
member_lookup
137.6
make_regex
48.8
CRAP 20.3
« blib/lib/Text/Nunjucks/Runtime/Regex.pm ↑ Summary blib/lib/Text/Nunjucks/Lib/TemplateError.pm »
1 package Text::Nunjucks::Runtime;
2 # ABSTRACT: 1:1 port of Nunjucks src/runtime.js -- frames, macros, member lookup
3  
4 23 our $VERSION = '0.001';
5  
6 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 23 ▶use builtin qw( is_bool created_as_number true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 23 ▶use Scalar::Util qw( blessed refaddr weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 23 ▶use POSIX qw( floor fmod );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 23 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 23 ▶use Hash::Ordered;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 23 ▶use Text::Nunjucks::Lib;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 23 ▶use Text::Nunjucks::Lib::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 23 ▶use Text::Nunjucks::Lib::TemplateError;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 23 ▶use Text::Nunjucks::Runtime::None;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 23 ▶use Text::Nunjucks::Runtime::KwArgs;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 23 ▶use Text::Nunjucks::Runtime::Regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use Text::Nunjucks::Runtime::SafeString;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 ▶use Text::Nunjucks::Runtime::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 23 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # runtime.js does `require('./lib')` and re-exports several lib functions; the
25 # port mirrors that. runtime.js is class-method-only here (getty-perl-core
26 # "dispatch through the invocant", like Lib): every ported function is a method
27 # taking $self, the compiler-emitted code calls $runtime->member_lookup(...) &c,
28 # and cross-calls go through $self. No instances of Runtime itself.
29 #
30 # runtime.js declares two real classes (Frame, SafeString); each is a class in
31 # its own file (Text::Nunjucks::Runtime::Frame, a Moo class, and
32 # Text::Nunjucks::Runtime::SafeString, a plain package; ADR 0020), loaded
33 # above and named once by the resolvers below. Three port additions live at
34 # their ADR-designated home, here or beside it, and are marked as such:
35 # * None sentinel -- JS null (ADR 0006), Text::Nunjucks::Runtime::None
36 # * KwArgs marker -- __keywords trailing hashref (ADR 0009),
37 # Text::Nunjucks::Runtime::KwArgs
38 # * the JS operators + JS ToNumber/ToPrimitive/ToString + truthiness (ADR
39 # 0007 / value model): Perl's native +,*,==,... do NOT have JS semantics,
40 # so the compiler (k15) emits calls to op_add/op_mul/op_eq/... here.
41 # The template regex literal (ADR 0013) is Text::Nunjucks::Runtime::Regex.
42 # The Number->String routine itself is in Lib (ADR 0007); this module feeds it.
43  
44 # JS special doubles, reused by the operators and division/mod guards.
45 23 my $POS_INF = 9**9**9;
46 23 my $NEG_INF = -$POS_INF;
47 23 my $NAN = $POS_INF - $POS_INF;
48  
49 #### resolvers -- the runtime "exports" that name a class once (getty-perl-core)
50  
51 567 ▶sub None { return Text::Nunjucks::Runtime::None->instance }
Subroutine None: called CC=1 SCAR=0.0
Pod undocumented
52 111467 ▶sub Frame { 'Text::Nunjucks::Runtime::Frame' }
Subroutine Frame: called CC=1 SCAR=0.0
Pod undocumented
53 107524 ▶sub SafeString { 'Text::Nunjucks::Runtime::SafeString' }
Subroutine SafeString: called CC=1 SCAR=0.0
Pod undocumented
54 1423 ▶sub KwArgs { 'Text::Nunjucks::Runtime::KwArgs' }
Subroutine KwArgs: called CC=1 SCAR=0.0
Pod undocumented
55 426 ▶sub Regex { 'Text::Nunjucks::Runtime::Regex' }
Subroutine Regex: called CC=1 SCAR=0.0
Pod undocumented
56  
57 356 ▶sub _error_class { 'Text::Nunjucks::Lib::Error' }
Subroutine _error_class: called CC=1 SCAR=0.0
58 449 ▶sub _template_error_class { 'Text::Nunjucks::Lib::TemplateError' }
Subroutine _template_error_class: called CC=1 SCAR=0.0
59  
60 #### sentinel predicates
61  
62 3544732 ▶sub _is_none { my ( $self, $v ) = @_; ( blessed($v) && $v->isa('Text::Nunjucks::Runtime::None') ) ? 1 : 0 }
Subroutine _is_none: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($v) && $v->isa('Text::Nunjucks::Runtime::None') ? : 3028 3541704 3544732
Condition100%3/3
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::None")
!ll&&!rl&&r
34251021166023028
MC/DC100%2/2
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::None")
builtin::blessed($v) $v->isa("Text::Nunjucks::Runtime::None")
Truth table100%3/3
A and B
A= builtin::blessed($v)B= $v->isa("Text::Nunjucks::Runtime::None")
ABresult
0X0
100
111
63 430917 ▶sub _is_safe_string { my ( $self, $v ) = @_; ( blessed($v) && $v->isa('Text::Nunjucks::Runtime::SafeString') ) ? 1 : 0 }
Subroutine _is_safe_string: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($v) && $v->isa('Text::Nunjucks::Runtime::SafeString') ? : 111701 319216 430917
Condition100%3/3
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::SafeString")
!ll&&!rl&&r
3177301486111701
MC/DC100%2/2
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::SafeString")
builtin::blessed($v) $v->isa("Text::Nunjucks::Runtime::SafeString")
Truth table100%3/3
A and B
A= builtin::blessed($v)B= $v->isa("Text::Nunjucks::Runtime::SafeString")
ABresult
0X0
100
111
64 212839 ▶sub _is_kwargs { my ( $self, $v ) = @_; ( blessed($v) && $v->isa('Text::Nunjucks::Runtime::KwArgs') ) ? 1 : 0 }
Subroutine _is_kwargs: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($v) && $v->isa('Text::Nunjucks::Runtime::KwArgs') ? : 2733 210106 212839
Condition100%3/3
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::KwArgs")
!ll&&!rl&&r
210011952733
MC/DC100%2/2
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::KwArgs")
builtin::blessed($v) $v->isa("Text::Nunjucks::Runtime::KwArgs")
Truth table100%3/3
A and B
A= builtin::blessed($v)B= $v->isa("Text::Nunjucks::Runtime::KwArgs")
ABresult
0X0
100
111
65 153543 ▶sub _is_regex { my ( $self, $v ) = @_; ( blessed($v) && $v->isa('Text::Nunjucks::Runtime::Regex') ) ? 1 : 0 }
Subroutine _is_regex: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($v) && $v->isa('Text::Nunjucks::Runtime::Regex') ? : 194 153349 153543
Condition100%3/3
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::Regex")
!ll&&!rl&&r
1515561793194
MC/DC100%2/2
builtin::blessed($v) and $v->isa("Text::Nunjucks::Runtime::Regex")
builtin::blessed($v) $v->isa("Text::Nunjucks::Runtime::Regex")
Truth table100%3/3
A and B
A= builtin::blessed($v)B= $v->isa("Text::Nunjucks::Runtime::Regex")
ABresult
0X0
100
111
66  
67 #### makeMacro
68  
69 12811 ▶sub make_macro {
Subroutine make_macro: called CC=1 SCAR=1.0
Pod undocumented
70 0 ▶ my ( $self, $arg_names, $kwarg_names, $func ) = @_;
Subroutine make_macro: not called CC=1 SCAR=1.0
Pod undocumented
71  
72 107019 ▶ return sub {
Subroutine __ANON__: called CC=7 SCAR=25.1
73 0 ▶ my @macro_args = @_;
Subroutine __ANON__: not called CC=7 SCAR=25.1
74 0 my $arg_count = $self->num_args( \@macro_args );
75 0 my @args;
76 0 my $kwargs = $self->get_keyword_args( \@macro_args );
77  
78 0 ▶ if ( $arg_count > scalar @$arg_names ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($arg_count > scalar @$arg_names) { } 32 106987 107019
elsif ($arg_count < scalar @$arg_names) { } 1276 105711 106987
if ($arg_count > scalar @$arg_names) { } 0 0 0
elsif ($arg_count < scalar @$arg_names) { } 0 0 0
79 0 @args = @macro_args[ 0 .. $#$arg_names ];
80  
81 # Positional arguments that should be passed in as keyword arguments
82 # (essentially default values).
83 0 my @overflow = @macro_args[ scalar(@args) .. $arg_count - 1 ];
84 0 for my $i ( 0 .. $#overflow ) {
85 0 ▶ if ( $i < scalar @$kwarg_names ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < scalar @$kwarg_names) 7 37 44
if ($i < scalar @$kwarg_names) 0 0 0
86 0 $kwargs->{ $kwarg_names->[$i] } = $overflow[$i];
87 }
88 }
89 0 push @args, $kwargs;
90 } elsif ( $arg_count < scalar @$arg_names ) {
91 0 ▶ @args = $arg_count > 0 ? @macro_args[ 0 .. $arg_count - 1 ] : ();
Branch: 2 branches
BranchTrueFalseTotal
$arg_count > 0 ? : 1253 23 1276
$arg_count > 0 ? : 0 0 0
92  
93 0 ▶ for ( my $i = $arg_count; $i < scalar @$arg_names; $i++ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i < scalar @$arg_names) 2362 1276 3638
if ($i < scalar @$arg_names) 0 0 0
94 0 my $arg = $arg_names->[$i];
95  
96 # Keyword arguments the caller passed by the name of a positional arg.
97 0 push @args, $kwargs->{$arg};
98 0 delete $kwargs->{$arg};
99 }
100 0 push @args, $kwargs;
101 } else {
102 0 @args = @macro_args;
103 }
104  
105 # Port-only (k83): a macro that calls itself N deep enters its body N
106 # calls deep, as upstream nests one JS call per level. Perl warns at the
107 # 100th nested call of a sub, at the call site, and a template cannot
108 # switch a Perl warning off; the depth is bounded by memory, not by Perl
109 # (see Lib async_iter).
110 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
111 0 return $func->(@args);
112 0 };
113 }
114  
115 #### render scope -- port-only, no upstream counterpart (ADR 0021)
116 #
117 # In upstream every function a compiled template is made of closes over
118 # `context`, and the Context holds the macros among them
119 # (context.setVariable): a tracing GC collects those cycles, a reference count
120 # never does. Two things make it more than the macros that read a name from
121 # outside: Perl keeps the enclosing sub of a closure alive, so a macro defined
122 # after an `import` or `include` holds the continuation it was made in and
123 # with it everything that continuation captured; and a macro whose body
124 # contains an async construct captures the `$cb` of its sub, behind which the
125 # continuations of the calling template wait.
126 #
127 # So no compiled sub holds its Context. The first statement of every root and
128 # block sub (Compiler _emit_func_begin) calls this with references to the
129 # sub's `$context` and `$frame` variables: the `$context` variable -- the one
130 # all nested subs of that call capture -- becomes a weak reference, and the
131 # Context remembers both variables (Context _track_scope) to put something
132 # harmless into them when it is freed.
133 #
134 # What holds a Context is what is still going to run in it: the call that
135 # started the render (Template render, _get_exported) for as long as it is on
136 # the stack, and every continuation from the moment the compiled code hands it
137 # out until it is called (_continuation). What already ran holds nothing.
138 14714 ▶sub _render_scope {
Subroutine _render_scope: called CC=3 SCAR=15.0
139 0 ▶ my ( $self, $context_ref, $frame_ref ) = @_;
Subroutine _render_scope: not called CC=3 SCAR=15.0
140 0 my $context = $$context_ref;
141 0 ▶ return unless blessed($context) && $context->can('_track_scope');
Branch: 2 branches
BranchTrueFalseTotal
unless builtin::blessed($context) and $context->can("_track_scope") 71 14643 14714
unless builtin::blessed($context) and $context->can("_track_scope") 0 0 0
Condition33%2/6
builtin::blessed($context) and $context->can("_track_scope")
!ll&&!rl&&r
07114643
builtin::blessed($context) and $context->can("_track_scope")
!ll&&!rl&&r
000
MC/DC50%1/2
builtin::blessed($context) and $context->can("_track_scope")
builtin::blessed($context) $context->can("_track_scope")
MC/DC0%0/2
builtin::blessed($context) and $context->can("_track_scope")
builtin::blessed($context) $context->can("_track_scope")
Truth table66%2/3
A and B
A= builtin::blessed($context)B= $context->can("_track_scope")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($context)B= $context->can("_track_scope")
ABresult
0X0
100
111
142 0 $context->_track_scope( $context_ref, $frame_ref );
143 0 weaken($$context_ref);
144 0 return;
145 }
146  
147 # Every callback the compiled code hands out to be called when something is
148 # done -- upstream's `function(err, res) { ... }` after an async filter, a
149 # getTemplate, a block -- is made through this (Compiler _continuation_open).
150 # The wrapper holds the Context until it is called, and for as long as that
151 # call runs; after that it holds nothing, so a macro made inside the
152 # continuation, which Perl makes keep the continuation, keeps no Context.
153 20808 ▶sub _continuation {
Subroutine _continuation: called CC=1 SCAR=1.2
154 0 ▶ my ( $self, $context, $continuation ) = @_;
Subroutine _continuation: not called CC=1 SCAR=1.2
155 20539 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
156 0 ▶ my $held = $context;
Subroutine __ANON__: not called CC=1 SCAR=1.2
157 0 undef $context;
158 0 return $continuation->(@_);
159 0 };
160 }
161  
162 #### makeKeywordArgs
163  
164 1423 ▶sub make_keyword_args {
Subroutine make_keyword_args: called CC=1 SCAR=1.2
Pod undocumented
165 0 ▶ my ( $self, $obj ) = @_;
Subroutine make_keyword_args: not called CC=1 SCAR=1.2
Pod undocumented
166 0 bless $obj, $self->KwArgs; # JS: obj.__keywords = true
167 0 return $obj;
168 }
169  
170 #### makeDict -- ordered object for template dict literals (ADR 0011)
171 #
172 # Port addition (no upstream counterpart): the compiler (k15) emits
173 # $runtime->make_dict(k1, v1, ...) for every template `{...}` literal so that
174 # JS insertion order survives (a plain Perl hash does not preserve it). The
175 # representation is Hash::Ordered; member_lookup / keys / in_operator below read
176 # it. Most of the engine's other objects come from here too (Lib group_by's
177 # result, the list filter's {key, value}, a regex match's groups). Their key
178 # order -- JS's, array indices first -- is Lib keys' (ADR 0011, k51), not this
179 # constructor's. Two engine-built objects do not come from here and are plain
180 # hashes, so they iterate key-sorted: the object Frame set creates for a dotted
181 # name such as `loop` (upstream runtime.js Frame.set, `obj[id] = {}`) and the
182 # import namespace (Environment get_exported, upstream environment.js:403) --
183 # open, k80.
184 1272 ▶sub make_dict {
Subroutine make_dict: called CC=1 SCAR=1.2
Pod undocumented
185 0 ▶ my ( $self, @kv ) = @_;
Subroutine make_dict: not called CC=1 SCAR=1.2
Pod undocumented
186 0 return Hash::Ordered->new(@kv);
187 }
188  
189 #### makeRegex -- compile a template regex literal into a Regex object (ADR 0013)
190 #
191 # JS RegExp flags (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp):
192 # g -- global (stateful, lastIndex; tracked on the Regex object, NOT a qr// flag)
193 # i -- case-insensitive
194 # m -- ^/$ match per line
195 # s -- . matches newlines
196 # u -- Unicode mode (in JS: \u{...} escapes, surrogate-pair merging)
197 # y -- sticky (stateful match at lastIndex; not portable to a stateless qr//)
198 # JS RegExp is constructed in upstream parser.js:1043 as `new RegExp(body, flags)`
199 # and then read through `memberLookup`. JS's RegExp.prototype.toString yields
200 # `/body/flags`, which is what `{{ r/x/i }}` renders (oracle: '{{ r/x/gi }}'
201 # -> '/x/gi').
202 #
203 # ADR 0013 home is here (k43): one seam that owns pattern translation and the
204 # Regex object. ADR 0005 says engine-internal regexes use explicit ECMAScript
205 # character-class sets; for user-facing regex literals the path of least
206 # surprise today is to compile the JS body verbatim under Perl's /a flag
207 # (ASCII-only `\d`/`\s`/`\w`, matching JS defaults), and croak loudly when
208 # the body does not compile (the "fail loud" rule).
209 206 ▶sub make_regex {
Subroutine make_regex: called CC=13 SCAR=38.9
Pod undocumented
210 0 ▶ my ( $self, $source, $flags ) = @_;
Subroutine make_regex: not called CC=13 SCAR=38.9
Pod undocumented
211 0 ▶ $flags //= '';
Condition25%1/4
$flags //= ""
l!l
2060
$flags //= ""
l!l
00
MC/DC0%0/1
$flags //= ""
$flags
MC/DC0%0/1
$flags //= ""
$flags
Truth table50%1/2
A
A= $flags
Aresult
11
00
Truth table0%0/2
A
A= $flags
Aresult
11
00
212  
213 # Validate every flag -- an unrecognised JS flag is a typo we should not
214 # silently absorb.
215 0 my %known = map { $_ => 1 } qw( g i m s u y );
216 0 for my $f ( split //, $flags ) {
217 croak __PACKAGE__ . '->make_regex: unsupported JS flag ' . $f
218 0 ▶ unless $known{$f};
Branch: 2 branches
BranchTrueFalseTotal
unless $known{$f} 0 115 115
unless $known{$f} 0 0 0
219 }
220  
221 # Map JS flags onto a Perl inline modifier group. g and y do not become
222 # qr// flags -- global state is carried on the Regex object itself
223 # (source/flags/global/test/exec), and a sticky match (y) is out of scope
224 # for a stateless qr// (k43 grow-on-demand: a corpus case for /y would
225 # motivate lastIndex tracking).
226 0 my $perl_mods = '';
227 0 ▶ $perl_mods .= 'i' if index( $flags, 'i' ) >= 0;
Branch: 2 branches
BranchTrueFalseTotal
if index($flags, "i") != -1 15 191 206
if index($flags, "i") != -1 0 0 0
228 0 ▶ $perl_mods .= 'm' if index( $flags, 'm' ) >= 0;
Branch: 2 branches
BranchTrueFalseTotal
if index($flags, "m") != -1 3 203 206
if index($flags, "m") != -1 0 0 0
229 0 ▶ $perl_mods .= 's' if index( $flags, 's' ) >= 0;
Branch: 2 branches
BranchTrueFalseTotal
if index($flags, "s") != -1 0 206 206
if index($flags, "s") != -1 0 0 0
230 # JS `u` (unicode mode): no Perl analogue is exactly right; JS `\d`/`\s`/`\w`
231 # are ASCII-only by default, so /a (ASCII-restricted) is the closest 1:1.
232 # A future corpus case with non-ASCII JS `\p{...}` will motivate either a
233 # tailored translator or a documented divergence (k43 ADR-grown).
234 0 ▶ $perl_mods .= 'a' if index( $flags, 'u' ) >= 0;
Branch: 2 branches
BranchTrueFalseTotal
if index($flags, "u") != -1 0 206 206
if index($flags, "u") != -1 0 0 0
235  
236 0 my $qr;
237 {
238 0 local $@;
239 0 my $compiled = eval { qr/(?$perl_mods:$source)/ };
240 0 ▶ croak __PACKAGE__ . '->make_regex: cannot compile pattern '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $compiled 0 206 206
unless defined $compiled 0 0 0
Condition0%0/4
$@ // "unknown error"
l!l
00
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
Truth table0%0/2
A
A= $@
Aresult
11
00
241 . $source . ': ' . ( $@ // 'unknown error' )
242 unless defined $compiled;
243 0 $qr = $compiled;
244 }
245  
246 # k46: the sticky qr// is the same pattern with a leading \G. ADR 0013 maps
247 # JS /y to a pos-anchored Perl match (the only portable 1:1).
248 0 my $qr_anchored;
249 {
250 0 local $@;
251 0 my $compiled = eval { qr/(?$perl_mods:\G(?:$source))/ };
252 0 ▶ croak __PACKAGE__ . '->make_regex: cannot compile sticky pattern '
Branch: 2 branches
BranchTrueFalseTotal
unless defined $compiled 0 206 206
unless defined $compiled 0 0 0
Condition0%0/4
$@ // "unknown error"
l!l
00
$@ // "unknown error"
l!l
00
MC/DC0%0/1
$@ // "unknown error"
$@
MC/DC0%0/1
$@ // "unknown error"
$@
Truth table0%0/2
A
A= $@
Aresult
11
00
Truth table0%0/2
A
A= $@
Aresult
11
00
253 . $source . ': ' . ( $@ // 'unknown error' )
254 unless defined $compiled;
255 0 $qr_anchored = $compiled;
256 }
257  
258 0 my $groups = $self->Regex->_capture_groups($source);
259 0 my %seen;
260 return $self->Regex->new(
261 source => $source,
262 flags => $flags,
263 _qr => $qr,
264 _qr_anchored => $qr_anchored,
265 _captures_count => scalar @$groups,
266 0 ▶ _group_names => [ grep { defined($_) && !$seen{$_}++ } @$groups ],
Condition50%3/6
defined $_ && !$seen{$_}++
!ll&&!rl&&r
38431
defined $_ && !$seen{$_}++
!ll&&!rl&&r
000
MC/DC100%2/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
MC/DC0%0/2
defined $_ && !$seen{$_}++
defined $_ !$seen{$_}++
Truth table100%3/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
Truth table0%0/3
A && not(B)
A= defined $_B= !$seen{$_}++
ABresult
0X0
100
111
267 );
268 }
269  
270 82426 ▶sub _is_ordered_hash {
Subroutine _is_ordered_hash: called CC=3 SCAR=14.2
271 0 ▶ my ( $self, $v ) = @_;
Subroutine _is_ordered_hash: not called CC=3 SCAR=14.2
272 0 ▶ return ( blessed($v) && $v->isa('Hash::Ordered') ) ? 1 : 0;
Branch: 2 branches
BranchTrueFalseTotal
builtin::blessed($v) && $v->isa('Hash::Ordered') ? : 1464 80962 82426
builtin::blessed($v) && $v->isa('Hash::Ordered') ? : 0 0 0
Condition50%3/6
builtin::blessed($v) and $v->isa("Hash::Ordered")
!ll&&!rl&&r
80888741464
builtin::blessed($v) and $v->isa("Hash::Ordered")
!ll&&!rl&&r
000
MC/DC100%2/2
builtin::blessed($v) and $v->isa("Hash::Ordered")
builtin::blessed($v) $v->isa("Hash::Ordered")
MC/DC0%0/2
builtin::blessed($v) and $v->isa("Hash::Ordered")
builtin::blessed($v) $v->isa("Hash::Ordered")
Truth table100%3/3
A and B
A= builtin::blessed($v)B= $v->isa("Hash::Ordered")
ABresult
0X0
100
111
Truth table0%0/3
A and B
A= builtin::blessed($v)B= $v->isa("Hash::Ordered")
ABresult
0X0
100
111
273 }
274  
275 #### isKeywordArgs
276  
277 212797 ▶sub is_keyword_args {
Subroutine is_keyword_args: called CC=1 SCAR=1.2
Pod undocumented
278 0 ▶ my ( $self, $obj ) = @_;
Subroutine is_keyword_args: not called CC=1 SCAR=1.2
Pod undocumented
279 0 return $self->_is_kwargs($obj);
280 }
281  
282 #### getKeywordArgs
283  
284 107022 ▶sub get_keyword_args {
Subroutine get_keyword_args: called CC=3 SCAR=14.2
Pod undocumented
285 0 ▶ my ( $self, $args ) = @_;
Subroutine get_keyword_args: not called CC=3 SCAR=14.2
Pod undocumented
286 0 my $len = scalar @$args;
287 0 ▶ if ($len) {
Branch: 2 branches
BranchTrueFalseTotal
if ($len) 106397 625 107022
if ($len) 0 0 0
288 0 my $last_arg = $args->[ $len - 1 ];
289 0 ▶ return $last_arg if $self->is_keyword_args($last_arg);
Branch: 2 branches
BranchTrueFalseTotal
if $self->is_keyword_args($last_arg) 1356 105041 106397
if $self->is_keyword_args($last_arg) 0 0 0
290 }
291 0 return {};
292 }
293  
294 #### numArgs
295  
296 107022 ▶sub num_args {
Subroutine num_args: called CC=3 SCAR=14.2
Pod undocumented
297 0 ▶ my ( $self, $args ) = @_;
Subroutine num_args: not called CC=3 SCAR=14.2
Pod undocumented
298 0 my $len = scalar @$args;
299 0 ▶ return 0 if $len == 0;
Branch: 2 branches
BranchTrueFalseTotal
if $len == 0 625 106397 107022
if $len == 0 0 0 0
300  
301 0 my $last_arg = $args->[ $len - 1 ];
302 0 ▶ return $self->is_keyword_args($last_arg) ? $len - 1 : $len;
Branch: 2 branches
BranchTrueFalseTotal
$self->is_keyword_args($last_arg) ? : 1356 105041 106397
$self->is_keyword_args($last_arg) ? : 0 0 0
303 }
304  
305 #### copySafeness
306  
307 919 ▶sub copy_safeness {
Subroutine copy_safeness: called CC=4 SCAR=17.9
Pod undocumented
308 0 ▶ my ( $self, $dest, $target ) = @_;
Subroutine copy_safeness: not called CC=4 SCAR=17.9
Pod undocumented
309 0 ▶ return $self->SafeString->new($target) if $self->_is_safe_string($dest);
Branch: 2 branches
BranchTrueFalseTotal
if $self->_is_safe_string($dest) 54 865 919
if $self->_is_safe_string($dest) 0 0 0
310 # target.toString(): reading toString off undefined / null throws V8's
311 # TypeError (oracle: undefined|string -> "Cannot read properties of undefined
312 # (reading 'toString')"); any other value's toString() is its JS ToString --
313 # an own toString member and a null prototype are k44
314 0 ▶ die $self->_js_read_error( $target, 'toString' ) if !defined($target) || $self->_is_none($target);
Branch: 2 branches
BranchTrueFalseTotal
if not defined $target or $self->_is_none($target) 9 856 865
if not defined $target or $self->_is_none($target) 0 0 0
Condition50%3/6
not defined $target or $self->_is_none($target)
l!l&&r!l&&!r
45856
not defined $target or $self->_is_none($target)
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $target or $self->_is_none($target)
not defined $target $self->_is_none($target)
MC/DC0%0/2
not defined $target or $self->_is_none($target)
not defined $target $self->_is_none($target)
Truth table100%3/3
not(A) or B
A= not defined $targetB= $self->_is_none($target)
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $targetB= $self->_is_none($target)
ABresult
1X1
011
000
315 0 return $self->_js_to_string($target);
316 }
317  
318 #### markSafe
319  
320 2541 ▶sub mark_safe {
Subroutine mark_safe: called CC=3 SCAR=15.4
Pod undocumented
321 0 ▶ my ( $self, $val ) = @_;
Subroutine mark_safe: not called CC=3 SCAR=15.4
Pod undocumented
322  
323 0 ▶ if ( Text::Nunjucks::Lib->is_string($val) ) {
Branch: 4 branches
BranchTrueFalseTotal
if ('Text::Nunjucks::Lib'->is_string($val)) { } 2539 2 2541
elsif (not 'Text::Nunjucks::Lib'->is_function($val)) { } 1 1 2
if ('Text::Nunjucks::Lib'->is_string($val)) { } 0 0 0
elsif (not 'Text::Nunjucks::Lib'->is_function($val)) { } 0 0 0
324 0 return $self->SafeString->new($val);
325 } elsif ( !Text::Nunjucks::Lib->is_function($val) ) {
326 0 return $val;
327 } else {
328 1 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=12.2
329 0 ▶ my $ret = $val->(@_); # val.apply(this, arguments) -- no `this` (ADR 0009)
Subroutine __ANON__: not called CC=2 SCAR=12.2
330 0 ▶ return $self->SafeString->new($ret) if Text::Nunjucks::Lib->is_string($ret);
Branch: 2 branches
BranchTrueFalseTotal
if "Text::Nunjucks::Lib"->is_string($ret) 1 0 1
if "Text::Nunjucks::Lib"->is_string($ret) 0 0 0
331 0 return $ret;
332 0 };
333 }
334 }
335  
336 #### suppressValue
337  
338 130817 ▶sub suppress_value {
Subroutine suppress_value: called CC=5 SCAR=20.9
Pod undocumented
339 0 ▶ my ( $self, $val, $autoescape ) = @_;
Subroutine suppress_value: not called CC=5 SCAR=20.9
Pod undocumented
340  
341 # JS: val = (val !== undefined && val !== null) ? val : '';
342 0 ▶ $val = '' if !defined($val) || $self->_is_none($val);
Branch: 2 branches
BranchTrueFalseTotal
if not defined $val or $self->_is_none($val) 607 130210 130817
if not defined $val or $self->_is_none($val) 0 0 0
Condition50%3/6
not defined $val or $self->_is_none($val)
l!l&&r!l&&!r
53869130210
not defined $val or $self->_is_none($val)
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $val or $self->_is_none($val)
not defined $val $self->_is_none($val)
MC/DC0%0/2
not defined $val or $self->_is_none($val)
not defined $val $self->_is_none($val)
Truth table100%3/3
not(A) or B
A= not defined $valB= $self->_is_none($val)
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $valB= $self->_is_none($val)
ABresult
1X1
011
000
343  
344 0 ▶ if ( $autoescape && !$self->_is_safe_string($val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($autoescape and not $self->_is_safe_string($val)) 126800 4017 130817
if ($autoescape and not $self->_is_safe_string($val)) 0 0 0
Condition50%3/6
$autoescape and not $self->_is_safe_string($val)
!ll&&!rl&&r
4373580126800
$autoescape and not $self->_is_safe_string($val)
!ll&&!rl&&r
000
MC/DC100%2/2
$autoescape and not $self->_is_safe_string($val)
$autoescape not $self->_is_safe_string($val)
MC/DC0%0/2
$autoescape and not $self->_is_safe_string($val)
$autoescape not $self->_is_safe_string($val)
Truth table100%3/3
A and not(B)
A= $autoescapeB= not $self->_is_safe_string($val)
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $autoescapeB= not $self->_is_safe_string($val)
ABresult
0X0
100
111
345 0 $val = Text::Nunjucks::Lib->escape( $self->_js_to_string($val) );
346 }
347  
348 0 return $val;
349 }
350  
351 #### ensureDefined
352  
353 22 ▶sub ensure_defined {
Subroutine ensure_defined: called CC=3 SCAR=14.2
Pod undocumented
354 0 ▶ my ( $self, $val, $lineno, $colno ) = @_;
Subroutine ensure_defined: not called CC=3 SCAR=14.2
Pod undocumented
355 0 ▶ if ( !defined($val) || $self->_is_none($val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $val or $self->_is_none($val)) 3 19 22
if (not defined $val or $self->_is_none($val)) 0 0 0
Condition50%3/6
not defined $val or $self->_is_none($val)
l!l&&r!l&&!r
2119
not defined $val or $self->_is_none($val)
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $val or $self->_is_none($val)
not defined $val $self->_is_none($val)
MC/DC0%0/2
not defined $val or $self->_is_none($val)
not defined $val $self->_is_none($val)
Truth table100%3/3
not(A) or B
A= not defined $valB= $self->_is_none($val)
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $valB= $self->_is_none($val)
ABresult
1X1
011
000
356 0 die $self->_template_error_class->new(
357 'attempted to output null or undefined value',
358 $lineno + 1,
359 $colno + 1
360 );
361 }
362 0 return $val;
363 }
364  
365 #### memberLookup
366 #
367 # Upstream is a raw `obj[val]` (any property/method reachable, functions come
368 # back as a closure bound to the owner). Perl scalars/arrays have no such
369 # members, so ADR 0008 makes this a curated, in-place emulated String / Array /
370 # Number surface -- grown on demand, each entry oracle-verified. String
371 # indexing, .length and the string methods reckon in UTF-16 code units through
372 # the Lib seams of ADR 0012; ToNumber / ToString of their arguments stay here.
373 #
374 # ADR 0016 split: this function is the PROPERTY-ACCESS seam
375 # (`member_lookup($obj, $key)` returns the value the template would see at
376 # `{{ obj.key }}`); the METHOD-CALL seam `member_lookup_method` returns a bound
377 # coderef for `{{ obj.key() }}`. The compiler picks at the AST site
378 # (compile_lookup_val vs compile_fun_call -> the latter on a LookupVal name).
379  
380 my %STRING_METHODS = (
381 # JS case mapping (Final_Sigma, ß -> SS, lone surrogates unchanged) is the
382 # one Lib seam the lower / upper / capitalize filters use as well
383 206 ▶ toUpperCase => sub { my ( $self, $s ) = @_; Text::Nunjucks::Lib->js_to_upper_case($s) },
Subroutine __ANON__: called CC=1 SCAR=0.0
384 198 ▶ toLowerCase => sub { my ( $self, $s ) = @_; Text::Nunjucks::Lib->js_to_lower_case($s) },
Subroutine __ANON__: called CC=1 SCAR=0.0
385 2 ▶ trim => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
386 2 my ( $self, $s ) = @_;
387 2 my $ws = Text::Nunjucks::Lib->js_whitespace_class; # ADR 0005 seam
388 2 $s =~ s/\A(?:$ws)+//;
389 2 $s =~ s/(?:$ws)+\z//;
390 2 return $s;
391 },
392 24 ▶ replace => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
393 # String.prototype.replace(searchValue, replaceValue): a regex pattern runs
394 # RegExp.prototype[Symbol.replace] (ADR 0013); a string pattern replaces its
395 # FIRST occurrence, with a coderef's result (called with matched, position,
396 # string) or the replacement string's $ patterns (GetSubstitution). Oracle:
397 # 'abc'.replace('b', '$&$&') -> abbc, 'a1b'.replace(r/\d/, '-') -> a-b.
398 24 my ( $self, $s, $pattern, $repl ) = @_;
399 24 ▶ return $pattern->symbol_replace( $s, $repl ) if $self->_is_regex($pattern);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_regex($pattern) 7 17 24
400 17 my $functional = Text::Nunjucks::Lib->is_function($repl);
401 17 $pattern = $self->_js_to_string($pattern);
402 17 ▶ $repl = $self->_js_to_string($repl) unless $functional;
Branch: 1 branch
BranchTrueFalseTotal
unless $functional 16 1 17
403 17 my $idx = Text::Nunjucks::Lib->js_string_index_of( $s, $pattern );
404 17 ▶ return $s if $idx < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $idx < 0 1 16 17
405 16 ▶ my $replacement = $functional
Branch: 1 branch
BranchTrueFalseTotal
$functional ? : 1 15 16
406 ? $self->_js_to_string( $repl->( $pattern, $idx, $s ) )
407 : Text::Nunjucks::Lib->js_get_substitution( $pattern, $s, $idx, [], undef, $repl );
408 # preceding + replacement + following, joined in code units (ADR 0012)
409 16 my $out = Text::Nunjucks::Lib->js_substring( $s, 0, $idx );
410 16 Text::Nunjucks::Lib->js_string_append( $out, $replacement );
411 16 Text::Nunjucks::Lib->js_string_append( $out,
412 Text::Nunjucks::Lib->js_substring( $s, $idx + Text::Nunjucks::Lib->js_string_length($pattern) ) );
413 16 return $out;
414 },
415 234 ▶ slice => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
416 234 my ( $self, $s, $start, $end ) = @_;
417 234 return Text::Nunjucks::Lib->js_string_slice( $s,
418 $self->_to_number_or_undef($start), $self->_to_number_or_undef($end) );
419 },
420 261 ▶ substring => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
421 261 my ( $self, $s, $start, $end ) = @_;
422 261 return Text::Nunjucks::Lib->js_substring( $s,
423 $self->_to_number_or_undef($start), $self->_to_number_or_undef($end) );
424 },
425 3893 ▶ substr => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
426 3893 my ( $self, $s, $start, $length ) = @_;
427 3893 return Text::Nunjucks::Lib->js_substr( $s,
428 $self->_to_number_or_undef($start), $self->_to_number_or_undef($length) );
429 },
430 192 ▶ split => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
431 192 my ( $self, $s, $sep, $limit ) = @_;
432 # ToString(separator) unless undefined (split() -> [whole string]); a null
433 # separator is the string 'null'
434 192 ▶ return Text::Nunjucks::Lib->js_split( $s,
Branch: 1 branch
BranchTrueFalseTotal
defined $sep ? : 190 2 192
435 defined($sep) ? $self->_js_to_string($sep) : undef, $self->_to_number_or_undef($limit) );
436 },
437 216 ▶ indexOf => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
438 216 my ( $self, $s, $search, $from ) = @_;
439 216 return Text::Nunjucks::Lib->js_string_index_of( $s,
440 $self->_js_to_string($search), $self->_to_number_or_undef($from) );
441 },
442 54 ▶ lastIndexOf => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
443 54 my ( $self, $s, $search, $from ) = @_;
444 54 return Text::Nunjucks::Lib->js_string_last_index_of( $s,
445 $self->_js_to_string($search), $self->_to_number_or_undef($from) );
446 },
447 135 ▶ charAt => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
448 135 my ( $self, $s, $i ) = @_;
449 135 return Text::Nunjucks::Lib->js_char_at( $s, $self->_to_number_or_undef($i) );
450 },
451 2 ▶ concat => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
452 2 my ( $self, $s, @args ) = @_;
453 2 Text::Nunjucks::Lib->js_string_append( $s, $self->_js_to_string($_) ) for @args; # ADR 0012
454 2 return $s;
455 },
456 23 );
457  
458 my %ARRAY_METHODS = (
459 339 ▶ join => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
460 339 my ( $self, $arr, $sep ) = @_;
461 339 ▶ $sep = defined($sep) ? $self->_js_to_string($sep) : ',';
Branch: 1 branch
BranchTrueFalseTotal
defined $sep ? : 338 1 339
462 339 return $self->_array_join( $arr, $sep );
463 },
464 2 ▶ indexOf => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
465 2 my ( $self, $arr, $search, $from ) = @_;
466 2 return Text::Nunjucks::Lib->index_of( $arr, $search, $from );
467 },
468 26 ▶ lastIndexOf => sub {
Subroutine __ANON__: called CC=8 SCAR=20.8
469 # Array.prototype.lastIndexOf(searchElement[, fromIndex]): strict equality,
470 # scanning back from fromIndex -- length - 1 when absent, counted from the
471 # end when negative. Oracle: [1,2,1].lastIndexOf(1) 2, (1, 1) 0, (1, -1) 2,
472 # (1, -2) 0, (1, -9) -1, (1, 9) 2, (1, 'x') 0. The truncate filter reaches
473 # it on an array input (input.lastIndexOf(' ', length)).
474 26 my ( $self, $arr, $search, @from ) = @_;
475 26 my $len = scalar @$arr;
476 26 ▶ return -1 if $len == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 2 24 26
477 24 ▶ my $n = @from ? Text::Nunjucks::Lib->_js_to_integer_or_infinity( $self->_to_number( $from[0] ) ) : $len - 1;
Branch: 1 branch
BranchTrueFalseTotal
@from ? : 20 4 24
478 24 ▶ return -1 if $n == $NEG_INF;
Branch: 1 branch
BranchTrueFalseTotal
if $n == $NEG_INF 2 22 24
479 22 ▶ my $k = $n >= 0 ? ( $n < $len - 1 ? $n : $len - 1 ) : $len + $n;
Branch: 2 branches
BranchTrueFalseTotal
$n >= 0 ? : 16 6 22
$n < $len - 1 ? : 6 10 16
480 22 ▶ for ( my $i = $k; $i >= 0; $i-- ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 29 4 33
481 29 ▶ return $i if Text::Nunjucks::Lib->_strict_eq( $arr->[$i], $search );
Branch: 1 branch
BranchTrueFalseTotal
if "Text::Nunjucks::Lib"->_strict_eq($arr->[$i], $search) 18 11 29
482 }
483 4 return -1;
484 },
485 136 ▶ slice => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
486 136 my ( $self, $arr, $start, $end ) = @_;
487 136 my $len = scalar @$arr;
488 136 ( $start, $end ) = $self->_slice_bounds( $len, $start, $end );
489 136 ▶ return [] if $start >= $end;
Branch: 1 branch
BranchTrueFalseTotal
if $start >= $end 25 111 136
490 111 return [ @{$arr}[ $start .. $end - 1 ] ];
491 },
492 3 ▶ concat => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
493 3 my ( $self, $arr, @args ) = @_;
494 3 my @new = @$arr;
495 3 for my $a (@args) {
496 3 ▶ if ( Text::Nunjucks::Lib->is_array($a) ) { push @new, @$a }
Branch: 1 branch
BranchTrueFalseTotal
if ('Text::Nunjucks::Lib'->is_array($a)) { } 1 2 3
497 2 else { push @new, $a }
498 }
499 3 return \@new;
500 },
501 176 ▶ filter => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
502 # Array.prototype.filter(callbackfn[, thisArg]): the length is read once,
503 # callbackfn gets (element, index, array) and no this (ADR 0009), an index
504 # the callback removed is skipped, an element is kept when the result is
505 # truthy. Oracle: [1, 0, '', 2].filter(m) with a macro m printing its
506 # argument keeps 1,0,,2 (its output is a SafeString, an object); a
507 # callbackfn that is not callable -> "TypeError: number 5 is not a
508 # function", none -> "TypeError: object null is not a function", no
509 # argument -> "TypeError: undefined is not a function". The select /
510 # reject / selectattr / rejectattr filters' .filter reaches it.
511 176 my ( $self, $arr, $callbackfn ) = @_;
512 176 ▶ die $self->_error_class->new( $self->_js_not_callable_text($callbackfn) . ' is not a function', name => 'TypeError' )
Branch: 1 branch
BranchTrueFalseTotal
unless "Text::Nunjucks::Lib"->is_function($callbackfn) 6 170 176
513 unless Text::Nunjucks::Lib->is_function($callbackfn);
514 170 my $len = scalar @$arr;
515 170 my @a;
516 170 ▶ for ( my $k = 0; $k < $len; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $len) 800 162 962
517 800 ▶ next if $k >= scalar @$arr; # HasProperty(O, k)
Branch: 1 branch
BranchTrueFalseTotal
if $k >= scalar @$arr 1 799 800
518 799 my $k_value = $arr->[$k];
519 799 ▶ push @a, $k_value if $self->_truthy( $callbackfn->( $k_value, $k, $arr ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_truthy(&$callbackfn($k_value, $k, $arr)) 385 406 791
520 }
521 162 return \@a;
522 },
523 162 ▶ reduce => sub {
Subroutine __ANON__: called CC=6 SCAR=17.9
524 # Array.prototype.reduce(callbackfn[, initialValue]): the length is read
525 # once, callbackfn gets (accumulator, element, index, array), an index the
526 # callback removed is skipped. Oracle: [1,2,3].reduce(f, 0) with f(a, b)
527 # returning a + b -> 123 (macro output is a string), f(a, b, i, arr) sees
528 # x:1:0:2 then :2:1:2. [].reduce(f) -> "TypeError: Reduce of empty array
529 # with no initial value"; a callbackfn that is not callable is checked
530 # first -> "TypeError: number 5 is not a function". The sum filter's
531 # arr.reduce reaches it.
532 162 my ( $self, $arr, $callbackfn, @initial_value ) = @_;
533 162 ▶ die $self->_error_class->new( $self->_js_not_callable_text($callbackfn) . ' is not a function', name => 'TypeError' )
Branch: 1 branch
BranchTrueFalseTotal
unless "Text::Nunjucks::Lib"->is_function($callbackfn) 21 141 162
534 unless Text::Nunjucks::Lib->is_function($callbackfn);
535 141 my $len = scalar @$arr;
536 141 my $k = 0;
537 141 my $accumulator;
538 141 ▶ if (@initial_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (@initial_value) { } 135 6 141
539 135 $accumulator = $initial_value[0];
540 } else {
541 6 ▶ die $self->_error_class->new( 'Reduce of empty array with no initial value', name => 'TypeError' )
Branch: 1 branch
BranchTrueFalseTotal
if $len == 0 2 4 6
542 if $len == 0;
543 4 $accumulator = $arr->[ $k++ ];
544 }
545 139 ▶ for ( ; $k < $len; $k++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < $len) 233 139 372
546 233 ▶ next if $k >= scalar @$arr; # HasProperty(O, k)
Branch: 1 branch
BranchTrueFalseTotal
if $k >= scalar @$arr 0 233 233
547 233 $accumulator = $callbackfn->( $accumulator, $arr->[$k], $k, $arr );
548 }
549 139 return $accumulator;
550 },
551 # mutators -- change the underlying Perl structure in place (ADR 0008)
552 31485 ▶ push => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
553 31485 my ( $self, $arr, @args ) = @_;
554 31485 push @$arr, @args;
555 31485 return scalar @$arr; # Array.push returns the new length
556 },
557 9 ▶ pop => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
558 9 my ( $self, $arr ) = @_;
559 9 ▶ return undef unless @$arr; # JS pop() on [] -> undefined
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 3 6 9
560 6 return pop @$arr;
561 },
562 9 ▶ shift => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
563 9 my ( $self, $arr ) = @_;
564 9 ▶ return undef unless @$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 3 6 9
565 6 return shift @$arr;
566 },
567 1 ▶ unshift => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
568 1 my ( $self, $arr, @args ) = @_;
569 1 unshift @$arr, @args;
570 1 return scalar @$arr;
571 },
572 263 ▶ sort => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
573 # Array.prototype.sort([comparefn]): V8's TimSort, the one Lib seam
574 # (decision B, k17) -- undefined last and never compared, ToStrings in
575 # UTF-16 code-unit order without a comparefn, a comparefn's result through
576 # ToNumber. Oracle: [u, 'z', 'a'].sort() -> a,z,undefined; a macro m(a, b)
577 # giving a - b sorts [3, 1, 10, 2] to 1,2,3,10; sort(5) -> "TypeError: The
578 # comparison function must be either a function or undefined: 5".
579 263 my ( $self, $arr, $comparefn ) = @_;
580 263 return Text::Nunjucks::Lib->js_array_sort( $arr, $comparefn );
581 },
582 97 ▶ reverse => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
583 97 my ( $self, $arr ) = @_;
584 97 @$arr = reverse @$arr;
585 97 return $arr;
586 },
587 23 );
588  
589 my %NUMBER_METHODS = (
590 3 ▶ toFixed => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
591 3 my ( $self, $n, $digits ) = @_;
592 3 ▶ $digits = defined($digits) ? int( $self->_to_number($digits) ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $digits ? : 2 1 3
593 3 return sprintf( '%.*f', $digits, $n );
594 },
595 23 );
596  
597 114219 ▶sub member_lookup {
Subroutine member_lookup: called CC=30 SCAR=49.2
Pod undocumented
598 0 ▶ my ( $self, $obj, $val ) = @_;
Subroutine member_lookup: not called CC=30 SCAR=49.2
Pod undocumented
599  
600 # if (obj === undefined || obj === null) return undefined;
601 0 ▶ return undef if !defined($obj) || $self->_is_none($obj);
Branch: 2 branches
BranchTrueFalseTotal
if not defined $obj or $self->_is_none($obj) 19 114200 114219
if not defined $obj or $self->_is_none($obj) 0 0 0
Condition50%3/6
not defined $obj or $self->_is_none($obj)
l!l&&r!l&&!r
181114200
not defined $obj or $self->_is_none($obj)
l!l&&r!l&&!r
000
MC/DC100%2/2
not defined $obj or $self->_is_none($obj)
not defined $obj $self->_is_none($obj)
MC/DC0%0/2
not defined $obj or $self->_is_none($obj)
not defined $obj $self->_is_none($obj)
Truth table100%3/3
not(A) or B
A= not defined $objB= $self->_is_none($obj)
ABresult
1X1
011
000
Truth table0%0/3
not(A) or B
A= not defined $objB= $self->_is_none($obj)
ABresult
1X1
011
000
602  
603 # SafeString extends String in JS: member access works off its value.
604 0 ▶ my $str_target = $self->_is_safe_string($obj) ? $obj->val : undef;
Branch: 2 branches
BranchTrueFalseTotal
$self->_is_safe_string($obj) ? : 364 113836 114200
$self->_is_safe_string($obj) ? : 0 0 0
605  
606 0 my $key = $self->_member_key($val); # JS ToString of the lookup key
607  
608 # Regex surface (ADR 0013): source / flags / global / lastIndex are property
609 # reads, not method calls. `test` and `exec` are method-shaped and go through
610 # member_lookup_method (FunCall path). lastIndex is template-visible
611 # ({{ r.lastIndex }}) and reflects the JS RegExp match position; it advances
612 # after a g/y match and resets to 0 on no-match.
613 0 ▶ if ( $self->_is_regex($obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_regex($obj)) 37 114163 114200
if ($self->_is_regex($obj)) 0 0 0
614 0 ▶ return $obj->source if $key eq 'source';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "source" 2 35 37
if $key eq "source" 0 0 0
615 0 ▶ return $obj->flags if $key eq 'flags';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "flags" 2 33 35
if $key eq "flags" 0 0 0
616 0 ▶ return $obj->global if $key eq 'global';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "global" 4 29 33
if $key eq "global" 0 0 0
617 0 ▶ return $obj->lastIndex if $key eq 'lastIndex';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "lastIndex" 22 7 29
if $key eq "lastIndex" 0 0 0
618 0 return undef;
619 }
620  
621 # Array surface
622 0 ▶ if ( Text::Nunjucks::Lib->is_array($obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ("Text::Nunjucks::Lib"->is_array($obj)) 26734 87429 114163
if ("Text::Nunjucks::Lib"->is_array($obj)) 0 0 0
623 0 ▶ if ( $self->_is_index_key($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_index_key($key)) 23203 3531 26734
if ($self->_is_index_key($key)) 0 0 0
624 0 my $i = 0 + $key;
625 0 ▶ return undef if $i >= scalar @$obj;
Branch: 2 branches
BranchTrueFalseTotal
if $i >= scalar @$obj 10 23193 23203
if $i >= scalar @$obj 0 0 0
626 0 return $obj->[$i];
627 }
628 0 ▶ return scalar @$obj if $key eq 'length';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "length" 2802 729 3531
if $key eq "length" 0 0 0
629 0 ▶ if ( $ARRAY_METHODS{$key} ) { # ADR 0016: method on FunCall seam
Branch: 2 branches
BranchTrueFalseTotal
if ($ARRAY_METHODS{$key}) 62 667 729
if ($ARRAY_METHODS{$key}) 0 0 0
630 0 ▶ return sub { $ARRAY_METHODS{$key}->( $self, $obj, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.4
631 }
632 0 return undef;
633 }
634  
635 # String surface (plain string or SafeString value)
636 0 ▶ if ( defined($str_target) || Text::Nunjucks::Lib->is_string($obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $str_target or "Text::Nunjucks::Lib"->is_string($obj)) 5533 81896 87429
if (defined $str_target or "Text::Nunjucks::Lib"->is_string($obj)) 0 0 0
Condition50%3/6
defined $str_target or "Text::Nunjucks::Lib"->is_string($obj)
l!l&&r!l&&!r
364516981896
defined $str_target or "Text::Nunjucks::Lib"->is_string($obj)
l!l&&r!l&&!r
000
MC/DC100%2/2
defined $str_target or "Text::Nunjucks::Lib"->is_string($obj)
defined $str_target "Text::Nunjucks::Lib"->is_string($obj)
MC/DC0%0/2
defined $str_target or "Text::Nunjucks::Lib"->is_string($obj)
defined $str_target "Text::Nunjucks::Lib"->is_string($obj)
Truth table100%3/3
A or B
A= defined $str_targetB= "Text::Nunjucks::Lib"->is_string($obj)
ABresult
1X1
011
000
Truth table0%0/3
A or B
A= defined $str_targetB= "Text::Nunjucks::Lib"->is_string($obj)
ABresult
1X1
011
000
637 0 ▶ my $s = defined($str_target) ? $str_target : $obj;
Branch: 2 branches
BranchTrueFalseTotal
defined $str_target ? : 364 5169 5533
defined $str_target ? : 0 0 0
638 # A primitive string has JS exotic index access (s[0]); a SafeString is a
639 # String OBJECT -- length + methods but NO index access (oracle: on a
640 # SafeString `s[0]` and `s['0']` render ''). So gate indexing to plain
641 # strings only, never $str_target (the SafeString case).
642 # Index and length count UTF-16 code units (ADR 0012): s[i] past an astral
643 # character lands one further, and inside one yields a lone surrogate. One
644 # charAt call measures the string once; its '' (a code unit is never empty)
645 # is the index past the end, where s[i] is undefined.
646 0 ▶ if ( !defined($str_target) && $self->_is_index_key($key) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $str_target and $self->_is_index_key($key)) 240 5293 5533
if (not defined $str_target and $self->_is_index_key($key)) 0 0 0
Condition50%3/6
not defined $str_target and $self->_is_index_key($key)
!ll&&!rl&&r
3644929240
not defined $str_target and $self->_is_index_key($key)
!ll&&!rl&&r
000
MC/DC100%2/2
not defined $str_target and $self->_is_index_key($key)
not defined $str_target $self->_is_index_key($key)
MC/DC0%0/2
not defined $str_target and $self->_is_index_key($key)
not defined $str_target $self->_is_index_key($key)
Truth table100%3/3
not(A) and B
A= not defined $str_targetB= $self->_is_index_key($key)
ABresult
0X0
100
111
Truth table0%0/3
not(A) and B
A= not defined $str_targetB= $self->_is_index_key($key)
ABresult
0X0
100
111
647 0 my $unit = Text::Nunjucks::Lib->js_char_at( $s, 0 + $key );
648 0 ▶ return $unit eq '' ? undef : $unit;
Branch: 2 branches
BranchTrueFalseTotal
$unit eq '' ? : 5 235 240
$unit eq '' ? : 0 0 0
649 }
650 0 ▶ return Text::Nunjucks::Lib->js_string_length($s) if $key eq 'length';
Branch: 2 branches
BranchTrueFalseTotal
if $key eq "length" 5168 125 5293
if $key eq "length" 0 0 0
651 0 ▶ if ( $STRING_METHODS{$key} ) { # ADR 0016: method on FunCall seam
Branch: 2 branches
BranchTrueFalseTotal
if ($STRING_METHODS{$key}) 19 106 125
if ($STRING_METHODS{$key}) 0 0 0
652 0 ▶ return sub { $STRING_METHODS{$key}->( $self, $s, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.4
653 }
654 0 return undef;
655 }
656  
657 # Number surface
658 0 ▶ if ( created_as_number($obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (created_as_number $obj) 76 81820 81896
if (created_as_number $obj) 0 0 0
659 0 ▶ if ( $NUMBER_METHODS{$key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($NUMBER_METHODS{$key}) 3 73 76
if ($NUMBER_METHODS{$key}) 0 0 0
660 0 ▶ return sub { $NUMBER_METHODS{$key}->( $self, $obj, @_ ) };
Subroutine __ANON__: called CC=1 SCAR=0.4
661 }
662 0 return undef;
663 }
664  
665 # Ordered hash (template dict literal / engine-built object, ADR 0011): read
666 # it like a plain object before the generic blessed-object branch below.
667 0 ▶ if ( $self->_is_ordered_hash($obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_ordered_hash($obj)) 1142 80678 81820
if ($self->_is_ordered_hash($obj)) 0 0 0
668 0 ▶ return $obj->exists($key) ? $obj->get($key) : undef;
Branch: 2 branches
BranchTrueFalseTotal
$obj->exists($key) ? : 936 206 1142
$obj->exists($key) ? : 0 0 0
669 }
670  
671 # Plain hashref: property access (a coderef value is returned as-is and
672 # called with no `this` per ADR 0009).
673 0 ▶ if ( ref($obj) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $obj eq "HASH") 80619 59 80678
if (ref $obj eq "HASH") 0 0 0
674 0 ▶ return exists( $obj->{$key} ) ? $obj->{$key} : undef;
Branch: 2 branches
BranchTrueFalseTotal
exists $obj->{$key} ? : 80304 315 80619
exists $obj->{$key} ? : 0 0 0
675 }
676  
677 # Blessed user object (ADR 0016: this seam returns the VALUE -- an accessor
678 # call or a Moo attribute read -- not a bound coderef. The coderef comes from
679 # member_lookup_method, which the compiler emits at FunCall sites).
680 0 ▶ if ( blessed($obj) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($obj)) 4 55 59
if (builtin::blessed($obj)) 0 0 0
681 0 ▶ return $obj->$key if $obj->can($key);
Branch: 2 branches
BranchTrueFalseTotal
if $obj->can($key) 1 3 4
if $obj->can($key) 0 0 0
682 0 return undef;
683 }
684  
685 0 return undef;
686 }
687  
688 # ADR 0016: the second half of the split. Where member_lookup returns the value
689 # of a property, member_lookup_method returns a bound coderef for the method at
690 # $key, ready to be passed to call_wrap. The two branches land on different
691 # emitted symbols in the compiled Perl; FunCall -> member_lookup_method, every
692 # other property read -> member_lookup.
693 38314 ▶sub member_lookup_method {
Subroutine member_lookup_method: called CC=22 SCAR=30.9
Pod undocumented
694 38314 my ( $self, $obj, $val ) = @_;
695  
696 38314 ▶ return undef if !defined($obj) || $self->_is_none($obj);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $obj or $self->_is_none($obj) 29 38285 38314
Condition100%3/3
not defined $obj or $self->_is_none($obj)
l!l&&r!l&&!r
21838285
MC/DC100%2/2
not defined $obj or $self->_is_none($obj)
not defined $obj $self->_is_none($obj)
Truth table100%3/3
not(A) or B
A= not defined $objB= $self->_is_none($obj)
ABresult
1X1
011
000
697  
698 38285 my $key = $self->_member_key($val);
699  
700 # Regex: test/exec are method-shaped; source/flags/global are properties
701 # (FunCall would be `{{ r.test("...") }} }`).
702 38285 ▶ if ( $self->_is_regex($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_regex($obj)) 51 38234 38285
703 51 ▶ return sub { $obj->test(@_) } if $key eq 'test';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "test" 18 33 51
704 33 ▶ return sub { $obj->exec(@_) } if $key eq 'exec';
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "exec" 32 1 33
705 1 return undef;
706 }
707  
708 # String surface -- emulated methods come back bound to the string value.
709 38234 ▶ if ( Text::Nunjucks::Lib->is_string($obj) || $self->_is_safe_string($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Text::Nunjucks::Lib"->is_string($obj) or $self->_is_safe_string($obj)) 5418 32816 38234
Condition100%3/3
"Text::Nunjucks::Lib"->is_string($obj) or $self->_is_safe_string($obj)
l!l&&r!l&&!r
53417732816
MC/DC100%2/2
"Text::Nunjucks::Lib"->is_string($obj) or $self->_is_safe_string($obj)
"Text::Nunjucks::Lib"->is_string($obj) $self->_is_safe_string($obj)
Truth table100%3/3
A or B
A= "Text::Nunjucks::Lib"->is_string($obj)B= $self->_is_safe_string($obj)
ABresult
1X1
011
000
710 5418 ▶ my $s = $self->_is_safe_string($obj) ? $obj->val : $obj;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_safe_string($obj) ? : 77 5341 5418
711 5418 ▶ return sub { $STRING_METHODS{$key}->( $self, $s, @_ ) } if $STRING_METHODS{$key};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $STRING_METHODS{$key} 5398 20 5418
712 20 return undef;
713 }
714  
715 # Array surface
716 32816 ▶ if ( Text::Nunjucks::Lib->is_array($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Text::Nunjucks::Lib"->is_array($obj)) 32657 159 32816
717 32657 ▶ return sub { $ARRAY_METHODS{$key}->( $self, $obj, @_ ) } if $ARRAY_METHODS{$key};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $ARRAY_METHODS{$key} 32646 11 32657
718 11 return undef;
719 }
720  
721 # Number surface
722 159 ▶ if ( created_as_number($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (created_as_number $obj) 27 132 159
723 0 ▶ return sub { $NUMBER_METHODS{$key}->( $self, $obj, @_ ) } if $NUMBER_METHODS{$key};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Branch: 1 branch
BranchTrueFalseTotal
if $NUMBER_METHODS{$key} 0 27 27
724 27 return undef;
725 }
726  
727 # Hashref (plain or ordered): the FunCall site must produce a coderef, so
728 # we only return one if the value is callable. Upstream: calling a
729 # non-function member on a plain object is a TypeError ("not a function")
730 # -- we mirror that by returning undef and letting call_wrap produce the
731 # exact upstream text.
732 132 ▶ if ( ref($obj) eq 'HASH' || $self->_is_ordered_hash($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH" or $self->_is_ordered_hash($obj)) 108 24 132
Condition100%3/3
ref $obj eq "HASH" or $self->_is_ordered_hash($obj)
l!l&&r!l&&!r
476124
MC/DC100%2/2
ref $obj eq "HASH" or $self->_is_ordered_hash($obj)
ref $obj eq "HASH" $self->_is_ordered_hash($obj)
Truth table100%3/3
A or B
A= ref $obj eq "HASH"B= $self->_is_ordered_hash($obj)
ABresult
1X1
011
000
733 # a Hash::Ordered is a blessed array, never dereferenced as a hash
734 my $fn = $self->_is_ordered_hash($obj)
735 ? ( $obj->exists($key) ? $obj->get($key) : undef )
736 108 ▶ : ( exists( $obj->{$key} ) ? $obj->{$key} : undef );
Branch: 3 branches
BranchTrueFalseTotal
$self->_is_ordered_hash($obj) ? : 61 47 108
$obj->exists($key) ? : 50 11 61
exists $obj->{$key} ? : 39 8 47
737 108 ▶ return Text::Nunjucks::Lib->is_function($fn) ? $fn : undef;
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->is_function($fn) ? : 88 20 108
738 }
739  
740 # Blessed user object: `{{ obj.foo() }} }` needs a bound coderef.
741 24 ▶ if ( blessed($obj) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($obj)) 2 22 24
742 2 ▶ return sub { $obj->$key(@_) } if $obj->can($key);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $obj->can($key) 1 1 2
743 1 return undef;
744 }
745  
746 22 return undef;
747 }
748  
749 # Port addition, ADR 0016 / ADR 0010: a native method call the engine's own
750 # JavaScript makes on a value it never checked -- filters.js runs
751 # str.toLowerCase(), str.split('\n'), input.lastIndexOf(' ', length) on whatever
752 # the template piped in. JS reads the member and calls it: on undefined / null
753 # the read throws, on a value without that method the call does, both as a
754 # TypeError whose text V8 builds from the source expression, so the caller
755 # names the upstream variable ($src). Oracle: 5|lower -> "TypeError:
756 # str.toLowerCase is not a function", undefined|trim -> "TypeError: Cannot read
757 # properties of undefined (reading 'replace')".
758 6170 ▶sub js_method_call {
Subroutine js_method_call: called CC=2 SCAR=6.9
Pod undocumented
759 6170 my ( $self, $obj, $src, $name, @args ) = @_;
760 6170 my $method = $self->member_lookup_method( $obj, $name );
761 6170 ▶ die $self->_js_method_error( $obj, $src, $name ) unless Text::Nunjucks::Lib->is_function($method);
Branch: 1 branch
BranchTrueFalseTotal
unless "Text::Nunjucks::Lib"->is_function($method) 107 6063 6170
762 6063 return $method->(@args);
763 }
764  
765 # Port addition, same seam: the string a String.prototype method the engine
766 # calls with one of its own regexes (str.replace(/\r\n|\n/g, ...),
767 # str.match(/\w+/g)) works on -- the value itself, a SafeString's value (a
768 # String object), else the TypeError js_method_call throws for the member
769 # ($name) upstream calls.
770 1330 ▶sub js_string_this {
Subroutine js_string_this: called CC=3 SCAR=11.0
Pod undocumented
771 1330 my ( $self, $obj, $src, $name ) = @_;
772 1330 ▶ return $obj->val if $self->_is_safe_string($obj);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_safe_string($obj) 28 1302 1330
773 1302 ▶ return $obj if Text::Nunjucks::Lib->is_string($obj);
Branch: 1 branch
BranchTrueFalseTotal
if "Text::Nunjucks::Lib"->is_string($obj) 1262 40 1302
774 40 die $self->_js_method_error( $obj, $src, $name );
775 }
776  
777 147 ▶sub _js_method_error {
Subroutine _js_method_error: called CC=3 SCAR=11.0
778 147 my ( $self, $obj, $src, $name ) = @_;
779 147 ▶ return $self->_js_read_error( $obj, $name ) if !defined($obj) || $self->_is_none($obj);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $obj or $self->_is_none($obj) 24 123 147
Condition100%3/3
not defined $obj or $self->_is_none($obj)
l!l&&r!l&&!r
1311123
MC/DC100%2/2
not defined $obj or $self->_is_none($obj)
not defined $obj $self->_is_none($obj)
Truth table100%3/3
not(A) or B
A= not defined $objB= $self->_is_none($obj)
ABresult
1X1
011
000
780 123 return $self->_error_class->new( $src . '.' . $name . ' is not a function', name => 'TypeError' );
781 }
782  
783 # Port addition, same seam: a plain property read obj[key] the engine's own
784 # JavaScript makes on a value it never checked -- filters.js reads arr[0],
785 # arr.length, v[attr] off whatever the template piped in. On undefined / null
786 # the read throws V8's TypeError (oracle: undefined|first -> "TypeError: Cannot
787 # read properties of undefined (reading '0')", null|last -> "... of null
788 # (reading 'length')"); on anything else it is member_lookup, the JS property
789 # read of the ADR 0008 surface. The key is used as given -- v[attr] with attr
790 # 'a.b' reads the key 'a.b', no path.
791 4835 ▶sub js_member_read {
Subroutine js_member_read: called CC=3 SCAR=11.0
Pod undocumented
792 4835 my ( $self, $obj, $key ) = @_;
793 4835 ▶ die $self->_js_read_error( $obj, $key ) if !defined($obj) || $self->_is_none($obj);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $obj or $self->_is_none($obj) 57 4778 4835
Condition100%3/3
not defined $obj or $self->_is_none($obj)
l!l&&r!l&&!r
43144778
MC/DC100%2/2
not defined $obj or $self->_is_none($obj)
not defined $obj $self->_is_none($obj)
Truth table100%3/3
not(A) or B
A= not defined $objB= $self->_is_none($obj)
ABresult
1X1
011
000
794 4778 return $self->member_lookup( $obj, $key );
795 }
796  
797 # V8's text for a property read on undefined / null. It names the key as V8
798 # prints it without side effects, Object::NoSideEffectsToMaybeString (oracle,
799 # with v undefined in [v]|join(',', k)): a string as is, a number or boolean as
800 # its ToString ('1.5', 'true'), a plain object as '#<Object>' or its
801 # constructor's name; a key with no such form drops the "(reading ...)" clause
802 # -- an array, a SafeString, a regex, a plain object with an own toString
803 # ({'toString': 1}) or an own constructor that is no named function
804 # ({'constructor': 1}).
805 90 ▶sub _js_read_error {
Subroutine _js_read_error: called CC=2 SCAR=6.9
806 90 my ( $self, $obj, $key ) = @_;
807 90 my $name = $self->_js_no_side_effects_to_maybe_string($key);
808 90 ▶ return $self->_error_class->new(
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 72 18 90
809 'Cannot read properties of ' . $self->_js_type($obj) . ( defined($name) ? " (reading '" . $name . "')" : '' ),
810 name => 'TypeError'
811 );
812 }
813  
814 # V8's rendering of a value in a message it formats itself, where it may also
815 # have none (Object::NoSideEffectsToMaybeString, V8 13.6.233.17
816 # src/objects/objects.cc): undefined, null, a boolean, a number and a string
817 # as their ToString -- a string as is, not cut. An object is looked at without
818 # running anything: GetDataProperty(o, 'toString'), an own key first, else the
819 # prototype's. Only an object whose toString is Object.prototype's has a form,
820 # and only through GetDataProperty(o, 'constructor'): a function with a
821 # non-empty name shows as '#<name>'. For the plain objects the port models --
822 # a dict literal, a caller hash, keyword arguments, all with Object.prototype
823 # as their prototype -- that is (oracle, [2, 1].sort(x) and [v]|join(',', x)):
824 # * an own toString key, whatever it holds ({'toString': 1}, {'toString':
825 # u}, toString=1): no form;
826 # * else an own constructor key: '#<name>' when it is a function with a name
827 # ({'constructor': m}, a macro m -> '#<macro>'; in node a function with an
828 # empty name gives no form), else no form ({'constructor': 1}, '', u, none,
829 # [] and a SafeString all have none);
830 # * else Object.prototype.constructor, Object: '#<Object>'.
831 # An array, a regex and a SafeString have a toString of their own: no form. A
832 # function would show its JS source, which has no Perl form; it has none here,
833 # and neither does any other blessed object. Returns undef for no form.
834 189 ▶sub _js_no_side_effects_to_maybe_string {
Subroutine _js_no_side_effects_to_maybe_string: called CC=11 SCAR=24.0
835 189 my ( $self, $v ) = @_;
836 189 ▶ return $self->_js_to_string($v) if !ref($v) || $self->_is_none($v);
Branch: 1 branch
BranchTrueFalseTotal
if not ref $v or $self->_is_none($v) 101 88 189
Condition100%3/3
not ref $v or $self->_is_none($v)
l!l&&r!l&&!r
96588
MC/DC100%2/2
not ref $v or $self->_is_none($v)
not ref $v $self->_is_none($v)
Truth table100%3/3
not(A) or B
A= not ref $vB= $self->_is_none($v)
ABresult
1X1
011
000
837 88 ▶ return undef unless ref($v) eq 'HASH' || $self->_is_ordered_hash($v) || $self->_is_kwargs($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v) 18 70 88
Condition100%6/6
ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v)
l!l&&r!l&&!r
561418
ref $v eq "HASH" or $self->_is_ordered_hash($v)
l!l&&r!l&&!r
332332
MC/DC100%3/3
ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v)
ref $v eq "HASH" $self->_is_ordered_hash($v) $self->_is_kwargs($v)
Truth table100%4/4
A or B or C
A= ref $v eq "HASH"B= $self->_is_ordered_hash($v)C= $self->_is_kwargs($v)
ABCresult
1XX1
01X1
0011
0000
838 my $own = $self->_is_ordered_hash($v)
839 39 ▶ ? sub { $v->exists( $_[0] ) }
Subroutine __ANON__: called CC=1 SCAR=0.0
840 70 ▶ : sub { exists $v->{ $_[0] } };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_ordered_hash($v) ? : 23 47 70
841 70 ▶ return undef if $own->('toString');
Branch: 1 branch
BranchTrueFalseTotal
if &$own("toString") 20 50 70
842 50 ▶ return '#<Object>' unless $own->('constructor');
Branch: 1 branch
BranchTrueFalseTotal
unless &$own("constructor") 19 31 50
843 31 my $ctor = Text::Nunjucks::Lib->_own_value( $v, 'constructor' );
844 31 ▶ my $name = Text::Nunjucks::Lib->is_function($ctor) ? $self->_js_function_name($ctor) : '';
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->is_function($ctor) ? : 8 23 31
845 31 ▶ return $name ne '' ? '#<' . $name . '>' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$name ne '' ? : 3 28 31
846 }
847  
848 # V8's rendering of a value in a message it formats itself
849 # (Object::NoSideEffectsToString; oracle with [2, 1].sort(x), Array sort's
850 # comparison-function TypeError): its NoSideEffectsToMaybeString form, else
851 # '[object ' . its class name . ']' -- a dict literal, caller hash or keyword
852 # arguments with an own toString or a constructor that is no named function
853 # ({'toString': 1}, {'constructor': 1}) as '[object Object]', an array as
854 # '[object Array]', a regex as '[object RegExp]' and a SafeString as '[object
855 # Object]'. No Symbol.toStringTag is reachable from a template. A function
856 # would show its JS source; the one caller never hands it one. Any other
857 # blessed object shows as '#<Object>', a plain object's form (the port's
858 # choice, no JS counterpart to measure).
859 99 ▶sub _js_no_side_effects_to_string {
Subroutine _js_no_side_effects_to_string: called CC=8 SCAR=20.9
860 99 my ( $self, $v ) = @_;
861 99 my $shown = $self->_js_no_side_effects_to_maybe_string($v);
862 99 ▶ return $shown if defined $shown;
Branch: 1 branch
BranchTrueFalseTotal
if defined $shown 51 48 99
863 48 ▶ return '[object Array]' if ref($v) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 5 43 48
864 43 ▶ return '[object RegExp]' if $self->_is_regex($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_regex($v) 3 40 43
865 40 ▶ return '[object Object]'
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v) or $self->_is_safe_string($v) 40 0 40
Condition88%8/9
ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v) or $self->_is_safe_string($v)
l!l&&r!l&&!r
3640
ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v)
l!l&&r!l&&!r
3064
ref $v eq "HASH" or $self->_is_ordered_hash($v)
l!l&&r!l&&!r
201010
MC/DC0%0/4
ref $v eq "HASH" or $self->_is_ordered_hash($v) or $self->_is_kwargs($v) or $self->_is_safe_string($v)
ref $v eq "HASH" $self->_is_ordered_hash($v) $self->_is_kwargs($v) $self->_is_safe_string($v)
Truth table80%4/5
A or B or C or D
A= ref $v eq "HASH"B= $self->_is_ordered_hash($v)C= $self->_is_kwargs($v)D= $self->_is_safe_string($v)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
866 if ref($v) eq 'HASH' || $self->_is_ordered_hash($v) || $self->_is_kwargs($v) || $self->_is_safe_string($v);
867 0 return '#<Object>';
868 }
869  
870 # JSFunction::GetName, the name V8 shows for a function in its own messages.
871 # A template cannot rename a function, so it is the function's name property
872 # as a template reads it: a macro's is 'macro' upstream (makeMacro returns
873 # `function macro(...)`), a caller's function has the name JS gave it. A Perl
874 # coderef owns no name property yet (k88), so this is '' -- V8's "no name" --
875 # until member_lookup gives functions their own properties.
876 11 ▶sub _js_function_name {
Subroutine _js_function_name: called CC=2 SCAR=7.1
877 11 my ( $self, $fn ) = @_;
878 11 my $name = $self->member_lookup( $fn, 'name' );
879 11 ▶ return Text::Nunjucks::Lib->is_string($name) ? $name : '';
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->is_string($name) ? : 0 11 11
880 }
881  
882 # V8's rendering of a value a builtin was handed to call that is not callable
883 # (Array reduce's callbackfn; oracle with [1].reduce(x)): undefined ->
884 # 'undefined', null -> 'object null', a boolean or number -> its type and
885 # ToString ('boolean true', 'number 1.5', 'number NaN'), a string -> 'string
886 # "x"', cut to its first 100 code units and '<...>' when longer, a function ->
887 # 'function', any other object -> 'object'. V8 renders a value it cannot iterate
888 # the same way (js_array_destructure; oracle: [m]|urlencode with a macro m ->
889 # "function is not iterable ...").
890 48 ▶sub _js_not_callable_text {
Subroutine _js_not_callable_text: called CC=8 SCAR=20.8
891 48 my ( $self, $v ) = @_;
892 48 ▶ return 'undefined' if !defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 8 40 48
893 40 ▶ return 'object null' if $self->_is_none($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_none($v) 7 33 40
894 33 my $type = $self->_js_type($v);
895 33 ▶ return $type . ' ' . $self->_js_to_string($v) if $type eq 'boolean' || $type eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "boolean" or $type eq "number" 18 15 33
Condition100%3/3
$type eq "boolean" or $type eq "number"
l!l&&r!l&&!r
41415
MC/DC100%2/2
$type eq "boolean" or $type eq "number"
$type eq "boolean" $type eq "number"
Truth table100%3/3
A or B
A= $type eq "boolean"B= $type eq "number"
ABresult
1X1
011
000
896 15 ▶ return 'function' if Text::Nunjucks::Lib->is_function($v);
Branch: 1 branch
BranchTrueFalseTotal
if "Text::Nunjucks::Lib"->is_function($v) 2 13 15
897 13 ▶ return 'object' unless $type eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
unless $type eq "string" 8 5 13
898 5 ▶ my $shown = Text::Nunjucks::Lib->js_string_length($v) > 100
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->js_string_length($v) > 100 ? : 3 2 5
899 ? Text::Nunjucks::Lib->js_substring( $v, 0, 100 ) . '<...>'
900 : $v;
901 5 return 'string "' . $shown . '"';
902 }
903  
904 # Port addition, same seam: the array destructuring pattern the engine's own
905 # JavaScript binds -- urlencode's keyvals.map(([k, v]) => ...) -- over whatever
906 # the template handed it. ES runs the iteration protocol: an array yields its
907 # elements, a string -- primitive, or a SafeString's value (a String object) --
908 # its code points, a lone surrogate on its own (oracle: ['\u{1F600}b']|urlencode
909 # -> %F0%9F%98%80=b). The first $count come back, undefined past the end. Any
910 # other value has no Symbol.iterator and throws V8's TypeError, the value
911 # rendered as for a call (oracle: [5]|urlencode -> "number 5 is not iterable
912 # (cannot read property Symbol(Symbol.iterator))", [{}] -> "object is not
913 # iterable ...").
914 144 ▶sub js_array_destructure {
Subroutine js_array_destructure: called CC=5 SCAR=16.1
Pod undocumented
915 144 my ( $self, $value, $count ) = @_;
916 144 my $items;
917 144 ▶ if ( Text::Nunjucks::Lib->is_array($value) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ('Text::Nunjucks::Lib'->is_array($value)) { } 112 32 144
elsif ('Text::Nunjucks::Lib'->is_string($value) or $self->_is_safe_string($value)) { } 15 17 32
918 112 $items = $value;
919 ▶ } elsif ( Text::Nunjucks::Lib->is_string($value) || $self->_is_safe_string($value) ) {
Condition100%3/3
"Text::Nunjucks::Lib"->is_string($value) or $self->_is_safe_string($value)
l!l&&r!l&&!r
12317
MC/DC100%2/2
"Text::Nunjucks::Lib"->is_string($value) or $self->_is_safe_string($value)
"Text::Nunjucks::Lib"->is_string($value) $self->_is_safe_string($value)
Truth table100%3/3
A or B
A= "Text::Nunjucks::Lib"->is_string($value)B= $self->_is_safe_string($value)
ABresult
1X1
011
000
920 15 ▶ my $s = $self->_is_safe_string($value) ? $value->val : $value;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_safe_string($value) ? : 3 12 15
921 15 $items = [ split //, Text::Nunjucks::Lib->_js_from_units($s) ];
922 } else {
923 17 die $self->_js_not_iterable_error($value);
924 }
925 127 return CORE::map { $items->[$_] } 0 .. $count - 1;
926 }
927  
928 # Port addition, same seam: value[Symbol.iterator] read for its truth value --
929 # the iterable test's !!value[Symbol.iterator]. Of the values the port models,
930 # an array, a string and a SafeString (a String object, String.prototype's
931 # iterator) have one; a number, a boolean, a plain or ordered object, keyword
932 # arguments, a regex and a function do not. On undefined / null V8 throws its
933 # not-iterable TypeError for the read (oracle: {{ null is iterable }} ->
934 # "TypeError: object null is not iterable (cannot read property
935 # Symbol(Symbol.iterator))"). A Map, a Set and a generator have no Perl image
936 # (k44).
937 27 ▶sub js_has_symbol_iterator {
Subroutine js_has_symbol_iterator: called CC=6 SCAR=17.9
Pod undocumented
938 27 my ( $self, $value ) = @_;
939 27 ▶ die $self->_js_not_iterable_error($value) if !defined($value) || $self->_is_none($value);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $value or $self->_is_none($value) 4 23 27
Condition100%3/3
not defined $value or $self->_is_none($value)
l!l&&r!l&&!r
2223
MC/DC100%2/2
not defined $value or $self->_is_none($value)
not defined $value $self->_is_none($value)
Truth table100%3/3
not(A) or B
A= not defined $valueB= $self->_is_none($value)
ABresult
1X1
011
000
940 return (
941 23 ▶ Text::Nunjucks::Lib->is_array($value)
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->is_array($value) || 'Text::Nunjucks::Lib'->is_string($value) || $self->_is_safe_string($value) ? : 12 11 23
Condition100%6/6
"Text::Nunjucks::Lib"->is_array($value) or "Text::Nunjucks::Lib"->is_string($value) or $self->_is_safe_string($value)
l!l&&r!l&&!r
10211
"Text::Nunjucks::Lib"->is_array($value) or "Text::Nunjucks::Lib"->is_string($value)
l!l&&r!l&&!r
6413
MC/DC100%3/3
"Text::Nunjucks::Lib"->is_array($value) or "Text::Nunjucks::Lib"->is_string($value) or $self->_is_safe_string($value)
"Text::Nunjucks::Lib"->is_array($value) "Text::Nunjucks::Lib"->is_string($value) $self->_is_safe_string($value)
Truth table100%4/4
A or B or C
A= "Text::Nunjucks::Lib"->is_array($value)B= "Text::Nunjucks::Lib"->is_string($value)C= $self->_is_safe_string($value)
ABCresult
1XX1
01X1
0011
0000
942 || Text::Nunjucks::Lib->is_string($value)
943 || $self->_is_safe_string($value)
944 ) ? 1 : 0;
945 }
946  
947 21 ▶sub _js_not_iterable_error {
Subroutine _js_not_iterable_error: called CC=1 SCAR=0.0
948 21 my ( $self, $value ) = @_;
949 21 return $self->_error_class->new(
950 $self->_js_not_callable_text($value) . ' is not iterable (cannot read property Symbol(Symbol.iterator))',
951 name => 'TypeError'
952 );
953 }
954  
955 #### callWrap
956  
957 34504 ▶sub call_wrap {
Subroutine call_wrap: called CC=3 SCAR=11.0
Pod undocumented
958 34504 my ( $self, $obj, $name, $context, $args ) = @_;
959  
960 34504 ▶ if ( $self->_falsy($obj) ) { # JS: if (!obj)
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_falsy($obj)) { } 91 34413 34504
elsif (not 'Text::Nunjucks::Lib'->is_function($obj)) { } 2 34411 34413
961 91 die $self->_error_class->new(
962 'Unable to call `' . $name . '`, which is undefined or falsey' );
963 } elsif ( !Text::Nunjucks::Lib->is_function($obj) ) { # typeof obj !== 'function'
964 2 die $self->_error_class->new(
965 'Unable to call `' . $name . '`, which is not a function' );
966 }
967  
968 # JS: obj.apply(context, args). The compiler binds member-call owners inside
969 # member_lookup; a bare context coderef gets no `this` (ADR 0009), so the
970 # Context passed as $context is not used as an invocant here.
971 # A macro that calls itself N deep comes through here N calls deep, as in
972 # make_macro (k83).
973 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
974 34411 return $obj->( @$args );
975 }
976  
977 #### contextOrFrameLookup
978  
979 74451 ▶sub context_or_frame_lookup {
Subroutine context_or_frame_lookup: called CC=2 SCAR=6.9
Pod undocumented
980 74451 my ( $self, $context, $frame, $name ) = @_;
981 74451 my $val = $frame->lookup($name);
982 74451 ▶ return defined($val) ? $val : $context->lookup($name); # val !== undefined
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 36283 38168 74451
983 }
984  
985 #### handleError
986  
987 448 ▶sub handle_error {
Subroutine handle_error: called CC=4 SCAR=13.9
Pod undocumented
988 448 my ( $self, $error, $lineno, $colno ) = @_;
989 # JS: if (error.lineno) return error; -- an already-positioned TemplateError.
990 448 ▶ if ( blessed($error) && $error->can('lineno') && $error->lineno ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($error) and $error->can("lineno") and $error->lineno) 2 446 448
Condition100%6/6
builtin::blessed($error) and $error->can("lineno") and $error->lineno
!ll&&!rl&&r
426202
builtin::blessed($error) and $error->can("lineno")
!ll&&!rl&&r
442222
MC/DC100%3/3
builtin::blessed($error) and $error->can("lineno") and $error->lineno
builtin::blessed($error) $error->can("lineno") $error->lineno
Truth table100%4/4
A and B and C
A= builtin::blessed($error)B= $error->can("lineno")C= $error->lineno
ABCresult
0XX0
10X0
1100
1111
991 2 return $error;
992 }
993 446 return $self->_template_error_class->new( $error, $lineno, $colno );
994 }
995  
996 #### asyncEach
997  
998 1310 ▶sub async_each {
Subroutine async_each: called CC=3 SCAR=11.0
Pod undocumented
999 1310 my ( $self, $arr, $dimen, $iter, $cb ) = @_;
1000  
1001 # Port-only (k83): the loop body $iter is entered once per item before the
1002 # previous item's call has returned, as upstream nests one JS call per item
1003 # (ADR 0003); an asyncEach inside a template that includes itself enters
1004 # async_iter / async_for once per level. Perl warns at the 100th nested call
1005 # of a sub, at the call site; the depth is bounded by memory, not by Perl,
1006 # so that warning is off for the calls below (see Lib async_iter).
1007 23 ▶ no warnings 'recursion';
Subroutine BEGIN: called CC=1 SCAR=0.0
1008  
1009 1310 ▶ if ( Text::Nunjucks::Lib->is_array($arr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Text::Nunjucks::Lib'->is_array($arr)) { } 799 511 1310
1010 799 my $len = scalar @$arr;
1011  
1012 Text::Nunjucks::Lib->async_iter(
1013 $arr,
1014 3245 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1015 3245 my ( $item, $i, $next ) = @_;
1016 3245 ▶ if ( $dimen == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dimen == 1) { } 1788 1457 3245
elsif ($dimen == 2) { } 493 964 1457
elsif ($dimen == 3) { } 482 482 964
1017 1788 $iter->( $item, $i, $len, $next );
1018 } elsif ( $dimen == 2 ) {
1019 493 $iter->( $item->[0], $item->[1], $i, $len, $next );
1020 } elsif ( $dimen == 3 ) {
1021 482 $iter->( $item->[0], $item->[1], $item->[2], $i, $len, $next );
1022 } else {
1023 482 push @$item, $i, $len, $next;
1024 482 $iter->(@$item);
1025 }
1026 },
1027 799 $cb
1028 );
1029 } else {
1030 # JS lib.asyncFor does keys(arr || {}); that guard must recognise None (JS
1031 # null), whose home is Runtime (ADR 0006), so normalise the falsy iterable
1032 # here before delegating to Lib (which is None-agnostic by design).
1033 Text::Nunjucks::Lib->async_for(
1034 $self->_falsy($arr) ? {} : $arr,
1035 1000 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
1036 1000 my ( $key, $val, $i, $len, $next ) = @_;
1037 1000 $iter->( $key, $val, $i, $len, $next );
1038 },
1039 511 ▶ $cb
Branch: 1 branch
BranchTrueFalseTotal
$self->_falsy($arr) ? : 7 504 511
1040 );
1041 }
1042 }
1043  
1044 #### asyncAll
1045  
1046 62 ▶sub async_all {
Subroutine async_all: called CC=10 SCAR=23.0
Pod undocumented
1047 62 my ( $self, $arr, $dimen, $func, $cb ) = @_;
1048  
1049 62 my $finished = 0;
1050 62 my $len;
1051 my @output_arr;
1052  
1053 129 ▶ my $done = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
1054 129 my ( $i, $output ) = @_;
1055 129 $finished++;
1056 129 $output_arr[$i] = $output;
1057  
1058 129 ▶ if ( $finished == $len ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($finished == $len) 45 84 129
1059 # outputArr.join(''), joined in code units (ADR 0012)
1060 45 my $joined = '';
1061 45 ▶ Text::Nunjucks::Lib->js_string_append( $joined, defined($_) ? $_ : '' ) for @output_arr;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 123 0 123
1062 45 $cb->( undef, $joined );
1063 }
1064 62 };
1065  
1066 62 ▶ if ( Text::Nunjucks::Lib->is_array($arr) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Text::Nunjucks::Lib'->is_array($arr)) { } 42 20 62
1067 42 $len = scalar @$arr;
1068  
1069 42 ▶ if ( $len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) { } 2 40 42
1070 2 $cb->( undef, '' );
1071 } else {
1072 40 ▶ for ( my $i = 0; $i < scalar @$arr; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$arr) 110 40 150
1073 110 my $item = $arr->[$i];
1074 110 ▶ if ( $dimen == 1 ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($dimen == 1) { } 97 13 110
elsif ($dimen == 2) { } 9 4 13
elsif ($dimen == 3) { } 2 2 4
1075 97 $func->( $item, $i, $len, $done );
1076 } elsif ( $dimen == 2 ) {
1077 9 $func->( $item->[0], $item->[1], $i, $len, $done );
1078 } elsif ( $dimen == 3 ) {
1079 2 $func->( $item->[0], $item->[1], $item->[2], $i, $len, $done );
1080 } else {
1081 2 push @$item, $i, $len, $done;
1082 2 $func->(@$item);
1083 }
1084 }
1085 }
1086 } else {
1087 # JS keys(arr || {}) -- JS-falsiness, None-aware (ADR 0006), not just `// {}`
1088 # (which would let a None through and crash Lib->keys on the deref).
1089 20 ▶ my $keys = Text::Nunjucks::Lib->keys( $self->_falsy($arr) ? {} : $arr );
Branch: 1 branch
BranchTrueFalseTotal
$self->_falsy($arr) ? : 7 13 20
1090 20 $len = scalar @$keys;
1091  
1092 20 ▶ if ( $len == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len == 0) { } 7 13 20
1093 7 $cb->( undef, '' );
1094 } else {
1095 # JS: func(k, arr[k], i, len, done) -- property access. The same lookup
1096 # seam the runtime uses for member access (member_lookup) handles
1097 # Hash::Ordered (template dict literal / engine-built object, ADR 0011)
1098 # and plain hashrefs, matching arr[k] on a JS plain object; calling it
1099 # here keeps Runtime::async_all on one seam instead of duplicating the
1100 # Hash::Ordered branch that Lib::async_for already has.
1101 13 ▶ for ( my $i = 0; $i < scalar @$keys; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$keys) 29 13 42
1102 29 my $k = $keys->[$i];
1103 29 $func->( $k, $self->member_lookup( $arr, $k ), $i, $len, $done );
1104 }
1105 }
1106 }
1107 }
1108  
1109 #### fromIterator
1110  
1111 2160 ▶sub from_iterator {
Subroutine from_iterator: called CC=5 SCAR=16.1
Pod undocumented
1112 2160 my ( $self, $arr ) = @_;
1113 # JS returns arr unless it is a non-null, non-array object with Symbol.iterator
1114 # (then Array.from). Perl core has no iterator protocol, so the conversion
1115 # branch is inert and every value passes through unchanged; a future blessed
1116 # iterable would hook in here.
1117 2160 ▶ return $arr if !defined($arr) || !ref($arr) || $self->_is_none($arr);
Branch: 1 branch
BranchTrueFalseTotal
if not defined $arr && ref $arr or $self->_is_none($arr) 15 2145 2160
Condition100%6/6
not defined $arr && ref $arr or $self->_is_none($arr)
l!l&&r!l&&!r
1412145
defined $arr && ref $arr
!ll&&!rl&&r
772146
MC/DC100%3/3
not defined $arr && ref $arr or $self->_is_none($arr)
defined $arr ref $arr $self->_is_none($arr)
Truth table100%4/4
not(A && B) or C
A= defined $arrB= ref $arrC= $self->_is_none($arr)
ABCresult
0XX1
10X1
1111
1100
1118 2145 ▶ return $arr if Text::Nunjucks::Lib->is_array($arr);
Branch: 1 branch
BranchTrueFalseTotal
if "Text::Nunjucks::Lib"->is_array($arr) 1492 653 2145
1119 653 return $arr;
1120 }
1121  
1122 #### re-exported lib helpers (module.exports: isArray, keys, inOperator)
1123  
1124 170 ▶sub is_array { my ( $self, $obj ) = @_; Text::Nunjucks::Lib->is_array($obj) }
Subroutine is_array: called CC=1 SCAR=0.0
Pod undocumented
1125 135 ▶sub keys { my ( $self, $obj ) = @_; Text::Nunjucks::Lib->keys($obj) }
Subroutine keys: called CC=1 SCAR=0.0
Pod undocumented
1126 201 ▶sub in_operator {
Subroutine in_operator: called CC=1 SCAR=0.0
Pod undocumented
1127 201 my ( $self, $key, $val ) = @_;
1128 201 return Text::Nunjucks::Lib->in_operator( $key, $val );
1129 }
1130  
1131 #### string seams the compiled code calls (ADR 0012) -- port additions
1132  
1133 # The output buffer: every `buffer += value` the compiler emits is this call, so
1134 # two halves of a pair written one after the other come out as the one astral
1135 # character, as they do in JS's buffer. The buffer is passed on aliased ($_[1]
1136 # after the shift), so Lib appends to the template's variable in place.
1137 167100 ▶sub js_string_append { shift; Text::Nunjucks::Lib->js_string_append(@_) }
Subroutine js_string_append: called CC=1 SCAR=0.0
Pod undocumented
1138  
1139 # The single-name {% for %} reads arr.length once and arr[i] per element. On a
1140 # primitive string both are code-unit operations that measure the whole string,
1141 # so per element they would make the loop O(n^2); the loop indexes the string's
1142 # split('') array instead -- the same length and the same arr[i] for every i, as
1143 # a string cannot change under the loop. Anything else passes through, a
1144 # SafeString included (a String object: its arr[i] is undefined, not a unit).
1145 850 ▶sub _for_code_units {
Subroutine _for_code_units: called CC=2 SCAR=6.9
1146 850 my ( $self, $arr ) = @_;
1147 850 ▶ return Text::Nunjucks::Lib->is_string($arr) ? Text::Nunjucks::Lib->js_split( $arr, '' ) : $arr;
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->is_string($arr) ? : 20 830 850
1148 }
1149  
1150 #### ------------------------------------------------------------------------
1151 #### JS value model: ToNumber / ToPrimitive / ToString, truthiness (ADR 0007,
1152 #### 0006, 0001). Port additions -- Perl's native operators lack JS semantics.
1153 #### ------------------------------------------------------------------------
1154  
1155 1047453 ▶sub _dbl { return unpack( 'd', pack( 'd', $_[1] ) ) } # force to an IEEE double
Subroutine _dbl: called CC=1 SCAR=0.0
1156  
1157 # JS typeof-ish classifier over the value kinds the port models.
1158 74455 ▶sub _js_type {
Subroutine _js_type: called CC=6 SCAR=17.9
1159 74455 my ( $self, $v ) = @_;
1160 74455 ▶ return 'undefined' if !defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 404 74051 74455
1161 74051 ▶ return 'null' if $self->_is_none($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_none($v) 216 73835 74051
1162 73835 ▶ return 'boolean' if is_bool($v);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::is_bool($v) 221 73614 73835
1163 73614 ▶ return 'number' if created_as_number($v);
Branch: 1 branch
BranchTrueFalseTotal
if created_as_number $v 70410 3204 73614
1164 3204 ▶ return 'string' if !ref($v);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 3045 159 3204
1165 159 return 'object'; # array / hash / SafeString / blessed
1166 }
1167  
1168 # JS truthiness. null (a ref) is falsy, so it is handled before delegating the
1169 # kind-based part to Lib->_falsy (which keeps '0' and empty refs truthy).
1170 605352 ▶sub _falsy {
Subroutine _falsy: called CC=2 SCAR=6.9
1171 605352 my ( $self, $v ) = @_;
1172 605352 ▶ return 1 if $self->_is_none($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_none($v) 122 605230 605352
1173 605230 return Text::Nunjucks::Lib->_falsy($v);
1174 }
1175  
1176 349894 ▶sub _truthy {
Subroutine _truthy: called CC=2 SCAR=6.9
1177 349894 my ( $self, $v ) = @_;
1178 349894 ▶ return $self->_falsy($v) ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$self->_falsy($v) ? : 317889 32005 349894
1179 }
1180  
1181 # ECMAScript ToString. null -> "null", undefined -> "undefined" (Array.join and
1182 # suppress_value remap those to '' themselves). Container stringification lives
1183 # here (js-semantics: "mechanism in Runtime") and is order-independent, so it
1184 # does not touch the open k3 object-key-order decision.
1185 489220 ▶sub _js_to_string {
Subroutine _js_to_string: called CC=10 SCAR=23.0
1186 489220 my ( $self, $v ) = @_;
1187 489220 ▶ return 'undefined' if !defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 75 489145 489220
1188 489145 ▶ return 'null' if $self->_is_none($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_none($v) 563 488582 489145
1189 488582 ▶ return ( $v ? 'true' : 'false' ) if is_bool($v);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($v) 1781 486801 488582
$v ? : 1167 614 1781
1190 486801 ▶ return Text::Nunjucks::Lib->number_to_string($v) if created_as_number($v);
Branch: 1 branch
BranchTrueFalseTotal
if created_as_number $v 227807 258994 486801
1191 258994 ▶ return $v unless ref($v); # string
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 244230 14764 258994
1192 14764 ▶ return $v->val if $self->_is_safe_string($v); # SafeString -> value
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_safe_string($v) 4016 10748 14764
1193 10748 ▶ return $self->_array_join( $v, ',' ) if ref($v) eq 'ARRAY';# Array.toString => join(',')
Branch: 1 branch
BranchTrueFalseTotal
if ref $v eq "ARRAY" 10058 690 10748
1194 690 ▶ return "$v" if $self->_is_regex($v); # Regex.toString -> /source/flags
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_regex($v) 9 681 690
1195 681 return '[object Object]'; # object / other ref (overloaded objects: k8)
1196 }
1197  
1198 # Array.prototype.join: null/undefined elements become '' (not "null"); the
1199 # pieces are joined in code units (ADR 0012), so split('').join('') restores
1200 # a string whose astral characters split() cut into halves.
1201 11517 ▶sub _array_join {
Subroutine _array_join: called CC=5 SCAR=16.1
1202 11517 my ( $self, $arr, $sep ) = @_;
1203 11517 my $out = '';
1204 11517 for my $i ( 0 .. $#$arr ) {
1205 32721 my $v = $arr->[$i];
1206 32721 ▶ Text::Nunjucks::Lib->js_string_append( $out, $sep ) if $i > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $i > 0 21816 10905 32721
1207 32721 ▶ Text::Nunjucks::Lib->js_string_append( $out,
Branch: 1 branch
BranchTrueFalseTotal
!defined($v) || $self->_is_none($v) ? : 694 32027 32721
Condition100%3/3
not defined $v or $self->_is_none($v)
l!l&&r!l&&!r
26243232027
MC/DC100%2/2
not defined $v or $self->_is_none($v)
not defined $v $self->_is_none($v)
Truth table100%3/3
not(A) or B
A= not defined $vB= $self->_is_none($v)
ABresult
1X1
011
000
1208 ( !defined($v) || $self->_is_none($v) ) ? '' : $self->_js_to_string($v) );
1209 }
1210 11517 return $out;
1211 }
1212  
1213 172770 ▶sub _to_primitive {
Subroutine _to_primitive: called CC=5 SCAR=16.1
1214 172770 my ( $self, $v ) = @_;
1215 172770 ▶ return $v if !defined($v) || $self->_is_none($v) || !ref($v); # already primitive
Branch: 1 branch
BranchTrueFalseTotal
if not defined $v or $self->_is_none($v) or not ref $v 168022 4748 172770
Condition100%6/6
not defined $v or $self->_is_none($v) or not ref $v
l!l&&r!l&&!r
3661676564748
not defined $v or $self->_is_none($v)
l!l&&r!l&&!r
129237172404
MC/DC100%3/3
not defined $v or $self->_is_none($v) or not ref $v
not defined $v $self->_is_none($v) not ref $v
Truth table100%4/4
not(A) or B or not(C)
A= not defined $vB= $self->_is_none($v)C= not ref $v
ABCresult
1XX1
01X1
0011
0000
1216 4748 ▶ return $v->val if $self->_is_safe_string($v);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_safe_string($v) 52 4696 4748
1217 4696 return $self->_js_to_string($v); # array/object -> ToString
1218 }
1219  
1220 # ECMAScript Number(string): trim JS whitespace, '' -> 0, Infinity spellings,
1221 # 0x/0o/0b radixes, else a valid decimal literal or NaN.
1222 119406 ▶sub _string_to_number {
Subroutine _string_to_number: called CC=9 SCAR=22.0
1223 119406 my ( $self, $s ) = @_;
1224 119406 my $ws = Text::Nunjucks::Lib->js_whitespace_class;
1225 119406 ( my $t = $s ) =~ s/\A(?:$ws)+//;
1226 119406 $t =~ s/(?:$ws)+\z//;
1227  
1228 119406 ▶ return 0 if $t eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "" 177 119229 119406
1229 119229 ▶ return $POS_INF if $t eq 'Infinity' || $t eq '+Infinity';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "Infinity" or $t eq "+Infinity" 24 119205 119229
Condition100%3/3
$t eq "Infinity" or $t eq "+Infinity"
l!l&&r!l&&!r
222119205
MC/DC100%2/2
$t eq "Infinity" or $t eq "+Infinity"
$t eq "Infinity" $t eq "+Infinity"
Truth table100%3/3
A or B
A= $t eq "Infinity"B= $t eq "+Infinity"
ABresult
1X1
011
000
1230 119205 ▶ return $NEG_INF if $t eq '-Infinity';
Branch: 1 branch
BranchTrueFalseTotal
if $t eq "-Infinity" 15 119190 119205
1231 119190 ▶ if ( $t =~ /\A0[xX][0-9a-fA-F]+\z/
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /\A0[xX][0-9a-fA-F]+\z/ or $t =~ /\A0[oO][0-7]+\z/ or $t =~ /\A0[bB][01]+\z/) 64 119126 119190
Condition100%6/6
$t =~ /\A0[xX][0-9a-fA-F]+\z/ or $t =~ /\A0[oO][0-7]+\z/ or $t =~ /\A0[bB][01]+\z/
l!l&&r!l&&!r
568119126
$t =~ /\A0[xX][0-9a-fA-F]+\z/ or $t =~ /\A0[oO][0-7]+\z/
l!l&&r!l&&!r
542119134
MC/DC100%3/3
$t =~ /\A0[xX][0-9a-fA-F]+\z/ or $t =~ /\A0[oO][0-7]+\z/ or $t =~ /\A0[bB][01]+\z/
$t =~ /\A0[xX][0-9a-fA-F]+\z/ $t =~ /\A0[oO][0-7]+\z/ $t =~ /\A0[bB][01]+\z/
Truth table100%4/4
A or B or C
A= $t =~ /\A0[xX][0-9a-fA-F]+\z/B= $t =~ /\A0[oO][0-7]+\z/C= $t =~ /\A0[bB][01]+\z/
ABCresult
1XX1
01X1
0011
0000
1232 || $t =~ /\A0[oO][0-7]+\z/
1233 || $t =~ /\A0[bB][01]+\z/ )
1234 {
1235 64 return oct($t) + 0.0;
1236 }
1237 119126 ▶ if ( $t =~ /\A[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($t =~ /\A[+-]?(?:[0-9]+\.?[0-9]*|\.[0-9]+)(?:[eE][+-]?[0-9]+)?\z/) 116637 2489 119126
1238 116637 return 0 + $t;
1239 }
1240 2489 return $NAN;
1241 }
1242  
1243 # ECMAScript ToNumber, always returning a real IEEE double so the operators run
1244 # in JS double arithmetic (Perl would otherwise keep exact 64-bit integers).
1245 903671 ▶sub _to_number {
Subroutine _to_number: called CC=8 SCAR=20.8
1246 903671 my ( $self, $v ) = @_;
1247 903671 my $n;
1248 903671 ▶ if ( !defined($v) ) { $n = $NAN } # undefined -> NaN
Branch: 6 branches
BranchTrueFalseTotal
if (not defined $v) { } 1677 901994 903671
elsif ($self->_is_none($v)) { } 1123 900871 901994
elsif (builtin::is_bool($v)) { } 2009 898862 900871
elsif (created_as_number $v) { } 774836 124026 898862
elsif ($self->_is_safe_string($v)) { } 103116 20910 124026
elsif (not ref $v) { } 16290 4620 20910
1249 1123 elsif ( $self->_is_none($v) ) { $n = 0 } # null -> 0
1250 2009 ▶ elsif ( is_bool($v) ) { $n = $v ? 1 : 0 }
Branch: 1 branch
BranchTrueFalseTotal
$v ? : 953 1056 2009
1251 774836 elsif ( created_as_number($v) ){ $n = $v }
1252 103116 elsif ( $self->_is_safe_string($v) ) { $n = $self->_string_to_number( $v->val ) }
1253 16290 elsif ( !ref($v) ) { $n = $self->_string_to_number($v) }
1254 4620 else { $n = $self->_to_number( $self->_to_primitive($v) ) }
1255 903671 return $self->_dbl($n);
1256 }
1257  
1258 #### JS operators the compiler emits (compiler.js compileAdd/Sub/.../Compare)
1259  
1260 12490 ▶sub op_add {
Subroutine op_add: called CC=3 SCAR=11.0
Pod undocumented
1261 12490 my ( $self, $a, $b ) = @_;
1262 12490 my $pa = $self->_to_primitive($a);
1263 12490 my $pb = $self->_to_primitive($b);
1264 # JS +: string concat if either primitive is a string, else numeric add.
1265 12490 ▶ if ( Text::Nunjucks::Lib->is_string($pa) || Text::Nunjucks::Lib->is_string($pb) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ("Text::Nunjucks::Lib"->is_string($pa) or "Text::Nunjucks::Lib"->is_string($pb)) 543 11947 12490
Condition100%3/3
"Text::Nunjucks::Lib"->is_string($pa) or "Text::Nunjucks::Lib"->is_string($pb)
l!l&&r!l&&!r
4935011947
MC/DC100%2/2
"Text::Nunjucks::Lib"->is_string($pa) or "Text::Nunjucks::Lib"->is_string($pb)
"Text::Nunjucks::Lib"->is_string($pa) "Text::Nunjucks::Lib"->is_string($pb)
Truth table100%3/3
A or B
A= "Text::Nunjucks::Lib"->is_string($pa)B= "Text::Nunjucks::Lib"->is_string($pb)
ABresult
1X1
011
000
1266 543 my $s = $self->_js_to_string($pa);
1267 543 Text::Nunjucks::Lib->js_string_append( $s, $self->_js_to_string($pb) ); # ADR 0012
1268 543 return $s;
1269 }
1270 11947 return $self->_dbl( $self->_to_number($pa) + $self->_to_number($pb) );
1271 }
1272  
1273 # `~` operator -- compiler emits `a + "" + b`, i.e. String(a) . String(b),
1274 # joined in code units (ADR 0012).
1275 57127 ▶sub op_concat {
Subroutine op_concat: called CC=1 SCAR=0.0
Pod undocumented
1276 57127 my ( $self, $a, $b ) = @_;
1277 57127 my $s = $self->_js_to_string($a);
1278 57127 Text::Nunjucks::Lib->js_string_append( $s, $self->_js_to_string($b) );
1279 57127 return $s;
1280 }
1281  
1282 93640 ▶sub op_sub { my ( $self, $a, $b ) = @_; $self->_dbl( $self->_to_number($a) - $self->_to_number($b) ) }
Subroutine op_sub: called CC=1 SCAR=0.0
Pod undocumented
1283  
1284 19574 ▶sub op_mul {
Subroutine op_mul: called CC=2 SCAR=6.9
Pod undocumented
1285 19574 my ( $self, $a, $b ) = @_;
1286 19574 my $na = $self->_to_number($a);
1287 19574 my $nb = $self->_to_number($b);
1288 19574 my $r = $na * $nb;
1289 # IEEE keeps a zero product's sign as sign(a) xor sign(b) (so 0 * -1 == -0),
1290 # but Perl collapses it to +0; a later `1 / (0 * -1)` then needs the -0 (F3).
1291 # Restore the sign via copysign when the product is zero.
1292 19574 ▶ $r = POSIX::copysign( 0, POSIX::copysign( 1, $na ) * POSIX::copysign( 1, $nb ) )
Branch: 1 branch
BranchTrueFalseTotal
if $r == 0 340 19234 19574
1293 if $r == 0;
1294 19574 return $self->_dbl($r);
1295 }
1296  
1297 1336 ▶sub op_div {
Subroutine op_div: called CC=6 SCAR=17.9
Pod undocumented
1298 1336 my ( $self, $a, $b ) = @_;
1299 1336 my $na = $self->_to_number($a);
1300 1336 my $nb = $self->_to_number($b);
1301 1336 ▶ return $NAN if $na != $na || $nb != $nb;
Branch: 1 branch
BranchTrueFalseTotal
if $na != $na or $nb != $nb 96 1240 1336
Condition100%3/3
$na != $na or $nb != $nb
l!l&&r!l&&!r
8881240
MC/DC100%2/2
$na != $na or $nb != $nb
$na != $na $nb != $nb
Truth table100%3/3
A or B
A= $na != $naB= $nb != $nb
ABresult
1X1
011
000
1302 1240 ▶ if ( $nb == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nb == 0) 265 975 1240
1303 265 ▶ return $NAN if $na == 0; # 0 / 0 (any signs) -> NaN
Branch: 1 branch
BranchTrueFalseTotal
if $na == 0 93 172 265
1304 # x / +-0 -> +-Infinity with sign = sign(x) xor sign(0); copysign reads the
1305 # -0 divisor too, so 1/-0 -> -Infinity and -1/-0 -> Infinity (F3).
1306 172 my $sign = POSIX::copysign( 1, $na ) * POSIX::copysign( 1, $nb );
1307 172 ▶ return $sign < 0 ? $NEG_INF : $POS_INF;
Branch: 1 branch
BranchTrueFalseTotal
$sign < 0 ? : 63 109 172
1308 }
1309 975 return $self->_dbl( $na / $nb );
1310 }
1311  
1312 11365 ▶sub op_mod {
Subroutine op_mod: called CC=4 SCAR=13.9
Pod undocumented
1313 11365 my ( $self, $a, $b ) = @_;
1314 11365 my $na = $self->_to_number($a);
1315 11365 my $nb = $self->_to_number($b);
1316 11365 ▶ return $NAN if $na != $na || $nb != $nb || $nb == 0; # JS n % 0 -> NaN
Branch: 1 branch
BranchTrueFalseTotal
if $na != $na or $nb != $nb or $nb == 0 123 11242 11365
Condition100%6/6
$na != $na or $nb != $nb or $nb == 0
l!l&&r!l&&!r
744911242
$na != $na or $nb != $nb
l!l&&r!l&&!r
205411291
MC/DC100%3/3
$na != $na or $nb != $nb or $nb == 0
$na != $na $nb != $nb $nb == 0
Truth table100%4/4
A or B or C
A= $na != $naB= $nb != $nbC= $nb == 0
ABCresult
1XX1
01X1
0011
0000
1317 11242 return $self->_dbl( POSIX::fmod( $na, $nb ) ); # JS % keeps sign of dividend
1318 }
1319  
1320 56 ▶sub op_pow {
Subroutine op_pow: called CC=1 SCAR=0.0
Pod undocumented
1321 56 my ( $self, $a, $b ) = @_;
1322 # compiler.js emits Math.pow(a, b): the one Lib seam (1 ** Infinity is NaN)
1323 56 return $self->_dbl( Text::Nunjucks::Lib->js_math_pow( $self->_to_number($a), $self->_to_number($b) ) );
1324 }
1325  
1326 6 ▶sub op_floordiv {
Subroutine op_floordiv: called CC=8 SCAR=28.3
Pod undocumented
1327 6 my ( $self, $a, $b ) = @_;
1328 6 my $na = $self->_to_number($a);
1329 6 my $nb = $self->_to_number($b);
1330 6 my $q;
1331 6 ▶ if ( $nb == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nb == 0) { } 0 6 6
1332 0 ▶ return $NAN if $na != $na || $na == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $na != $na or $na == 0 0 0 0
Condition0%0/3
$na != $na or $na == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$na != $na or $na == 0
$na != $na $na == 0
Truth table0%0/3
A or B
A= $na != $naB= $na == 0
ABresult
1X1
011
000
1333 0 ▶ $q = ( $na < 0 ) ? $NEG_INF : $POS_INF;
Branch: 1 branch
BranchTrueFalseTotal
$na < 0 ? : 0 0 0
1334 } else {
1335 6 $q = $na / $nb;
1336 }
1337 6 ▶ return $q if $q != $q; # NaN
Branch: 1 branch
BranchTrueFalseTotal
if $q != $q 0 6 6
1338 6 ▶ return $q if $q == $POS_INF || $q == $NEG_INF; # Math.floor(+-Infinity)
Branch: 1 branch
BranchTrueFalseTotal
if $q == $POS_INF or $q == $NEG_INF 0 6 6
Condition33%1/3
$q == $POS_INF or $q == $NEG_INF
l!l&&r!l&&!r
006
MC/DC0%0/2
$q == $POS_INF or $q == $NEG_INF
$q == $POS_INF $q == $NEG_INF
Truth table33%1/3
A or B
A= $q == $POS_INFB= $q == $NEG_INF
ABresult
1X1
011
000
1339 6 return $self->_dbl( POSIX::floor($q) ); # Math.floor(a / b)
1340 }
1341  
1342 6276 ▶sub op_neg { my ( $self, $a ) = @_; $self->_dbl( -$self->_to_number($a) ) }
Subroutine op_neg: called CC=1 SCAR=0.0
Pod undocumented
1343 66 ▶sub op_pos { my ( $self, $a ) = @_; $self->_dbl( $self->_to_number($a) ) }
Subroutine op_pos: called CC=1 SCAR=0.0
Pod undocumented
1344  
1345 # and / or return the operand (not a boolean); not returns a boolean.
1346 12019 ▶sub op_or { my ( $self, $a, $b ) = @_; $self->_falsy($a) ? $b : $a }
Subroutine op_or: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->_falsy($a) ? : 10920 1099 12019
1347 3949 ▶sub op_and { my ( $self, $a, $b ) = @_; $self->_falsy($a) ? $a : $b }
Subroutine op_and: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->_falsy($a) ? : 63 3886 3949
1348 3823 ▶sub op_not { my ( $self, $a ) = @_; $self->_falsy($a) ? true : false }
Subroutine op_not: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->_falsy($a) ? : 3369 454 3823
1349  
1350 189 ▶sub op_eq { my ( $self, $a, $b ) = @_; $self->_loose_eq( $a, $b ) ? true : false }
Subroutine op_eq: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->_loose_eq($a, $b) ? : 86 103 189
1351 6 ▶sub op_ne { my ( $self, $a, $b ) = @_; $self->_loose_eq( $a, $b ) ? false : true }
Subroutine op_ne: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$self->_loose_eq($a, $b) ? : 3 3 6
1352 4848 ▶sub op_seq { my ( $self, $a, $b ) = @_; Text::Nunjucks::Lib->_strict_eq( $a, $b ) ? true : false }
Subroutine op_seq: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->_strict_eq($a, $b) ? : 1583 3265 4848
1353 3319 ▶sub op_sne { my ( $self, $a, $b ) = @_; Text::Nunjucks::Lib->_strict_eq( $a, $b ) ? false : true }
Subroutine op_sne: called CC=2 SCAR=6.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->_strict_eq($a, $b) ? : 2401 918 3319
1354  
1355 55538 ▶sub op_lt { my ( $self, $a, $b ) = @_; my $r = $self->_less_than( $a, $b ); ( defined($r) && $r ) ? true : false }
Subroutine op_lt: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $r && $r ? : 19048 36490 55538
Condition100%3/3
defined $r and $r
!ll&&!rl&&r
2493624119048
MC/DC100%2/2
defined $r and $r
defined $r $r
Truth table100%3/3
A and B
A= defined $rB= $r
ABresult
0X0
100
111
1356 15786 ▶sub op_gt { my ( $self, $a, $b ) = @_; my $r = $self->_less_than( $b, $a ); ( defined($r) && $r ) ? true : false }
Subroutine op_gt: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $r && $r ? : 13396 2390 15786
Condition100%3/3
defined $r and $r
!ll&&!rl&&r
214217613396
MC/DC100%2/2
defined $r and $r
defined $r $r
Truth table100%3/3
A and B
A= defined $rB= $r
ABresult
0X0
100
111
1357 136 ▶sub op_le { my ( $self, $a, $b ) = @_; my $r = $self->_less_than( $b, $a ); ( defined($r) && !$r ) ? true : false }
Subroutine op_le: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $r && !$r ? : 46 90 136
Condition100%3/3
defined $r and not $r
!ll&&!rl&&r
88246
MC/DC100%2/2
defined $r and not $r
defined $r not $r
Truth table100%3/3
A and not(B)
A= defined $rB= not $r
ABresult
0X0
100
111
1358 124 ▶sub op_ge { my ( $self, $a, $b ) = @_; my $r = $self->_less_than( $a, $b ); ( defined($r) && !$r ) ? true : false }
Subroutine op_ge: called CC=3 SCAR=11.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
defined $r && !$r ? : 50 74 124
Condition100%3/3
defined $r and not $r
!ll&&!rl&&r
56950
MC/DC100%2/2
defined $r and not $r
defined $r not $r
Truth table100%3/3
A and not(B)
A= defined $rB= not $r
ABresult
0X0
100
111
1359  
1360 # ECMAScript Abstract Equality (==).
1361 214 ▶sub _loose_eq {
Subroutine _loose_eq: called CC=23 SCAR=37.5
1362 214 my ( $self, $a, $b ) = @_;
1363 214 my $ta = $self->_js_type($a);
1364 214 my $tb = $self->_js_type($b);
1365  
1366 214 ▶ return Text::Nunjucks::Lib->_strict_eq( $a, $b ) ? 1 : 0 if $ta eq $tb;
Branch: 2 branches
BranchTrueFalseTotal
if $ta eq $tb 193 21 214
'Text::Nunjucks::Lib'->_strict_eq($a, $b) ? : 88 105 193
1367  
1368 21 ▶ return 1
Branch: 1 branch
BranchTrueFalseTotal
if $ta eq "null" and $tb eq "undefined" or $ta eq "undefined" and $tb eq "null" 1 20 21
Condition66%6/9
$ta eq "null" and $tb eq "undefined" or $ta eq "undefined" and $tb eq "null"
l!l&&r!l&&!r
1020
$ta eq "null" and $tb eq "undefined"
!ll&&!rl&&r
1911
$ta eq "undefined" and $tb eq "null"
!ll&&!rl&&r
2000
MC/DC50%2/4
$ta eq "null" and $tb eq "undefined" or $ta eq "undefined" and $tb eq "null"
$ta eq "null" $tb eq "undefined" $ta eq "undefined" $tb eq "null"
Truth table42%3/7
A and B or C and D
A= $ta eq "null"B= $tb eq "undefined"C= $ta eq "undefined"D= $tb eq "null"
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
1369 if ( $ta eq 'null' && $tb eq 'undefined' )
1370 || ( $ta eq 'undefined' && $tb eq 'null' );
1371 20 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
if $ta eq "null" or $ta eq "undefined" or $tb eq "null" or $tb eq "undefined" 1 19 20
Condition66%6/9
$ta eq "null" or $ta eq "undefined" or $tb eq "null" or $tb eq "undefined"
l!l&&r!l&&!r
1019
$ta eq "null" or $ta eq "undefined" or $tb eq "null"
l!l&&r!l&&!r
1019
$ta eq "null" or $ta eq "undefined"
l!l&&r!l&&!r
1019
MC/DC25%1/4
$ta eq "null" or $ta eq "undefined" or $tb eq "null" or $tb eq "undefined"
$ta eq "null" $ta eq "undefined" $tb eq "null" $tb eq "undefined"
Truth table40%2/5
A or B or C or D
A= $ta eq "null"B= $ta eq "undefined"C= $tb eq "null"D= $tb eq "undefined"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1372 if $ta eq 'null' || $ta eq 'undefined' || $tb eq 'null' || $tb eq 'undefined';
1373  
1374 19 ▶ return $self->_loose_eq( $self->_to_number($a), $b ) if $ta eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $ta eq "boolean" 2 17 19
1375 17 ▶ return $self->_loose_eq( $a, $self->_to_number($b) ) if $tb eq 'boolean';
Branch: 1 branch
BranchTrueFalseTotal
if $tb eq "boolean" 7 10 17
1376  
1377 10 ▶ return $self->_loose_eq( $a, $self->_to_number($b) ) if $ta eq 'number' && $tb eq 'string';
Branch: 1 branch
BranchTrueFalseTotal
if $ta eq "number" and $tb eq "string" 2 8 10
Condition66%2/3
$ta eq "number" and $tb eq "string"
!ll&&!rl&&r
802
MC/DC50%1/2
$ta eq "number" and $tb eq "string"
$ta eq "number" $tb eq "string"
Truth table66%2/3
A and B
A= $ta eq "number"B= $tb eq "string"
ABresult
0X0
100
111
1378 8 ▶ return $self->_loose_eq( $self->_to_number($a), $b ) if $ta eq 'string' && $tb eq 'number';
Branch: 1 branch
BranchTrueFalseTotal
if $ta eq "string" and $tb eq "number" 6 2 8
Condition66%2/3
$ta eq "string" and $tb eq "number"
!ll&&!rl&&r
206
MC/DC50%1/2
$ta eq "string" and $tb eq "number"
$ta eq "string" $tb eq "number"
Truth table66%2/3
A and B
A= $ta eq "string"B= $tb eq "number"
ABresult
0X0
100
111
1379  
1380 2 ▶ return $self->_loose_eq( $self->_to_primitive($a), $b )
Branch: 1 branch
BranchTrueFalseTotal
if $ta eq "object" and $tb eq "number" || $tb eq "string" 2 0 2
Condition33%2/6
$ta eq "object" and $tb eq "number" || $tb eq "string"
!ll&&!rl&&r
002
$tb eq "number" or $tb eq "string"
l!l&&r!l&&!r
020
MC/DC0%0/3
$ta eq "object" and $tb eq "number" || $tb eq "string"
$ta eq "object" $tb eq "number" $tb eq "string"
Truth table25%1/4
A and B or C
A= $ta eq "object"B= $tb eq "number"C= $tb eq "string"
ABCresult
0XX0
1000
11X1
1011
1381 if $ta eq 'object' && ( $tb eq 'number' || $tb eq 'string' );
1382 0 ▶ return $self->_loose_eq( $a, $self->_to_primitive($b) )
Branch: 1 branch
BranchTrueFalseTotal
if $tb eq "object" and $ta eq "number" || $ta eq "string" 0 0 0
Condition0%0/6
$tb eq "object" and $ta eq "number" || $ta eq "string"
!ll&&!rl&&r
000
$ta eq "number" or $ta eq "string"
l!l&&r!l&&!r
000
MC/DC0%0/3
$tb eq "object" and $ta eq "number" || $ta eq "string"
$tb eq "object" $ta eq "number" $ta eq "string"
Truth table0%0/4
A and B or C
A= $tb eq "object"B= $ta eq "number"C= $ta eq "string"
ABCresult
0XX0
1000
11X1
1011
1383 if $tb eq 'object' && ( $ta eq 'number' || $ta eq 'string' );
1384  
1385 0 return 0;
1386 }
1387  
1388 # ECMAScript Abstract Relational Comparison; returns 1, 0, or undef (a NaN
1389 # operand, which every comparison operator turns into false).
1390 71584 ▶sub _less_than {
Subroutine _less_than: called CC=7 SCAR=19.5
1391 71584 my ( $self, $x, $y ) = @_;
1392 71584 my $px = $self->_to_primitive($x);
1393 71584 my $py = $self->_to_primitive($y);
1394  
1395 71584 ▶ if ( $self->_js_type($px) eq 'string' && $self->_js_type($py) eq 'string' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_js_type($px) eq "string" and $self->_js_type($py) eq "string") 1044 70540 71584
Condition100%3/3
$self->_js_type($px) eq "string" and $self->_js_type($py) eq "string"
!ll&&!rl&&r
702333071044
MC/DC100%2/2
$self->_js_type($px) eq "string" and $self->_js_type($py) eq "string"
$self->_js_type($px) eq "string" $self->_js_type($py) eq "string"
Truth table100%3/3
A and B
A= $self->_js_type($px) eq "string"B= $self->_js_type($py) eq "string"
ABresult
0X0
100
111
1396 1044 ▶ return Text::Nunjucks::Lib->js_string_compare( $px, $py ) < 0 ? 1 : 0; # code units (ADR 0012)
Branch: 1 branch
BranchTrueFalseTotal
'Text::Nunjucks::Lib'->js_string_compare($px, $py) < 0 ? : 668 376 1044
1397 }
1398  
1399 70540 my $nx = $self->_to_number($px);
1400 70540 my $ny = $self->_to_number($py);
1401 70540 ▶ return undef if $nx != $nx || $ny != $ny; # NaN
Branch: 1 branch
BranchTrueFalseTotal
if $nx != $nx or $ny != $ny 476 70064 70540
Condition100%3/3
$nx != $nx or $ny != $ny
l!l&&r!l&&!r
22025670064
MC/DC100%2/2
$nx != $nx or $ny != $ny
$nx != $nx $ny != $ny
Truth table100%3/3
A or B
A= $nx != $nxB= $ny != $ny
ABresult
1X1
011
000
1402 70064 ▶ return ( $nx < $ny ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$nx < $ny ? : 31927 38137 70064
1403 }
1404  
1405 #### helpers for the emulated member surface
1406  
1407 # JS uses ToInteger on slice bounds; this clamps like Array slice (negatives
1408 # count from the end). Strings slice through the Lib seams (ADR 0012).
1409 136 ▶sub _slice_bounds {
Subroutine _slice_bounds: called CC=9 SCAR=22.0
1410 136 my ( $self, $len, $start, $end ) = @_;
1411 # ToIntegerOrInfinity: NaN is 0, so [1,2,3].slice(0, 'x') is [] (oracle, k55)
1412 136 ▶ $start = defined($start) ? Text::Nunjucks::Lib->_js_to_integer_or_infinity( $self->_to_number($start) ) : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $start ? : 135 1 136
1413 136 ▶ $end = defined($end) ? Text::Nunjucks::Lib->_js_to_integer_or_infinity( $self->_to_number($end) ) : $len;
Branch: 1 branch
BranchTrueFalseTotal
defined $end ? : 129 7 136
1414 136 ▶ $start = $start < 0 ? ( $len + $start > 0 ? $len + $start : 0 ) : ( $start > $len ? $len : $start );
Branch: 3 branches
BranchTrueFalseTotal
$start < 0 ? : 3 133 136
$len + $start > 0 ? : 2 1 3
$start > $len ? : 2 131 133
1415 136 ▶ $end = $end < 0 ? ( $len + $end > 0 ? $len + $end : 0 ) : ( $end > $len ? $len : $end );
Branch: 3 branches
BranchTrueFalseTotal
$end < 0 ? : 1 135 136
$len + $end > 0 ? : 0 1 1
$end > $len ? : 8 127 135
1416 136 return ( $start, $end );
1417 }
1418  
1419 # ToNumber of an optional numeric method argument, keeping JS `undefined` as
1420 # undef: the Lib string seams (ADR 0012) give undefined its per-parameter
1421 # meaning (slice's end -> length, lastIndexOf's position -> +Infinity, ...).
1422 9373 ▶sub _to_number_or_undef {
Subroutine _to_number_or_undef: called CC=2 SCAR=6.9
1423 9373 my ( $self, $v ) = @_;
1424 9373 ▶ return defined($v) ? $self->_to_number($v) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 8047 1326 9373
1425 }
1426  
1427 # The JS string key that `obj[val]` uses to look a member up.
1428 538822 ▶sub _member_key {
Subroutine _member_key: called CC=7 SCAR=19.5
1429 538822 my ( $self, $val ) = @_;
1430 538822 ▶ return 'undefined' if !defined($val);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 40 538782 538822
1431 538782 ▶ return 'null' if $self->_is_none($val);
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_none($val) 10 538772 538782
1432 538772 ▶ return ( $val ? 'true' : 'false' ) if is_bool($val);
Branch: 2 branches
BranchTrueFalseTotal
if builtin::is_bool($val) 11 538761 538772
$val ? : 10 1 11
1433 538761 ▶ return Text::Nunjucks::Lib->number_to_string($val) if created_as_number($val);
Branch: 1 branch
BranchTrueFalseTotal
if created_as_number $val 24066 514695 538761
1434 514695 ▶ return $val unless ref($val);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val 514602 93 514695
1435 93 return $self->_js_to_string($val);
1436 }
1437  
1438 31903 ▶sub _is_index_key {
Subroutine _is_index_key: called CC=2 SCAR=6.9
1439 31903 my ( $self, $key ) = @_;
1440 31903 ▶ return ( $key =~ /\A(?:0|[1-9][0-9]*)\z/ ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /\A(?:0|[1-9][0-9]*)\z/ ? : 23443 8460 31903
1441 }
1442  
1443 23 1;
1444  
1445 __END__
« blib/lib/Text/Nunjucks/Runtime/Regex.pm ↑ Summary blib/lib/Text/Nunjucks/Lib/TemplateError.pm »