Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Further simplify creation of CompUnit object
  • Loading branch information
lizmat committed Aug 4, 2014
1 parent f2842a5 commit 225b5e0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
30 changes: 14 additions & 16 deletions src/core/CompUnit.pm
Expand Up @@ -15,19 +15,22 @@ class CompUnit {
my $default-from = 'Perl6';
my %instances;

method new( $path is copy, :$name is copy, :$extension is copy, :$from = $default-from ) {

# remove precomp extension if a precomp file
my $precomp-ext = $*VM.precomp-ext;
$path = $path.subst(/\.($precomp-ext)$/,"");
my $has-precomp = ?$0;
method new(
$path is copy,
:$name is copy,
:$extension is copy,
:$from = $default-from,
:$has-source is copy,
:$has-precomp is copy,
) {

# set name / extension if not already given
if !$name or !$extension {
if !$name or !$extension.defined {
my $file;
for $path.rindex($slash) -> $i {
$file = $i.defined ?? $path.substr($i+1) !! $path;
}

# no $slash in char class
if $file ~~ m/ (<-[\\/.]>+) . (<-[.]>+) $/ {
$name ||= ~$0;
Expand All @@ -36,16 +39,11 @@ class CompUnit {
}

# sanity test
my $has-source;
my $precomp-ext = $*VM.precomp-ext;
$path = IO::Spec.rel2abs($path);
for $path.IO -> $io {
return Nil if $io.d; # cannot be a directory

# do we have a precomp?
$has-source = $io.e;
$has-precomp ||= "$path.$precomp-ext".IO.e;
return Nil unless $has-source or $has-precomp;
}
$has-source //= ?$path.IO.f;
$has-precomp //= ?"$path.$precomp-ext".IO.f;
return Nil unless $has-source or $has-precomp;

$global.protect( { %instances{$path} //= self.bless(
:$path,
Expand Down
13 changes: 9 additions & 4 deletions src/core/CompUnitRepo/Local/File.pm
Expand Up @@ -39,15 +39,20 @@ class CompUnitRepo::Local::File does CompUnitRepo::Locally {
if %extensions{$from} -> @extensions {
for @extensions -> $extension {
my $path = $base ~ $extension;
return %seen{$base} = CompUnit.new($path,:$name,:$extension)
if $path.IO.f or ($path ~ '.' ~ $precomp-ext).IO.f;
return %seen{$base} = CompUnit.new(
$path, :$name, :$extension, :has-source
) if $path.IO.f;
return %seen{$base} = CompUnit.new(
$path, :$name, :$extension, :!has-source, :has-precomp
) if ($path ~ '.' ~ $precomp-ext).IO.f;
}
}

# no extensions to check, just check compiled version
elsif $base ~ $precomp-ext -> $path {
return %seen{$base} = CompUnit.new($path, :$name)
if $path.IO.f;
return %seen{$base} = CompUnit.new(
$path, :$name, :extension(''), :!has-source, :has-precomp
) if $path.IO.f;
}

# alas
Expand Down

0 comments on commit 225b5e0

Please sign in to comment.