blib/lib/Dallycot/Context.pm

filter: Statementstmt 94.1% 96 / 102 Branchbran 81.2% 26 / 32 Conditioncond 62.9% 17 / 27 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 95.6% 22 / 23 Podpod 0.0% 0 / 9 totaltotal 80.2% 167 / 208 CC 27 SCAR 16.4
CC
27
Cov
86%
has_namespace
12.0
make_closure
10.7
add_assignment
7.2
CRAP 5.2
« blib/lib/Dallycot/AST/Sum.pm ↑ Summary blib/lib/Dallycot/Value/Numeric.pm »
1 package Dallycot::Context;
2 10 our $AUTHORITY = 'cpan:JSMITH';
3  
4 # ABSTRACT: Execution context with value mappings and namespaces
5  
6 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 10 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 10 ▶use Array::Utils qw(unique array_minus);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 10 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 10 ▶use MooseX::Types::Moose qw/ArrayRef/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 10 ▶use Carp qw(croak cluck);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 10 ▶use experimental qw(switch);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 10 ▶use Promises qw(deferred);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 #
26 # Contexts form a chain from the kernel on down
27 # The context for a statement has no parent, but is copied from the kernel's
28 # context. Changes made are copied back to the kernel context info.
29 # Closures need to copy all of the info into a new context that is marked as
30 # a closure.
31  
32 3 ▶has namespaces => ( is => 'ro', isa => 'HashRef', default => sub { +{} }, lazy => 1 );
Subroutine __ANON__: called CC=1 SCAR=0.0
33  
34 630 ▶has environment => ( is => 'ro', isa => 'HashRef', default => sub { +{} }, lazy => 1 );
Subroutine __ANON__: called CC=1 SCAR=0.0
35  
36 81 ▶has namespace_search_path => ( is => 'ro', isa => 'ArrayRef', default => sub { [] }, lazy => 1 );
Subroutine __ANON__: called CC=1 SCAR=0.0
37  
38 10 has parent => ( is => 'ro', isa => 'Dallycot::Context', predicate => 'has_parent' );
39  
40 10 has is_closure => ( is => 'ro', isa => 'Bool', default => 0 );
41  
42 sub add_namespace {
43 4 ▶ my ( $self, $ns, $href ) = @_;
Subroutine add_namespace: called CC=4 SCAR=13.9
Pod undocumented
44  
45 4 ▶ if ( ( $self->is_closure || $self->has_parent )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_closure || $self->has_parent and defined $self->namespaces->{$ns}) 1 3 4
Condition83%5/6
$self->is_closure || $self->has_parent and defined $self->namespaces->{$ns}
!ll&&!rl&&r
211
$self->is_closure or $self->has_parent
l!l&&r!l&&!r
022
MC/DC66%2/3
$self->is_closure || $self->has_parent and defined $self->namespaces->{$ns}
$self->is_closure $self->has_parent defined $self->namespaces->{$ns}
Truth table60%3/5
A or B and C
A= $self->is_closureB= $self->has_parentC= defined $self->namespaces->{$ns}
ABCresult
00X0
1X00
0100
1X11
0111
46 && defined( $self->namespaces->{$ns} ) )
47 {
48 1 croak "Namespaces may not be defined multiple times in a sub-context or closure";
49 }
50 3 $self->namespaces->{$ns} = $href;
51  
52 3 return;
53 }
54  
55 sub get_namespace {
56 6 ▶ my ( $self, $ns ) = @_;
Subroutine get_namespace: called CC=3 SCAR=11.0
Pod undocumented
57  
58 6 ▶ if ( exists( $self->namespaces->{$ns} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $self->namespaces->{$ns}) { } 5 1 6
elsif ($self->has_parent) { } 1 0 1
59 5 return $self->namespaces->{$ns};
60 }
61 elsif ( $self->has_parent ) {
62 1 return $self->parent->get_namespace($ns);
63 }
64 }
65  
66 sub has_namespace {
67 0 ▶ my ( $self, $prefix ) = @_;
Subroutine has_namespace: not called CC=3 SCAR=24.8
Pod undocumented
68  
69 0 ▶ return exists( $self->namespaces->{$prefix} )
Condition0%0/6
exists $self->namespaces->{$prefix} || $self->has_parent && $self->parent->has_namespace($prefix)
l!l&&r!l&&!r
000
$self->has_parent and $self->parent->has_namespace($prefix)
!ll&&!rl&&r
000
MC/DC0%0/3
exists $self->namespaces->{$prefix} || $self->has_parent && $self->parent->has_namespace($prefix)
exists $self->namespaces->{$prefix} $self->has_parent $self->parent->has_namespace($prefix)
Truth table0%0/4
A || B and C
A= exists $self->namespaces->{$prefix}B= $self->has_parentC= $self->parent->has_namespace($prefix)
ABCresult
1XX1
0111
00X0
0100
70 || $self->has_parent && $self->parent->has_namespace($prefix);
71 }
72  
73 sub add_assignment {
74 285 ▶ my ( $self, $identifier, $expr ) = @_;
Subroutine add_assignment: called CC=7 SCAR=19.7
Pod undocumented
75  
76 285 ▶ if ( ( $self->is_closure || $self->has_parent ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_closure or $self->has_parent) 232 53 285
Condition66%2/3
$self->is_closure or $self->has_parent
l!l&&r!l&&!r
023253
MC/DC50%1/2
$self->is_closure or $self->has_parent
$self->is_closure $self->has_parent
Truth table66%2/3
A or B
A= $self->is_closureB= $self->has_parent
ABresult
1X1
011
000
77 232 my $d = $self->environment->{$identifier};
78 232 ▶ if ( $d && $d->is_resolved ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($d and $d->is_resolved) 0 231 231
Condition66%2/3
$d and $d->is_resolved
!ll&&!rl&&r
153790
MC/DC0%0/2
$d and $d->is_resolved
$d $d->is_resolved
Truth table66%2/3
A and B
A= $dB= $d->is_resolved
ABresult
0X0
100
111
79 0 croak "Identifiers may not be redefined in a sub-context or closure";
80 }
81 }
82 284 ▶ if ( defined $expr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expr) { } 179 105 284
83 179 ▶ if ( $expr->can('resolve') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr->can('resolve')) { } 13 166 179
84 13 return $self->environment->{$identifier} = $expr;
85 }
86 else {
87 166 my $d = deferred;
88 166 $d->resolve($expr);
89 166 return $self->environment->{$identifier} = $d;
90 }
91 }
92 else {
93 105 return $self->environment->{$identifier} = deferred;
94 }
95 }
96  
97 sub get_assignment {
98 6565 ▶ my ( $self, $identifier ) = @_;
Subroutine get_assignment: called CC=3 SCAR=11.0
Pod undocumented
99  
100 6565 ▶ if ( defined( $self->environment->{$identifier} ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->environment->{$identifier}) { } 4337 2228 6565
elsif ($self->has_parent) { } 1372 856 2228
101 4337 return $self->environment->{$identifier};
102 }
103 elsif ( $self->has_parent ) {
104 1372 return $self->parent->get_assignment($identifier);
105 }
106 }
107  
108 sub has_assignment {
109 4207 ▶ my ( $self, $identifier ) = @_;
Subroutine has_assignment: called CC=3 SCAR=11.0
Pod undocumented
110  
111 4207 ▶ return exists( $self->environment->{$identifier} )
Condition100%6/6
exists $self->environment->{$identifier} || $self->has_parent && $self->parent->has_assignment($identifier)
l!l&&r!l&&!r
3349318540
$self->has_parent and $self->parent->has_assignment($identifier)
!ll&&!rl&&r
318222318
MC/DC66%2/3
exists $self->environment->{$identifier} || $self->has_parent && $self->parent->has_assignment($identifier)
exists $self->environment->{$identifier} $self->has_parent $self->parent->has_assignment($identifier)
Truth table75%3/4
A || B and C
A= exists $self->environment->{$identifier}B= $self->has_parentC= $self->parent->has_assignment($identifier)
ABCresult
1XX1
0111
00X0
0100
112 || $self->has_parent && $self->parent->has_assignment($identifier);
113 }
114  
115 sub get_namespace_search_path {
116 604 ▶ my ($self) = @_;
Subroutine get_namespace_search_path: called CC=1 SCAR=0.0
Pod undocumented
117  
118 604 return $self->namespace_search_path;
119 }
120  
121 sub append_namespace_search_path {
122 199 ▶ my ( $self, @paths ) = @_;
Subroutine append_namespace_search_path: called CC=1 SCAR=0.0
Pod undocumented
123  
124 199 return push @{ $self->namespace_search_path }, @paths;
125 }
126  
127 sub make_closure {
128 755 ▶ my ( $self, $node ) = @_;
Subroutine make_closure: called CC=10 SCAR=23.7
Pod undocumented
129  
130 755 my ( %namespaces, %environment );
131  
132 # we only copy the values we can use
133 755 my @stack = ($node);
134 755 my @identifiers = ();
135  
136 755 ▶ while (@stack) {
Branch: 1 branch
BranchTrueFalseTotal
if (@stack) 6147 755 6902
137 6147 $node = shift @stack;
138 6147 ▶ if ( !ref $node ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $node) 0 6147 6147
139 0 cluck "We have a non-ref node! ($node)";
140 }
141  
142 6147 push @stack, $node->child_nodes;
143  
144 6147 my @ids = $node->identifiers;
145 6147 ▶ if (@ids) {
Branch: 1 branch
BranchTrueFalseTotal
if (@ids) 3298 2849 6147
146 3298 my @new_ids = array_minus( @ids, @identifiers );
147  
148 #push @stack, grep { ref } map { $self->get_assignment($_) } @new_ids;
149 3298 push @identifiers, @new_ids;
150 }
151 }
152  
153 755 @identifiers = values %{ +{ map { $_ => $_ } @identifiers } };
154  
155 755 for my $identifier (@identifiers) {
156 2049 ▶ if ( is_ArrayRef($identifier) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (is_ArrayRef($identifier)) { } 0 2049 2049
elsif (substr($identifier, 0, 1) ne '#' and not defined $environment{$identifier}) { } 1835 428 2263
157 0 ▶ if ( !defined( $namespaces{ $identifier->[0] } ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $namespaces{$identifier->[0]}) 0 0 0
158 0 $namespaces{ $identifier->[0] } = $self->get_namespace( $identifier->[0] );
159 }
160 }
161 ▶ elsif ( substr( $identifier, 0, 1 ) ne '#' && !defined( $environment{$identifier} ) ) {
Condition66%2/3
substr($identifier, 0, 1) ne "#" and not defined $environment{$identifier}
!ll&&!rl&&r
21401835
MC/DC50%1/2
substr($identifier, 0, 1) ne "#" and not defined $environment{$identifier}
substr($identifier, 0, 1) ne "#" not defined $environment{$identifier}
Truth table66%2/3
A and not(B)
A= substr($identifier, 0, 1) ne "#"B= not defined $environment{$identifier}
ABresult
0X0
100
111
162 1835 my $value = $self->get_assignment($identifier);
163 1835 ▶ $environment{$identifier} = $value if blessed $value;
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) 979 856 1835
164 }
165 }
166  
167 # making the closure a child/parent allows setting overrides once in the closure code
168 return $self->new(
169 namespaces => \%namespaces,
170 environment => \%environment,
171 755 namespace_search_path => [@{$self->namespace_search_path}]
172 );
173 }
174  
175 10 __PACKAGE__->meta->make_immutable;
176  
177 10 1;
« blib/lib/Dallycot/AST/Sum.pm ↑ Summary blib/lib/Dallycot/Value/Numeric.pm »