| 1 | package CatalystX::ASP::Compiler; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | 13 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use File::Slurp qw(read_file); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 6 | 13 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||
| 8 | 10 | with 'CatalystX::ASP::Parser'; | |||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||
| 10 | 10 | requires 'parse_file'; | |||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||
| 14 | CatalystX::ASP::Compiler - Role for CatalystX::ASP providing code compilation | ||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||
| 16 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||
| 18 | use CatalystX::ASP; | ||||||||||||||||||||||||||||||||
| 19 | with 'CatalystX::ASP::Compiler'; | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | sub execute { | ||||||||||||||||||||||||||||||||
| 22 | my ($self, $c, $scriptref) = @_; | ||||||||||||||||||||||||||||||||
| 23 | my $parsed = $self->parse($c, $scriptref); | ||||||||||||||||||||||||||||||||
| 24 | my $subid = $self->compile($c, $parsed->{data}); | ||||||||||||||||||||||||||||||||
| 25 | eval { &$subid }; | ||||||||||||||||||||||||||||||||
| 26 | } | ||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||
| 30 | This class implements the ability to compile parsed ASP code. | ||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | =cut | ||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||
| 34 | has '_compiled_includes' => ( | ||||||||||||||||||||||||||||||||
| 35 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 36 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||
| 37 | 16 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 38 | 10 | traits => [qw(Hash)], | |||||||||||||||||||||||||||||||
| 39 | handles => { | ||||||||||||||||||||||||||||||||
| 40 | _get_compiled_include => 'get', | ||||||||||||||||||||||||||||||||
| 41 | _add_compiled_include => 'set', | ||||||||||||||||||||||||||||||||
| 42 | _include_is_compiled => 'exists', | ||||||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||||||
| 44 | ); | ||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||
| 46 | has '_registered_includes' => ( | ||||||||||||||||||||||||||||||||
| 47 | is => 'rw', | ||||||||||||||||||||||||||||||||
| 48 | isa => 'HashRef', | ||||||||||||||||||||||||||||||||
| 49 | 16 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 50 | 9 | traits => [qw(Hash)], | |||||||||||||||||||||||||||||||
| 51 | handles => { | ||||||||||||||||||||||||||||||||
| 52 | _include_is_registered => 'exists', | ||||||||||||||||||||||||||||||||
| 53 | _add_registered_include => 'set', | ||||||||||||||||||||||||||||||||
| 54 | }, | ||||||||||||||||||||||||||||||||
| 55 | ); | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | =head1 METHODS | ||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||
| 59 | =over | ||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||
| 61 | =item $self->compile($c, $scriptref, $subid) | ||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||
| 63 | Takes a C<$scriptref> that has been parsed and C<$subid> for the name of the | ||||||||||||||||||||||||||||||||
| 64 | subroutine to compile the code into. Returns | ||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | 16 | ▶ | sub compile { | ||||||||||||||||||||||||||||||
|
Subroutine
compile: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||
| 69 | 18 | my ( $self, $c, $scriptref, $subid ) = @_; | |||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||
| 71 | 16 | my $package = $self->GlobalASA->package; | |||||||||||||||||||||||||||||||
| 72 | 16 | $self->_undefine_sub( $subid ); | |||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||
| 74 | 16 | my $code = join( ' ;; ', | |||||||||||||||||||||||||||||||
| 75 | "package $package;", # for no sub closure | ||||||||||||||||||||||||||||||||
| 76 | "no strict;", | ||||||||||||||||||||||||||||||||
| 77 | "sub $subid { ", | ||||||||||||||||||||||||||||||||
| 78 | "package $package;", # for sub closure | ||||||||||||||||||||||||||||||||
| 79 | $$scriptref, | ||||||||||||||||||||||||||||||||
| 80 | '}', | ||||||||||||||||||||||||||||||||
| 81 | ); | ||||||||||||||||||||||||||||||||
| 82 | 16 | $code =~ /^(.*)$/s; # Realized this is for untainting | |||||||||||||||||||||||||||||||
| 83 | 16 | $code = $1; | |||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||
| 85 | 12 | ▶ | no warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 86 | 16 | ▶ | local $SIG{__DIE__} = \&Carp::confess if $self->Debug; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 87 | 16 | ▶ | eval $code; ## no critic (BuiltinFunctions::ProhibitStringyEval) | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ASP_pp_root_templates_some_template_incx77cc855e2a5fef3c939781e74f222781xINC: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 88 | 16 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 89 | 1 | $c->error( "Error on compilation of $subid: $@" ); # don't throw error, so we can throw die later | |||||||||||||||||||||||||||||||
| 90 | 1 | $self->_undefine_sub( $subid ); | |||||||||||||||||||||||||||||||
| 91 | 1 | return; | |||||||||||||||||||||||||||||||
| 92 | } else { | ||||||||||||||||||||||||||||||||
| 93 | 16 | $self->register_include( $c, $scriptref ); | |||||||||||||||||||||||||||||||
| 94 | 16 | return $subid; | |||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||
| 98 | =item $self->compile_include($c, $include) | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | Takes an C<$include> file. This will search for the file in C<IncludesDir> and | ||||||||||||||||||||||||||||||||
| 101 | parse it, and assign it a C<$subid> based on it's filename. | ||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||
| 103 | =cut | ||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||
| 105 | 14 | ▶ | sub compile_include { | ||||||||||||||||||||||||||||||
|
Subroutine
compile_include: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 106 | 14 | my ( $self, $c, $include ) = @_; | |||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||
| 108 | 12 | my $file = $self->search_includes_dir( $include ); | |||||||||||||||||||||||||||||||
| 109 | 12 | ▶ | unless ( $file ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 110 | 2 | $c->error( "Error in compilation: $include not found" ); | |||||||||||||||||||||||||||||||
| 111 | 2 | return; | |||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||
| 114 | 13 | return $self->compile_file( $c, $file ); | |||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||
| 117 | =item $self->compile_file($c, $file) | ||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||
| 119 | Takes an C<$file> assuming it exists. This will search for the file in | ||||||||||||||||||||||||||||||||
| 120 | C<IncludesDir> and parse it, and assign it a C<$subid> based on it's filename. | ||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||
| 124 | 23 | ▶ | sub compile_file { | ||||||||||||||||||||||||||||||
|
Subroutine
compile_file: called CC=8 SCAR=21.4
| |||||||||||||||||||||||||||||||||
| 125 | 21 | my ( $self, $c, $file ) = @_; | |||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||
| 127 | 23 | my $id = $self->file_id( $file ); | |||||||||||||||||||||||||||||||
| 128 | 23 | my $subid = join( '', $self->GlobalASA->package, '::', $id, 'xINC' ); | |||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||
| 130 | 23 | ▶ | return $self->_get_compiled_include( $subid ) if $self->_include_is_compiled( $subid ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||
| 132 | 16 | my $parsed_object = $self->parse_file( $c, $file ); | |||||||||||||||||||||||||||||||
| 133 | 16 | ▶ | return unless $parsed_object; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||
| 135 | my %compiled_object = ( | ||||||||||||||||||||||||||||||||
| 136 | mtime => time(), | ||||||||||||||||||||||||||||||||
| 137 | perl => $parsed_object->{data}, | ||||||||||||||||||||||||||||||||
| 138 | 16 | file => $file, | |||||||||||||||||||||||||||||||
| 139 | ); | ||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||
| 141 | 16 | ▶ | if ( $parsed_object->{is_perl} | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$parsed_object->{"is_perl"} and my $code = $self->compile($c, $parsed_object->{"data"}, $subid)
MC/DC50%1/2
$parsed_object->{"is_perl"} and my $code = $self->compile($c, $parsed_object->{"data"}, $subid)
$parsed_object->{"is_perl"}
my $code = $self->compile($c, $parsed_object->{"data"}, $subid)
Truth table66%2/3
A and B
A= $parsed_object->{"is_perl"}B= my $code = $self->compile($c, $parsed_object->{"data"}, $subid)
| |||||||||||||||||||||||||||||||||
| 142 | && ( my $code = $self->compile( $c, $parsed_object->{data}, $subid ) ) ) { | ||||||||||||||||||||||||||||||||
| 143 | 14 | $compiled_object{is_perl} = 1; | |||||||||||||||||||||||||||||||
| 144 | 13 | $compiled_object{code} = $code; | |||||||||||||||||||||||||||||||
| 145 | } elsif ( $parsed_object->{is_raw} ) { | ||||||||||||||||||||||||||||||||
| 146 | 5 | $compiled_object{is_raw} = 1; | |||||||||||||||||||||||||||||||
| 147 | 5 | $compiled_object{code} = $parsed_object->{data}; | |||||||||||||||||||||||||||||||
| 148 | } else { | ||||||||||||||||||||||||||||||||
| 149 | 4 | return; | |||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||
| 152 | # for a returned code ref, don't cache | ||||||||||||||||||||||||||||||||
| 153 | $self->_add_compiled_include( $subid => \%compiled_object ) | ||||||||||||||||||||||||||||||||
| 154 | 15 | ▶ | if ( $subid && !$self->_parse_for_subs( $parsed_object->{data} ) ); | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$subid and not $self->_parse_for_subs($parsed_object->{"data"})
MC/DC0%0/2
$subid and not $self->_parse_for_subs($parsed_object->{"data"})
$subid
not $self->_parse_for_subs($parsed_object->{"data"})
Truth table33%1/3
A and not(B)
A= $subidB= not $self->_parse_for_subs($parsed_object->{"data"})
| |||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||
| 156 | 15 | return \%compiled_object; | |||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||
| 159 | =item $self->register_include($c, $scriptref) | ||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||
| 161 | Registers the file file of any calls to C<< $Response->Include() >> so as to | ||||||||||||||||||||||||||||||||
| 162 | prevent infinite recursion | ||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||
| 164 | =cut | ||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||
| 166 | 18 | ▶ | sub register_include { | ||||||||||||||||||||||||||||||
|
Subroutine
register_include: called CC=3 SCAR=11.5
| |||||||||||||||||||||||||||||||||
| 167 | 17 | my ( $self, $c, $scriptref ) = @_; | |||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||
| 169 | 17 | my $copy = $$scriptref; | |||||||||||||||||||||||||||||||
| 170 | 16 | $copy =~ s/\$Response\-\>Include\([\'\"]([^\$]+?)[\'\"]/ | |||||||||||||||||||||||||||||||
| 171 | { | ||||||||||||||||||||||||||||||||
| 172 | 1 | my $include = $1; | |||||||||||||||||||||||||||||||
| 173 | # prevent recursion | ||||||||||||||||||||||||||||||||
| 174 | 1 | ▶ | unless( $self->_include_is_registered( $include ) ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 175 | 1 | $self->_add_registered_include( $include => 1 ); | |||||||||||||||||||||||||||||||
| 176 | 1 | eval { $self->compile_include( $c, $include ); }; | |||||||||||||||||||||||||||||||
| 177 | 1 | ▶ | $c->log->warn( "Register include $include with error: $@" ) if $@; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||
| 179 | 1 | ''; | |||||||||||||||||||||||||||||||
| 180 | } /exsgi; | ||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||
| 183 | # This is how CHAMAS gets a subroutined destroyed | ||||||||||||||||||||||||||||||||
| 184 | 21 | ▶ | sub _undefine_sub { | ||||||||||||||||||||||||||||||
|
Subroutine
_undefine_sub: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||
| 185 | 19 | my ( $self, $subid ) = @_; | |||||||||||||||||||||||||||||||
| 186 | 19 | ▶ | if ( my $code = \&{$subid} ) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 187 | 19 | undef( &$code ); | |||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||
| 191 | 12 | ▶ | no Moose::Role; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||
| 193 | 9 | 1; | |||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||
| 195 | =back | ||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||
| 197 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||
| 199 | =over | ||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||
| 201 | =item * L<CatalystX::ASP> | ||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||
| 203 | =item * L<CatalystX::ASP::Parser> | ||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||
| 205 | =back | ||||||||||||||||||||||||||||||||