Skip to content

Commit

Permalink
Implement absolute path lookup in ModuleLoader.
Browse files Browse the repository at this point in the history
  • Loading branch information
donaldh committed Sep 12, 2013
1 parent 69c3cce commit efac451
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 22 deletions.
15 changes: 13 additions & 2 deletions src/Perl6/ModuleLoader.nqp
Expand Up @@ -10,6 +10,8 @@ sub DEBUG(*@strs) {
class Perl6::ModuleLoader does Perl6::ModuleLoaderVMConfig {
my %modules_loaded;
my %settings_loaded;
my $absolute_path_func;

my %language_module_loaders := nqp::hash(
'NQP', nqp::gethllsym('nqp', 'ModuleLoader'),
);
Expand All @@ -20,12 +22,21 @@ class Perl6::ModuleLoader does Perl6::ModuleLoaderVMConfig {
%language_module_loaders{$lang} := $loader;
}

method register_absolute_path_func($func) {
$absolute_path_func := $func;
}

method absolute_path($path) {
$absolute_path_func ?? $absolute_path_func($path) !! $path;
}


method ctxsave() {
$*MAIN_CTX := nqp::ctxcaller(nqp::ctx());
$*CTXSAVE := 0;
}

method search_path() {
method search_path() {
# See if we have an @*INC set up, and if so just use that.
my $PROCESS := nqp::gethllsym('perl6', 'PROCESS');
if !nqp::isnull($PROCESS) && nqp::existskey($PROCESS.WHO, '@INC') {
Expand All @@ -37,7 +48,7 @@ class Perl6::ModuleLoader does Perl6::ModuleLoaderVMConfig {
}
}
}

# Too early to have @*INC; probably no setting yet loaded to provide
# the PROCESS initialization.
my @search_paths;
Expand Down
4 changes: 4 additions & 0 deletions src/core/IO/Spec.pm
Expand Up @@ -61,3 +61,7 @@ my class IO::Spec {
method abs2rel( |c ) { $SPEC.abs2rel( |c ) }
method rel2abs( |c ) { $SPEC.rel2abs( |c ) }
}

nqp::gethllsym('perl6', 'ModuleLoader').register_absolute_path_func(
sub ($path) { return IO::Spec.rel2abs($path); }
);
17 changes: 7 additions & 10 deletions src/vm/jvm/ModuleLoaderVMConfig.nqp
Expand Up @@ -9,23 +9,20 @@ role Perl6::ModuleLoaderVMConfig {

# Locates files we could potentially load for this module.
method locate_candidates($module_name, @prefixes, :$file?) {
my $PROCESS := nqp::gethllsym('perl6', 'PROCESS');
my $curdir := !nqp::isnull($PROCESS) && nqp::existskey($PROCESS.WHO, '$CWD')
?? nqp::atkey($PROCESS.WHO, '$CWD') ~ '/'
!! '';
# If its name contains a slash or dot treat is as a path rather than a package name.
my @candidates;
if nqp::defined($file) {
if nqp::stat($curdir ~ $file, 0) {
$file := nqp::gethllsym('perl6', 'ModuleLoader').absolute_path($file);
if nqp::stat($file, 0) {
my %cand;
%cand<key> := $curdir ~ $file;
%cand<key> := $file;
my $dot := nqp::rindex($file, '.');
my $ext := $dot >= 0 ?? nqp::substr($file, $dot, nqp::chars($file) - $dot) !! '';
if $ext eq 'class' || $ext eq 'jar' {
%cand<load> := $curdir ~ $file;
%cand<load> := $file;
}
else {
%cand<pm> := $curdir ~ $file;
%cand<pm> := $file;
}
@candidates.push(%cand);
}
Expand All @@ -40,7 +37,7 @@ role Perl6::ModuleLoaderVMConfig {

# Go through the prefixes and build a candidate list.
for @prefixes -> $prefix {
$prefix := ~$prefix;
$prefix := nqp::gethllsym('perl6', 'ModuleLoader').absolute_path(~$prefix);
my $have_pm := nqp::stat("$prefix/$pm_path", 0);
my $have_pm6 := nqp::stat("$prefix/$pm6_path", 0);
my $have_class := nqp::stat("$prefix/$class_path", 0);
Expand Down Expand Up @@ -82,7 +79,7 @@ role Perl6::ModuleLoaderVMConfig {
my $path := "$setting_name.setting.jar";
my @prefixes := self.search_path();
for @prefixes -> $prefix {
$prefix := ~$prefix;
$prefix := nqp::gethllsym('perl6', 'ModuleLoader').absolute_path(~$prefix);
if nqp::stat("$prefix/$path", 0) {
$path := "$prefix/$path";
last;
Expand Down
17 changes: 7 additions & 10 deletions src/vm/parrot/ModuleLoaderVMConfig.nqp
Expand Up @@ -12,23 +12,20 @@ role Perl6::ModuleLoaderVMConfig {

# Locates files we could potentially load for this module.
method locate_candidates($module_name, @prefixes, :$file?) {
my $PROCESS := nqp::gethllsym('perl6', 'PROCESS');
my $curdir := !nqp::isnull($PROCESS) && nqp::existskey($PROCESS.WHO, '$CWD')
?? nqp::atkey($PROCESS.WHO, '$CWD') ~ '/'
!! '';
# If its name contains a slash or dot treat is as a path rather than a package name.
my @candidates;
if nqp::defined($file) {
if nqp::stat($curdir ~ $file, 0) {
$file := nqp::gethllsym('perl6', 'ModuleLoader').absolute_path($file);
if nqp::stat($file, 0) {
my %cand;
%cand<key> := $curdir ~ $file;
%cand<key> := $file;
my $dot := nqp::rindex($file, '.');
my $ext := $dot >= 0 ?? nqp::substr($file, $dot, nqp::chars($file) - $dot) !! '';
if $ext eq 'pbc' || $ext eq 'pir' {
%cand<load> := $curdir ~ $file;
%cand<load> := $file;
}
else {
%cand<pm> := $curdir ~ $file;
%cand<pm> := $file;
}
@candidates.push(%cand);
}
Expand All @@ -43,7 +40,7 @@ role Perl6::ModuleLoaderVMConfig {

# Go through the prefixes and build a candidate list.
for @prefixes -> $prefix {
$prefix := ~$prefix;
$prefix := nqp::gethllsym('perl6', 'ModuleLoader').absolute_path(~$prefix);
my $have_pm := nqp::stat("$prefix/$pm_path", 0);
my $have_pm6 := nqp::stat("$prefix/$pm6_path", 0);
my $have_pir := nqp::stat("$prefix/$pir_path", 0);
Expand Down Expand Up @@ -93,7 +90,7 @@ last; # temporary, until we actually don't do just @candidates[0]
my $path := "$setting_name.setting.pbc";
my @prefixes := self.search_path();
for @prefixes -> $prefix {
$prefix := ~$prefix;
$prefix := nqp::gethllsym('perl6', 'ModuleLoader').absolute_path(~$prefix);
if nqp::stat("$prefix/$path", 0) {
$path := "$prefix/$path";
last;
Expand Down

0 comments on commit efac451

Please sign in to comment.