blib/lib/Text/Nunjucks/Globals.pm

filter: Statementstmt 60.0% 63 / 105 Branchbran 47.2% 17 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 64.0% 16 / 25 Podpod 0.0% 0 / 6 totaltotal 55.8% 96 / 172 CC 19 SCAR 14.9
CC
19
Cov
57%
__ANON__
10.5
__ANON__
10.5
__ANON__
4.1
CRAP 4.4
« blib/lib/Text/Nunjucks/EmitterObj.pm ↑ Summary blib/lib/Text/Nunjucks/Context.pm »
1 package Text::Nunjucks::Globals;
2 # ABSTRACT: 1:1 port of Nunjucks src/globals.js -- the built-in global functions
3  
4 15 our $VERSION = '0.001';
5  
6 15 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use builtin qw( true false );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use Text::Nunjucks::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 15 ▶no warnings 'experimental::builtin';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # globals.js defines two helpers, cycler and joiner, and exports globals(), a
15 # function that builds a fresh { range, cycler, joiner } bag on every call, so
16 # each Environment owns its copy (environment.js: this.globals = globals()).
17 # The port keeps the three functions, in upstream's order, as class methods
18 # dispatched through $self, like Filters and Tests; `globals` returns a fresh
19 # plain hashref, the bag Environment.init stores and add_global writes into.
20 # range's body stays inline in that bag, where upstream defines it.
21 #
22 # A global is called the way call_wrap calls any function (ADR 0009): with the
23 # template's arguments, keyword arguments as a trailing KwArgs hashref, and no
24 # `this`. JS operators -- range's <, >, += and !step, joiner's || -- go through
25 # Runtime (ADR 0001 kinds, ADR 0006 null, ADR 0007 numbers): range('3') counts
26 # 0, 1, 2 and range('3', 5) is ['3'] ('3' + 1 is '31').
27 #
28 # The cycler is a plain JS object with own properties current, reset and next.
29 # The port builds it with Runtime make_dict, the engine-built object of ADR 0011,
30 # so it iterates in upstream's order (current;reset;next;), dumps as
31 # {"current":null}, prints [object Object] and is a mapping. Its methods read
32 # and write this.current. Here they close over the object instead -- the interim
33 # choice while decision k93 is open (ADR 0009's no-`this` covers user coderefs
34 # only). Upstream's memberLookup binds `this` to the cycler on every member read
35 # (cls.next() and a cls.next read and called later), which is where the two
36 # agree. They differ for a method reached without memberLookup -- e.g.
37 # `{% for k, v in cls %}` then `v()`, which upstream calls with the Context as
38 # `this` (oracle: the cycler's current stays null there; here it moves); k93
39 # lists the paths. The closures hold a weakened copy of the object, which holds them, so
40 # the pair is no reference cycle; once the object is gone nothing can read its
41 # current, and next() still steps through the items.
42  
43 5173 ▶sub _runtime { 'Text::Nunjucks::Runtime' }
Subroutine _runtime: called CC=1 SCAR=0.0
44  
45 #### cycler
46  
47 35 ▶sub cycler {
Subroutine cycler: called CC=1 SCAR=1.3
Pod undocumented
48 0 ▶ my ( $self, $items ) = @_;
Subroutine cycler: not called CC=1 SCAR=1.3
Pod undocumented
49 0 my $rt = $self->_runtime;
50 0 my $index = -1;
51 0 my $this; # the object returned below, weakened (see the header)
52  
53 my $obj = $rt->make_dict(
54 current => $rt->None,
55 4 ▶ reset => sub {
Subroutine __ANON__: called CC=2 SCAR=10.5
56 0 ▶ $index = -1;
Subroutine __ANON__: not called CC=2 SCAR=10.5
57 0 ▶ $this->set( current => $rt->None ) if $this;
Branch: 2 branches
BranchTrueFalseTotal
if $this 4 0 4
if $this 0 0 0
58 0 return undef;
59 },
60  
61 54 ▶ next => sub {
Subroutine __ANON__: called CC=3 SCAR=14.2
62 0 ▶ $index++;
Subroutine __ANON__: not called CC=3 SCAR=14.2
63 0 ▶ if ( $index >= scalar @$items ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($index >= scalar @$items) 12 42 54
if ($index >= scalar @$items) 0 0 0
64 0 $index = 0;
65 }
66  
67 # this.current = items[index]; return this.current;
68 0 my $current = $items->[$index];
69 0 ▶ $this->set( current => $current ) if $this;
Branch: 2 branches
BranchTrueFalseTotal
if $this 50 4 54
if $this 0 0 0
70 0 return $current;
71 },
72 0 );
73 0 weaken( $this = $obj );
74 0 return $obj;
75 }
76  
77 #### joiner
78  
79 18 ▶sub joiner {
Subroutine joiner: called CC=1 SCAR=1.2
Pod undocumented
80 0 ▶ my ( $self, $sep ) = @_;
Subroutine joiner: not called CC=1 SCAR=1.2
Pod undocumented
81 0 $sep = $self->_runtime->op_or( $sep, ',' );
82 0 my $first = true;
83  
84 35 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=9.2
85 0 ▶ my $val = $first ? '' : $sep;
Subroutine __ANON__: not called CC=2 SCAR=9.2
Branch: 2 branches
BranchTrueFalseTotal
$first ? : 16 19 35
$first ? : 0 0 0
86 0 $first = false;
87 0 return $val;
88 0 };
89 }
90  
91 #### globals
92  
93 # Making this a function instead so it returns a new object
94 # each time it's called. That way, if something like an environment
95 # uses it, they will each have their own copy.
96 5120 ▶sub globals {
Subroutine globals: called CC=1 SCAR=1.2
Pod undocumented
97 0 ▶ my ($self) = @_;
Subroutine globals: not called CC=1 SCAR=1.2
Pod undocumented
98 0 my $rt = $self->_runtime;
99 return {
100 90 ▶ range => sub {
Subroutine __ANON__: called CC=6 SCAR=23.5
101 0 ▶ my ( $start, $stop, $step ) = @_;
Subroutine __ANON__: not called CC=6 SCAR=23.5
102 0 ▶ if ( !defined($stop) ) { # typeof stop === 'undefined'
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $stop) { } 41 49 90
elsif ($rt->_falsy($step)) { } 34 15 49
if (not defined $stop) { } 0 0 0
elsif ($rt->_falsy($step)) { } 0 0 0
103 0 $stop = $start;
104 0 $start = 0;
105 0 $step = 1;
106 }
107 elsif ( $rt->_falsy($step) ) {
108 0 $step = 1;
109 }
110  
111 0 my $arr = [];
112 0 ▶ if ( $rt->op_gt( $step, 0 ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt->op_gt($step, 0)) { } 84 6 90
if ($rt->op_gt($step, 0)) { } 0 0 0
113 0 ▶ for ( my $i = $start; $rt->op_lt( $i, $stop ); $i = $rt->op_add( $i, $step ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt->op_lt($i, $stop)) 2843 84 2927
if ($rt->op_lt($i, $stop)) 0 0 0
114 0 push @$arr, $i;
115 }
116 }
117 else {
118 0 ▶ for ( my $i = $start; $rt->op_gt( $i, $stop ); $i = $rt->op_add( $i, $step ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($rt->op_gt($i, $stop)) 16 6 22
if ($rt->op_gt($i, $stop)) 0 0 0
119 0 push @$arr, $i;
120 }
121 }
122 0 return $arr;
123 },
124  
125 35 ▶ cycler => sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
126 0 ▶ return $self->cycler( [@_] ); # Array.prototype.slice.call(arguments)
Subroutine __ANON__: not called CC=1 SCAR=1.2
127 },
128  
129 18 ▶ joiner => sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
130 0 ▶ my ($sep) = @_;
Subroutine __ANON__: not called CC=1 SCAR=1.2
131 0 return $self->joiner($sep);
132 },
133 0 };
134 }
135  
136 15 1;
137  
138 __END__
« blib/lib/Text/Nunjucks/EmitterObj.pm ↑ Summary blib/lib/Text/Nunjucks/Context.pm »