| 1 | package Text::Nunjucks::Runtime::Frame; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: A scope of template variables (runtime.js class Frame) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 23 | our $VERSION = '0.001'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use Moo; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use Text::Nunjucks::Runtime; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | # upstream: class Frame in src/runtime.js. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | # constructor(parent, isolateWrites): variables is a bare bag (JS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | # Object.create(null)); topLevel starts false and the compiler/environment | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | # flip it to true on the root frame, so it is rw. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 136388 | ▶ | has variables => ( is => 'ro', default => sub { {} } ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 23 | has parent => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 136388 | ▶ | has top_level => ( is => 'rw', default => sub { 0 } ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 23 | has isolate_writes => ( is => 'ro' ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 136388 | ▶ | around BUILDARGS => sub { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 136388 | my ( $orig, $class, @args ) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | 136388 | ▶ | return $class->$orig(@args) if @args == 1 && ref $args[0] eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
@args == 1 and ref $args[0] eq "HASH"
MC/DC0%0/2
@args == 1 and ref $args[0] eq "HASH"
@args == 1
ref $args[0] eq "HASH"
Truth table33%1/3
A and B
A= @args == 1B= ref $args[0] eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | 136388 | my ( $parent, $isolate_writes ) = @args; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 136388 | return $class->$orig( parent => $parent, isolate_writes => $isolate_writes ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | 23 | }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 464673 | ▶ | sub set { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: called CC=5 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | my ( $self, $name, $val, $resolve_up ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
set: not called CC=5 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | # Allow variables with dots by automatically creating the nested structure. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 0 | my @parts = split /\./, $name, -1; # JS split('.') keeps trailing empties | |||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | 0 | my $obj = $self->variables; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | 0 | my $frame = $self; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 0 | ▶ | if ($resolve_up) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 0 | ▶ | if ( $frame = $self->resolve( $parts[0], 1 ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | 0 | $frame->set( $name, $val ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 0 | return; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 0 | for my $i ( 0 .. $#parts - 1 ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 0 | my $id = $parts[$i]; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | # JS: if (!obj[id]) obj[id] = {}; -- JS falsiness (null/None too), not Perl's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if ( Text::Nunjucks::Runtime->_falsy( $obj->{$id} ) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 0 | $obj->{$id} = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | $obj = $obj->{$id}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | $obj->{ $parts[-1] } = $val; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 4 | ▶ | sub get { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 0 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
get: not called CC=2 SCAR=9.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 0 | my $val = $self->variables->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | return $val if defined $val; # val !== undefined | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | return Text::Nunjucks::Runtime->None; # JS: return null | |||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 196431 | ▶ | sub lookup { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup: called CC=3 SCAR=13.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 0 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
lookup: not called CC=3 SCAR=13.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 0 | my $p = $self->parent; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 0 | my $val = $self->variables->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | ▶ | return $val if defined $val; # val !== undefined | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # Port-only (k83): one call per frame up the chain, as upstream's lookup | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | # recurses; a template that includes itself N deep stands on N frames or | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | # more. Perl warns at the 100th nested call of a sub, at the call site; the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | # depth is bounded by memory, not by Perl (see Lib async_iter). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | 23 | ▶ | no warnings 'recursion'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return $p && $p->lookup($name); # p && p.lookup(name) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$p && $p->lookup($name)
$p && $p->lookup($name)
MC/DC100%2/2
$p && $p->lookup($name)
$p
$p->lookup($name)
MC/DC0%0/2
$p && $p->lookup($name)
$p
$p->lookup($name)
Truth table100%3/3
A && B
A= $pB= $p->lookup($name)
Truth table0%0/3
A && B
A= $pB= $p->lookup($name)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 74358 | ▶ | sub resolve { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=5 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | my ( $self, $name, $for_write ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
resolve: not called CC=5 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | 0 | ▶ | my $p = ( $for_write && $self->isolate_writes ) ? undef : $self->parent; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition50%3/6
$for_write and $self->isolate_writes
$for_write and $self->isolate_writes
MC/DC100%2/2
$for_write and $self->isolate_writes
$for_write
$self->isolate_writes
MC/DC0%0/2
$for_write and $self->isolate_writes
$for_write
$self->isolate_writes
Truth table100%3/3
A and B
A= $for_writeB= $self->isolate_writes
Truth table0%0/3
A and B
A= $for_writeB= $self->isolate_writes
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 0 | my $val = $self->variables->{$name}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return $self if defined $val; # val !== undefined | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | # one call per frame up the chain, as in lookup (k83) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | 23 | ▶ | no warnings 'recursion'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | return $p && $p->resolve($name); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%3/6
$p && $p->resolve($name)
$p && $p->resolve($name)
MC/DC100%2/2
$p && $p->resolve($name)
$p
$p->resolve($name)
MC/DC0%0/2
$p && $p->resolve($name)
$p
$p->resolve($name)
Truth table100%3/3
A && B
A= $pB= $p->resolve($name)
Truth table0%0/3
A && B
A= $pB= $p->resolve($name)
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | 10941 | ▶ | sub push { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
push: called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 0 | ▶ | my ( $self, $isolate_writes ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
push: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | return ref($self)->new( $self, $isolate_writes ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 2222 | ▶ | sub pop { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pop: called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pop: not called CC=1 SCAR=1.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 0 | return $self->parent; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 23 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||