Compile/Compiler.pm

filter: Statementstmt 16.6% 58 / 349 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 24.6% 19 / 77 Podpod 0.0% 0 / 46 totaltotal 12.8% 77 / 598 CC 57 SCAR 31.1
CC
57
Cov
13%
extract_code_symbols
132.0
subref
56.9
compile_method
42.0
CRAP 22.3
↑ Summary Compile/Base.pm »
1 #!/usr/bin/perl
2  
3 package MooseX::Compile::Compiler;
4 2 ▶use base qw(MooseX::Compile::Base);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use Data::Dump qw(dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Data::Visitor::Callback;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use Storable;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use PadWalker;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use Class::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 2 our %compiled_classes;
18  
19 2 ▶use constant DEBUG => MooseX::Compile::Base::DEBUG();
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 # FIXME make this Moose based eventually
22 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
23 1 my ( $class, %args ) = @_;
24 1 bless \%args, $class;
25 }
26  
27 0 ▶sub compile_class {
Subroutine compile_class: not called CC=4 SCAR=30.0
Pod undocumented
28 0 my ( $self, %args ) = @_;
29 0 my $class = $args{class};
30  
31 0 ( my $short_name = "$class.pm" ) =~ s{::}{/}g;
32 0 $args{short_name} = $short_name;
33  
34 0 ▶ unless ( defined $args{file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"file"}) 0 0 0
35 0 $args{file} = $INC{$short_name};
36 }
37  
38 0 ▶ unless ( defined $args{pmc_file} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"pmc_file"}) 0 0 0
39 0 $args{pmc_file} = "$args{file}c";
40 }
41  
42 0 ▶ if ( $compiled_classes{$class}++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($MooseX::Compile::Compiler::compiled_classes{$class}++) 0 0 0
43 0 warn "already compiled class '$class'\n" if DEBUG;
44 0 return;
45 }
46  
47 0 my $t = times;
48  
49 0 $self->cache_meta(%args);
50 0 $self->write_pmc_file(%args);
51  
52 0 warn "compilation of .pmc and .mopc for class '$class' took " . ( times - $t ) . "s\n" if DEBUG;
53 }
54  
55 # FIXME these should really be methods, I suppose
56  
57 0 ▶sub sym ($$;@) {
Subroutine sym: not called CC=1 SCAR=6.9
Pod undocumented
58 0 my ( $sym, $type, @args ) = @_;
59 0 bless { @args, name => $sym }, "MooseX::Compile::mangled::$type";
60 }
61  
62 0 ▶sub package_name ($;$) {
Subroutine package_name: not called CC=2 SCAR=17.9
Pod undocumented
63 0 my ( $code, $cv ) = @_;
64 0 ▶ $cv ||= B::svref_2object($code);
Condition0%0/3
$cv ||= B::svref_2object($code)
l!l&&r!l&&!r
000
MC/DC0%0/2
$cv ||= B::svref_2object($code)
$cv B::svref_2object($code)
Truth table0%0/3
A ||= B
A= $cvB= B::svref_2object($code)
ABresult
1X1
011
000
65 0 local $@;
66 0 return eval { $cv->GV->STASH->NAME };
67 }
68  
69 0 ▶sub code_name ($;$) {
Subroutine code_name: not called CC=2 SCAR=17.9
Pod undocumented
70 0 my ( $code, $cv ) = @_;
71 0 ▶ $cv ||= B::svref_2object($code);
Condition0%0/3
$cv ||= B::svref_2object($code)
l!l&&r!l&&!r
000
MC/DC0%0/2
$cv ||= B::svref_2object($code)
$cv B::svref_2object($code)
Truth table0%0/3
A ||= B
A= $cvB= B::svref_2object($code)
ABresult
1X1
011
000
72 0 local $@;
73 0 return eval { join("::", package_name($code, $cv), $cv->GV->NAME) };
74 }
75  
76 0 ▶sub verified_code_name ($;$) {
Subroutine verified_code_name: not called CC=3 SCAR=24.8
Pod undocumented
77 0 my ( $code, $cv ) = @_;
78  
79 0 ▶ if ( my $name = code_name($code, $cv) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = &code_name($code, $cv)) 0 0 0
80 0 ▶ if ( verify_code_name($code, $name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&verify_code_name($code, $name)) 0 0 0
81 0 return $name;
82 }
83 }
84  
85 0 return;
86 }
87  
88 0 ▶sub verify_code_name ($$) {
Subroutine verify_code_name: not called CC=1 SCAR=0.6
Pod undocumented
89 0 my ( $code, $name ) = @_;
90  
91 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
92 0 \&$name == $code;
93 }
94  
95 0 ▶sub subref ($;$) {
Subroutine subref: not called CC=8 SCAR=40.4
Pod undocumented
96 0 my ( $code, $name ) = @_;
97  
98 0 ▶ if ( ref $code ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code) { } 0 0 0
99 0 my $cv = B::svref_2object($code);
100 0 ▶ $name ||= code_name($code, $cv);
Condition0%0/3
$name ||= &code_name($code, $cv)
l!l&&r!l&&!r
000
MC/DC0%0/2
$name ||= &code_name($code, $cv)
$name &code_name($code, $cv)
Truth table0%0/3
A ||= B
A= $nameB= &code_name($code, $cv)
ABresult
1X1
011
000
101 0 ▶ if ( $name && verify_code_name($code,$name) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and &verify_code_name($code, $name)) { } 0 0 0
Condition0%0/3
$name and &verify_code_name($code, $name)
!ll&&!rl&&r
000
MC/DC0%0/2
$name and &verify_code_name($code, $name)
$name &verify_code_name($code, $name)
Truth table0%0/3
A and B
A= $nameB= &verify_code_name($code, $name)
ABresult
0X0
100
111
102 0 my @args;
103 0 ▶ if ( -f ( my $file = $cv->FILE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f (my $file = $cv->FILE)) 0 0 0
104 0 my %rev_inc = reverse %INC;
105 0 ▶ push @args, file => $rev_inc{$file} if $rev_inc{$file} !~ /^(?:Moose|metaclass)\.pm$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rev_inc{$file} =~ /^(?:Moose|metaclass)\.pm$/ 0 0 0
106 }
107 0 return sym( $name, "subref", @args );
108 } else {
109 0 ▶ warn "$code has name '$name', but it doesn't point back to the cv" if $name;
Branch: 1 branch
BranchTrueFalseTotal
if $name 0 0 0
110 0 require Data::Dumper;
111 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
112 0 local $Data::Dumper::Deparse = 1;
113 0 warn Data::Dumper::Dumper({
114 name => $name,
115 name_strval => ("". \&$name),
116 name_ref => \&$name,
117 arg_ref => $code,
118 arg_strval => "$code",
119 });
120 0 die "Can't make a symbolic ref to $code, it has no name or the name is invalid";
121 }
122 } else {
123 0 return sym($code, "subref");
124 }
125 }
126  
127 0 ▶sub create_visitor {
Subroutine create_visitor: not called CC=1 SCAR=6.2
Pod undocumented
128 0 my ( $self, %args ) = @_;
129 0 my $class = $args{class};
130  
131 Data::Visitor::Callback->new(
132 0 ▶ "object" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
133 0 my ( $self, $obj ) = @_;
134  
135 0 ▶ return $obj if $obj->isa("Moose::Meta::TypeConstraint");
Branch: 1 branch
BranchTrueFalseTotal
if $obj->isa("Moose::Meta::TypeConstraint") 0 0 0
136  
137 0 $self->visit_ref($obj);
138 },
139 0 ▶ object_final => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
140 0 my ( $self, $obj ) = @_;
141  
142 0 ▶ if ( ref($obj) =~ /^Class::MOP::Class::__ANON__::/x ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref($obj) =~ /^Class::MOP::Class::__ANON__::/x) 0 0 0
143 0 die "Instance of anonymous class cannot be thawed: $obj";
144 }
145  
146 0 return $obj;
147 },
148 0 ▶ "Class::MOP::Class" => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
149 0 my ( $self, $meta ) = @_;
150  
151 0 ▶ if ( $meta->is_immutable ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->is_immutable) 0 0 0
152 0 my $options = $meta->immutable_transformer->options;
153 0 bless( $meta, $meta->{___original_class} ), # it's a copy, we can rebless
154 return bless {
155 class => $meta,
156 options => $options,
157 }, "MooseX::Compile::mangled::immutable_metaclass";
158 }
159
160 0 ▶ if ( $meta->is_anon_class ){
Branch: 1 branch
BranchTrueFalseTotal
if ($meta->is_anon_class) 0 0 0
161 0 warn "Can't reliably store anonymouse metaclasses yet";
162 }
163  
164 0 return $meta;
165 },
166 0 ▶ "Moose::Meta::TypeConstraint" => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
167 0 my ( $self, $constraint ) = @_;
168  
169 0 ▶ if ( defined ( my $name = $constraint->name ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $name = $constraint->name)) { } 0 0 0
170 0 return sym $name, "constraint";
171 } else {
172 0 warn "Anonymous constraint $constraint left in metaclass";
173 0 return $constraint;
174 }
175 },
176 0 ▶ code => sub {
Subroutine __ANON__: not called CC=6 SCAR=34.3
177 0 my ( $self, $code ) = @_;
178  
179 0 ▶ if ( my $subname = code_name($code) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $subname = &code_name($code)) 0 0 0
180 0 ▶ if ( $subname =~ /^Moose::Meta::Method::\w+::(.*)$/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($subname =~ /^Moose::Meta::Method::\w+::(.*)$/) { } 0 0 0
elsif ($subname =~ /^(?:Moose|metaclass)::([^:]+)$/) { } 0 0 0
elsif (not $subname =~ /__ANON__$/) { } 0 0 0
181 # FIXME should this be verified more closely?
182 # sometimes the coderef $code doesn't match \&{ $class::$1 }
183 0 return subref "${class}::$1";
184 } elsif ( $subname =~ /^(?:Moose|metaclass)::([^:]+)$/ ) {
185 0 my $method = $1;
186  
187 0 ▶ if ( $method eq 'meta' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq 'meta') { } 0 0 0
188 0 return subref "${class}::meta";
189 } else {
190 0 die "subname: $subname";
191 }
192 } elsif ( $subname !~ /__ANON__$/ ) {
193 0 return subref $code, $subname;
194 } else {
195 0 warn "Unable to locate symbol for $code ($subname) found in $class";
196 2 ▶ use B::Deparse;
Subroutine BEGIN: called CC=1 SCAR=0.0
197 0 warn B::Deparse->new->coderef2text($code);
198 0 return $code;
199 }
200 }
201  
202 0 return $code;
203 },
204 0 );
205 }
206  
207 0 ▶sub deflate_meta {
Subroutine deflate_meta: not called CC=1 SCAR=6.9
Pod undocumented
208 0 my ( $self, %args ) = @_;
209 0 my $meta = $args{meta};
210
211 0 my $visitor = $self->create_visitor(%args);
212
213 0 $visitor->visit($meta);
214 }
215  
216 0 ▶sub cache_meta {
Subroutine cache_meta: not called CC=1 SCAR=6.9
Pod undocumented
217 0 my ( $self, %args ) = @_;
218 0 my $class = $args{class};
219  
220 0 my $meta = $self->deflate_meta( %args, meta => $class->meta );
221 0 $self->store_meta( %args, meta => $meta );
222 }
223  
224 0 ▶sub store_meta {
Subroutine store_meta: not called CC=2 SCAR=14.6
Pod undocumented
225 0 my ( $self, %args ) = @_;
226 0 my $meta = $args{meta};
227  
228 0 my $mopc_file = $self->cached_meta_file(%args);
229 0 $mopc_file->dir->mkpath;
230  
231 0 local $@;
232 0 eval { Storable::nstore( $meta, $mopc_file ) };
233  
234 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
235 0 require YAML;
236 2 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
237 0 $YAML::UseCode = 1;
238 0 die join("\n", $@, YAML::Dump($meta) );
239 }
240
241 0 if ( DEBUG ) {
242 warn "stored $meta in '$mopc_file'\n";
243 }
244  
245 0 return 1;
246 }
247  
248 0 ▶sub method_category_filters {
Subroutine method_category_filters: not called CC=1 SCAR=4.9
Pod undocumented
249 0 my ( $self, %args ) = @_;
250  
251 return (
252 # FIXME recognize aliased methods
253 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=18.1
254 0 my ( $self, $entry ) = @_;
255 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
256 0 ▶ return "meta" if $entry->{name} eq 'meta' and package_name($entry->{body}) =~ /^(?: Moose | metaclass )/x,
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"name"} eq "meta" and &package_name($entry->{"body"}) =~ /^(?: Moose | metaclass )/x 0 0 0
Condition0%0/3
$entry->{"name"} eq "meta" and &package_name($entry->{"body"}) =~ /^(?: Moose | metaclass )/x
!ll&&!rl&&r
000
MC/DC0%0/2
$entry->{"name"} eq "meta" and &package_name($entry->{"body"}) =~ /^(?: Moose | metaclass )/x
$entry->{"name"} eq "meta" &package_name($entry->{"body"}) =~ /^(?: Moose | metaclass )/x
Truth table0%0/3
A and B
A= $entry->{"name"} eq "meta"B= &package_name($entry->{"body"}) =~ /^(?: Moose | metaclass )/x
ABresult
0X0
100
111
257 },
258 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
259 0 my ( $self, $entry ) = @_;
260 0 ▶ return "generated" if $entry->{meta}->isa("Class::MOP::Method::Generated");
Branch: 1 branch
BranchTrueFalseTotal
if $entry->{"meta"}->isa("Class::MOP::Method::Generated") 0 0 0
261 },
262 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
263 0 my ( $self, $entry ) = @_;
264 0 ▶ return "file" if B::svref_2object($entry->{body})->FILE eq $args{file};
Branch: 1 branch
BranchTrueFalseTotal
if B::svref_2object($entry->{"body"})->FILE eq $args{"file"} 0 0 0
265 },
266 0 ▶ sub { "unknown_methods" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
267 0 );
268 }
269  
270 0 ▶sub function_category_filters {
Subroutine function_category_filters: not called CC=1 SCAR=2.9
Pod undocumented
271 0 my ( $self, %args ) = @_;
272
273 return (
274 # FIXME check for Moose exports, too (Scalar::Util stuff, etc)
275 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=10.1
276 0 my ( $self, $entry ) = @_;
277 2 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
278 0 ▶ return "moose_sugar" if package_name($entry->{body}) eq 'Moose';
Branch: 1 branch
BranchTrueFalseTotal
if &package_name($entry->{"body"}) eq "Moose" 0 0 0
279 },
280 0 ▶ sub { "unknown_functions" },
Subroutine __ANON__: not called CC=1 SCAR=6.9
281 0 );
282 }
283  
284 0 ▶sub extract_code_symbols {
Subroutine extract_code_symbols: not called CC=11 SCAR=48.8
Pod undocumented
285 0 my ( $self, %args ) = @_;
286 0 my $class = $args{class};
287  
288 0 my %seen;
289 my %categorized_symbols;
290  
291 {
292 0 my @method_filters = $self->method_category_filters(%args);
293 0 my $method_map = $class->meta->get_method_map;
294  
295 0 foreach my $name ( sort keys %$method_map ) {
296 0 $seen{$name}++;
297  
298 0 my $method = $method_map->{$name};
299 0 my $body = $method->body;
300  
301 0 my $entry = { name => $name, meta => $method, body => $body };
302  
303 0 foreach my $filter ( @method_filters ) {
304 0 ▶ if ( my $category = $self->$filter($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $category = $self->$filter($entry)) 0 0 0
305 0 ▶ push @{ $categorized_symbols{$category} ||= [] }, $entry;
Condition0%0/2
$categorized_symbols{$category} ||= []
l!l
00
MC/DC0%0/1
$categorized_symbols{$category} ||= []
$categorized_symbols{$category}
Truth table0%0/2
A
A= $categorized_symbols{$category}
Aresult
11
00
306 0 last;
307 }
308 }
309 }
310 }
311  
312 {
313 0 ▶ my %symbols; @symbols{@{ Class::Inspector->functions($class) || [] }} = @{ Class::Inspector->function_refs($class) || [] };
Condition0%0/4
'Class::Inspector'->function_refs($class) || []
l!l
00
'Class::Inspector'->functions($class) || []
l!l
00
MC/DC0%0/1
'Class::Inspector'->function_refs($class) || []
'Class::Inspector'->function_refs($class)
MC/DC0%0/1
'Class::Inspector'->functions($class) || []
'Class::Inspector'->functions($class)
Truth table0%0/2
A
A= 'Class::Inspector'->function_refs($class)
Aresult
11
00
Truth table0%0/2
A
A= 'Class::Inspector'->functions($class)
Aresult
11
00
314  
315 0 my @function_filters = $self->function_category_filters(%args);
316  
317 0 foreach my $name ( sort grep { not $seen{$_}++ } keys %symbols ) {
318 0 my $body = $symbols{$name};
319 0 my $entry = { name => $name, body => $body };
320  
321 0 foreach my $filter ( @function_filters ) {
322 0 ▶ if ( my $category = $self->$filter($entry) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $category = $self->$filter($entry)) 0 0 0
323 0 ▶ push @{ $categorized_symbols{$category} ||= [] }, $entry;
Condition0%0/2
$categorized_symbols{$category} ||= []
l!l
00
MC/DC0%0/1
$categorized_symbols{$category} ||= []
$categorized_symbols{$category}
Truth table0%0/2
A
A= $categorized_symbols{$category}
Aresult
11
00
324 0 last;
325 }
326 }
327 }
328 }
329  
330 0 return %categorized_symbols;
331 }
332  
333 0 ▶sub compile_code_symbols {
Subroutine compile_code_symbols: not called CC=2 SCAR=17.9
Pod undocumented
334 0 my ( $self, %args ) = @_;
335  
336 0 my $symbols = $args{all_symbols};
337  
338 0 my @ret;
339  
340 0 foreach my $category ( @{ $args{'symbol_categories'} } ) {
341 0 my $method = "compile_${category}_code_symbols";
342 0 push @ret, $self->$method( %args, symbols => delete($symbols->{$category}) );
343 }
344  
345 0 @ret;
346 }
347  
348 0 ▶sub compile_file_code_symbols {
Subroutine compile_file_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
349 # this is already taken care of by the inclusion of the whole .pm after the preamble
350 0 return;
351 }
352  
353 0 ▶sub compile_meta_code_symbols {
Subroutine compile_meta_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
354 # we fake this one
355 0 return;
356 }
357  
358 0 ▶sub compile_moose_exports_code_symbols {
Subroutine compile_moose_exports_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
359 # not yet implemented
360 0 return;
361 }
362  
363 0 ▶sub compile_moose_sugar_code_symbols {
Subroutine compile_moose_sugar_code_symbols: not called CC=3 SCAR=24.8
Pod undocumented
364 0 my ( $self, %args ) = @_;
365 return map {
366 0 my $name = $_->{name};
367 0 my $proto = prototype($_->{body});
368 0 ▶ $proto = $proto ? " ($proto)" : "";
Branch: 1 branch
BranchTrueFalseTotal
$proto ? : 0 0 0
369 0 "*$name = Sub::Name::subname('Moose::$name', sub$proto { });";
370 0 ▶ } @{ $args{symbols} || [] };
Condition0%0/2
$args{'symbols'} || []
l!l
00
MC/DC0%0/1
$args{'symbols'} || []
$args{'symbols'}
Truth table0%0/2
A
A= $args{'symbols'}
Aresult
11
00
371 }
372  
373  
374 0 ▶sub compile_generated_code_symbols {
Subroutine compile_generated_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
375 0 my ( $self, %args ) = @_;
376 0 map { sprintf "*%s = %s;", $_->name => $self->compile_method(%args, method => $_) } map { $_->{meta} } @{ $args{symbols} };
377 }
378  
379 0 ▶sub compile_aliased_code_symbols {
Subroutine compile_aliased_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
380 0 return;
381 }
382  
383 0 ▶sub compile_unknown_method_code_symbols {
Subroutine compile_unknown_method_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
384 0 return;
385 }
386  
387 0 ▶sub compile_unknown_function_code_symbols {
Subroutine compile_unknown_function_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
388 0 return;
389 }
390  
391 0 ▶sub compile_method {
Subroutine compile_method: not called CC=6 SCAR=37.4
Pod undocumented
392 0 my ( $self, %args ) = @_;
393 0 my ( $class, $method ) = @args{qw(class method)};
394  
395 0 my $d = B::Deparse->new;
396  
397 0 my $body = $method->body;
398  
399 0 my $body_str = $d->coderef2text($body);
400  
401 0 my $closure_vars = PadWalker::closed_over($body);
402  
403 0 my @env;
404  
405 0 ▶ if ( my $constraints = delete $closure_vars->{'@type_constraints'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $constraints = delete $closure_vars->{"\@type_constraints"}) 0 0 0
406 my @constraint_code = map {
407 0 my $name = $_->name;
408  
409 0 ▶ defined $name
Branch: 1 branch
BranchTrueFalseTotal
defined $name ? : 0 0 0
410 ? "Moose::Util::TypeConstraints::find_type_constraint(". dump($name) .")"
411 : "die 'missing constraint'"
412 } @$constraints;
413
414 0 push @env, "CORE::require Moose::Util::TypeConstraints::OptimizedConstraints", join("\n ", 'my @type_constraints = (', map { "$_," } @constraint_code ) . "\n)",
415 }
416
417 push @env, map {
418 0 my $ref = $closure_vars->{$_};
419  
420 0 ▶ my $scalar = ref($ref) eq 'SCALAR' || ref($ref) eq 'REF';
Condition0%0/3
ref $ref eq 'SCALAR' || ref $ref eq 'REF'
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $ref eq 'SCALAR' || ref $ref eq 'REF'
ref $ref eq 'SCALAR' ref $ref eq 'REF'
Truth table0%0/3
A || B
A= ref $ref eq 'SCALAR'B= ref $ref eq 'REF'
ABresult
1X1
011
000
421  
422 "my $_ = " . ( $scalar
423 ? $self->_value_to_perl($$ref)
424 0 ▶ : "(" . join(", ", map { $self->_value_to_perl($_) } @$ref ) . ")" )
Branch: 1 branch
BranchTrueFalseTotal
$scalar ? : 0 0 0
425 } keys %$closure_vars;
426  
427 0 my $name = code_name($body);
428 0 my $quoted_name = dump($name);
429  
430 0 ▶ if ( @env ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@env) { } 0 0 0
431 0 my $env = join(";\n\n", @env);
432 0 $env =~ s/^/ /gm;
433 0 return "Sub::Name::subname( $quoted_name, do {\n$env;\n\n\nsub $body_str\n})";
434 } else {
435 0 return "Sub::Name::subname( $quoted_name, sub $body_str )";
436 }
437 }
438  
439 0 ▶sub _value_to_perl {
Subroutine _value_to_perl: not called CC=3 SCAR=24.8
440 0 my ( $self, $value ) = @_;
441  
442 0 ▶ ( (ref($value)||'') eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
(ref $value || '') eq 'CODE' ? : 0 0 0
Condition0%0/2
ref $value || ''
l!l
00
MC/DC0%0/1
ref $value || ''
ref $value
Truth table0%0/2
A
A= ref $value
Aresult
11
00
443 ? $self->_subref_to_perl($value)
444 : Data::Dump::dump($value) )
445 }
446  
447 0 ▶sub _subref_to_perl {
Subroutine _subref_to_perl: not called CC=3 SCAR=24.8
448 0 my ( $self, $subref ) = @_;
449  
450 0 my %rev_inc = reverse %INC;
451  
452 0 ▶ if ( ( my $name = code_name($subref) ) !~ /__ANON__$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not +(my $name = &code_name($subref)) =~ /__ANON__$/) { } 0 0 0
453 0 ▶ if ( -f ( my $file = B::svref_2object($subref)->FILE ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f (my $file = B::svref_2object($subref)->FILE)) { } 0 0 0
454 0 return "do { require " . dump($rev_inc{$file}) . "; \\&$name }";
455 } else {
456 0 return '\&' . $name;
457 }
458 } else {
459 0 "sub " . B::Deparse->new->coderef2text($subref);
460 }
461 }
462  
463 0 ▶sub write_pmc_file {
Subroutine write_pmc_file: not called CC=5 SCAR=34.0
Pod undocumented
464 0 my ( $self, %args ) = @_;
465  
466 0 my ( $class, $short_name, $file, $pmc_file ) = @args{qw(class short_name file pmc_file)};
467  
468 0 $pmc_file->dir->mkpath;
469  
470 0 ▶ open my $pm_fh, "<", $file or die "open($file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $pm_fh, "<", $file or die "open($file): $!" 0 0 0
471 0 ▶ open my $pmc_fh, ">", "$pmc_file" or die "Can't write .pmc, open($pmc_file): $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $pmc_fh, ">", "$pmc_file" or die "Can't write .pmc, open($pmc_file): $!" 0 0 0
472  
473 0 local $/;
474  
475 0 my $pm = <$pm_fh>;
476  
477 0 close $pm_fh;
478  
479 0 ▶ print $pmc_fh "$1\n\n" if $pm =~ /^(\#\!.*)/; # copy shebang
Branch: 1 branch
BranchTrueFalseTotal
if $pm =~ /^(\#\!.*)/ 0 0 0
480  
481 0 print $pmc_fh $self->pmc_preamble( %args ), "\n";
482  
483 0 print $pmc_fh "# verbatim copy of $file follows\n";
484 0 print $pmc_fh "# line 1\n";
485  
486 0 print $pmc_fh $pm;
487  
488 0 ▶ close $pmc_fh or die "Can't write .pmc, close($pmc_file): $!";
Branch: 1 branch
BranchTrueFalseTotal
close $pmc_fh or die "Can't write .pmc, close($pmc_file): $!" 0 0 0
489  
490 0 warn "wrote PMC file '$pmc_file'\n" if DEBUG;
491 }
492  
493 0 ▶sub pmc_preamble_comment {
Subroutine pmc_preamble_comment: not called CC=1 SCAR=6.9
Pod undocumented
494 0 my ( $self, %args ) = @_;
495  
496 0 return <<COMMENT;
497 # This file is generated by MooseX::Compile, and contains a cached
498 # version of the class '$args{class}'.
499 COMMENT
500 }
501  
502 0 ▶sub pmc_preamble_header {
Subroutine pmc_preamble_header: not called CC=1 SCAR=6.9
Pod undocumented
503 0 my( $self, %args ) = @_;
504 0 my $class = $args{class};
505  
506 0 return join("\n\n\n", map { my $method = "pmc_preamble_header_$_"; $self->$method(%args) } $self->pmc_preamble_header_pieces(%args) );
507 }
508  
509 0 ▶sub pmc_preamble_header_pieces {
Subroutine pmc_preamble_header_pieces: not called CC=1 SCAR=6.9
Pod undocumented
510 0 return qw(timing modules register_pmc hide_moose);
511 }
512  
513 0 ▶sub pmc_preamble_header_timing {
Subroutine pmc_preamble_header_timing: not called CC=1 SCAR=6.9
Pod undocumented
514 0 return <<'TIMING';
515 # used in debugging output if any
516 my $__mx_compile_t; BEGIN { $__mx_compile_t = times }
517 TIMING
518 }
519  
520 0 ▶sub pmc_preamble_header_modules {
Subroutine pmc_preamble_header_modules: not called CC=1 SCAR=6.9
Pod undocumented
521 return <<'MODULES'
522 # load a few modules we need
523 use Sub::Name ();
524 use Scalar::Util ();
525 MODULES
526 0 }
527  
528 0 ▶sub pmc_preamble_header_register_pmc {
Subroutine pmc_preamble_header_register_pmc: not called CC=1 SCAR=6.9
Pod undocumented
529 0 my ( $self, %args ) = @_;
530 0 my ( $quoted_class, $version ) = @args{qw(quoted_class quoted_compiler_version)};
531  
532 0 return <<REGISTER;
533 # Register this file as a PMC
534 use MooseX::Compile::Bootstrap (
535 class => $quoted_class,
536 file => __FILE__,
537 version => $version,
538 );
539 REGISTER
540 }
541  
542 0 ▶sub pmc_preamble_header_hide_moose {
Subroutine pmc_preamble_header_hide_moose: not called CC=1 SCAR=6.9
Pod undocumented
543 0 my ( $self, %args ) = @_;
544  
545 0 my $hide = <<'#\'HIDE_MOOSE';
546 #\
547 # disable requiring and importing of Moose from this compile class
548 my ( $__mx_compile_prev_require, %__mx_compile_overridden_imports );
549  
550 BEGIN {
551 $__mx_compile_prev_require = defined &CORE::GLOBAL::require ? \&CORE::GLOBAL::require : undef;
552  
553 no warnings 'redefine';
554  
555 # FIXME move this to Bootstrap? Bootstrap->override_global_require( class => $$quoted_class$$ )?
556 *CORE::GLOBAL::require = sub {
557 my ( $faked_class ) = ( $_[0] =~ m/^ ( Moose | metaclass ) \.pm $/x );
558  
559 return 1 if caller() eq $$quoted_class$$ and $faked_class;
560  
561 my $hook;
562  
563 if ( $faked_class and not $INC{$_[0]} ) {
564 # load Moose or metaclass in a clean env, and then wrap it's import()
565 no strict 'refs';
566  
567 my $import = "${faked_class}::import";
568  
569 my $wrapper = \&$import;
570  
571 undef *$import; # clean out the symbol so it doesn't warn about redefining
572  
573 $hook = bless [sub {
574 $__mx_compile_overridden_imports{$faked_class} = \&$import; # stash the real import
575 *$import = $wrapper;
576 }], "MooseX::Compile::Scope::Guard";
577 }
578  
579 if ( $__mx_compile_prev_require ) {
580 &$__mx_compile_prev_require;
581 } else {
582 require $_[0];
583 }
584 };
585  
586 foreach my $class qw(Moose metaclass) {
587 no strict 'refs';
588  
589 my $import = "${class}::import";
590  
591 $__mx_compile_overridden_imports{$class} = defined &$import && \&$import;
592  
593 *$import = sub {
594 if ( caller eq $$quoted_class$$ ) {
595 if ( $class eq 'Moose' ) {
596 strict->import;
597 warnings->import;
598 }
599  
600 return;
601 }
602  
603 if ( my $sub = $__mx_compile_overridden_imports{\$class} ) {
604 goto $sub;
605 }
606  
607 return;
608 };
609 }
610 }
611 #'HIDE_MOOSE
612  
613 0 $hide =~ s/\$\$(\w+)\$\$/$args{$1}/ge;
614  
615 0 return $hide;
616 }
617  
618 0 ▶sub pmc_preamble_setup_env {
Subroutine pmc_preamble_setup_env: not called CC=1 SCAR=6.9
Pod undocumented
619 0 my ( $self, %args ) = @_;
620  
621 0 my $class = $args{class};
622  
623 0 my $quoted_class = dump($class);
624  
625 0 my $decl = $self->pmc_preamble_class_def_for_begin(%args);
626  
627 0 return <<ENV;
628 # stub the sugar
629 BEGIN {
630 package $class;
631  
632 my \$fake_meta = bless { name => $quoted_class }, "MooseX::Compile::MetaBlackHole";
633 sub meta { \$fake_meta }
634  
635 $decl
636  
637 our \$__mx_is_compiled = 1;
638 }
639 ENV
640 }
641  
642 0 ▶sub pmc_preamble_class_def_for_begin {
Subroutine pmc_preamble_class_def_for_begin: not called CC=1 SCAR=6.9
Pod undocumented
643 0 my ( $self, %args ) = @_;
644  
645 0 join("\n\n", $self->compile_code_symbols( %args, symbol_categories => [qw(moose_sugar moose_exports)] ) );
646 }
647  
648 0 ▶sub pmc_preamble_at_end {
Subroutine pmc_preamble_at_end: not called CC=1 SCAR=6.9
Pod undocumented
649 0 my ( $self, %args ) = @_;
650 0 my ( $class, $code ) = @args{qw(class code)};
651  
652 return <<HOOK
653 # try to approximate the time that Moose generated code enters the class
654 # this presumes you didn't stick the moose sugar in a BEGIN { } block
655 my \$__mx_compile_run_at_end = bless [ sub {
656  
657 $code
658  
659 } ], "MooseX::Compile::Scope::Guard";
660 HOOK
661 0 }
662  
663 0 ▶sub pmc_preamble_unhide_moose {
Subroutine pmc_preamble_unhide_moose: not called CC=1 SCAR=6.9
Pod undocumented
664 0 my ( $self, %args ) = @_;
665  
666 0 return <<'#\'UNHIDE_MOOSE';
667 #\
668 # un-hijack CORE::GLOBAL::require so that it no longer hides Moose from this class
669 # and undo the import wrappers that likewise prevent importing if it's already loaded
670  
671 foreach my $class ( keys %__mx_compile_overridden_imports ) {
672 my $import = "${class}::import";
673 no strict 'refs';
674 if ( my $prev = delete $__mx_compile_overridden_imports{$class} ) {
675 no warnings 'redefine';
676 *$import = $prev;
677 } else {
678 delete ${ "${class}::" }{import};
679 }
680 }
681  
682 if ( $__mx_compile_prev_require ) {
683 no warnings 'redefine';
684 *CORE::GLOBAL::require = $__mx_compile_prev_require;
685 } else {
686 delete $CORE::GLOBAL::{require};
687 }
688 #'UNHIDE_MOOSE
689 }
690  
691 0 ▶sub pmc_preamble_generated_code {
Subroutine pmc_preamble_generated_code: not called CC=1 SCAR=6.9
Pod undocumented
692 0 my ( $self, %args ) = @_;
693  
694 0 my $class = $args{class};
695  
696 0 return $self->pmc_preamble_at_end(
697 %args,
698 code => join("\n\n",
699 $self->pmc_preamble_unhide_moose(%args),
700 $self->pmc_preamble_generated_code_body(%args),
701 qq{warn "loading of class '$class' finished in " . (times - \$__mx_compile_t) . "s\\n" if MooseX::Compile::Base::DEBUG();},
702 ),
703 );
704 }
705  
706 0 ▶sub pmc_preamble_generated_code_body {
Subroutine pmc_preamble_generated_code_body: not called CC=1 SCAR=6.9
Pod undocumented
707 0 my ( $self, %args ) = @_;
708  
709 0 my $class = $args{class};
710  
711 0 my $quoted_class = dump($class);
712  
713 0 return join("\n",
714 "package $class;",
715 $self->pmc_preamble_class_def_for_end(%args),
716 qq{warn "bootstrap of class '$class' finished in " . (times - \$__mx_compile_t) . "s\\n" if MooseX::Compile::Base::DEBUG();},
717 );
718 }
719  
720 0 ▶sub pmc_preamble_class_def_for_end {
Subroutine pmc_preamble_class_def_for_end: not called CC=1 SCAR=6.9
Pod undocumented
721 0 my ( $self, %args ) = @_;
722  
723 return (
724 0 $self->pmc_preamble_define_isa(%args),
725 $self->pmc_preamble_define_code_symbols(%args),
726 $self->pmc_preamble_call_post_hook(%args),
727 );
728 }
729  
730 0 ▶sub pmc_preamble_define_isa {
Subroutine pmc_preamble_define_isa: not called CC=1 SCAR=6.9
Pod undocumented
731 0 my ( $self, %args ) = @_;
732  
733 0 my $ISA = dump($args{class}->meta->superclasses);
734  
735 return <<ISA
736 our \@ISA = $ISA;
737 MooseX::Compile::Bootstrap->load_classes(\@ISA);
738 ISA
739 0 }
740  
741 0 ▶sub pmc_preamble_define_code_symbols {
Subroutine pmc_preamble_define_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
742 0 my ( $self, %args ) = @_;
743  
744 return (
745 0 $self->compile_code_symbols(%args, symbol_categories => [qw(generated aliased)]),
746 $self->pmc_preamble_faked_code_symbols(%args),
747 );
748 }
749  
750 0 ▶sub pmc_preamble_faked_code_symbols {
Subroutine pmc_preamble_faked_code_symbols: not called CC=1 SCAR=6.9
Pod undocumented
751 0 my ( $self, %args ) = @_;
752  
753 return <<METHODS
754 {
755 no warnings 'redefine';
756 *meta = Sub::Name::subname("Moose::meta", sub { MooseX::Compile::Bootstrap->load_cached_meta( class => __PACKAGE__, pmc_file => __FILE__ . 'c' ) });
757 }
758 METHODS
759 0 }
760  
761 0 ▶sub pmc_preamble_call_post_hook {
Subroutine pmc_preamble_call_post_hook: not called CC=1 SCAR=6.9
Pod undocumented
762 0 my ( $self, %args ) = @_;
763 0 my $class = $args{class};
764  
765 return <<HOOK
766 ${class}::__mx_compile_post_hook()
767 if defined \&${class}::__mx_compile_post_hook;
768 HOOK
769 0 }
770  
771 0 ▶sub pmc_preamble {
Subroutine pmc_preamble: not called CC=1 SCAR=4.3
Pod undocumented
772 0 my ( $self, %args ) = @_;
773 0 my ( $class, $file ) = @args{qw(class file)};
774  
775 0 ( my $short_name = "$class.pm" ) =~ s{::}{/}g;
776  
777 0 $args{short_name} = $short_name;
778  
779 0 $args{quoted_class} = dump($class);
780  
781 0 $args{compiler_version} = $MooseX::Compile::Base::VERSION;
782  
783 0 $args{quoted_compiler_version} = dump($MooseX::Compile::Base::VERSION);
784  
785 0 $args{all_symbols} = { $self->extract_code_symbols(%args) };
786  
787 0 my $code = join("\n",
788 $self->pmc_preamble_comment(%args),
789 $self->pmc_preamble_header(%args),
790 $self->pmc_preamble_setup_env(%args),
791 $self->pmc_preamble_generated_code(%args),
792 $self->pmc_preamble_footer(%args),
793 );
794  
795 0 delete @{ $args{all_symbols} }{qw(file meta unknown_methods unknown_functions)};
796  
797 0 if ( DEBUG && keys %{ $args{all_symbols} } ) {
798 2 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
799 warn "leftover symbols: " . Dumper($args{all_symbols});
800 }
801  
802 0 return $code;
803 }
804  
805 0 ▶sub pmc_preamble_footer {
Subroutine pmc_preamble_footer: not called CC=1 SCAR=6.9
Pod undocumented
806 0 my ( $self, %args ) = @_;
807 return <<FOOTER
808 BEGIN { warn "giving control back to original '$args{short_name}', bootstrap preamble took " . (times - \$__mx_compile_t) . "s\\n" if MooseX::Compile::Base::DEBUG() }
809 FOOTER
810 0 }
811  
812 2 __PACKAGE__
813  
814 __END__
↑ Summary Compile/Base.pm »