Skip to content

Commit

Permalink
Create lexical stub package for 'require'd module.
Browse files Browse the repository at this point in the history
require Test; will now create a lexically scoped stub package "Test" to be
replaced by the loaded module's package at runtime. Nested packages NYI.
With this, "require Test; Test::plan(1);" works again.

Fixes RT #125083
  • Loading branch information
niner committed Mar 6, 2017
1 parent 63cf5ca commit 3e86d0f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/Perl6/Actions.nqp
Expand Up @@ -1927,6 +1927,21 @@ class Perl6::Actions is HLL::Actions does STDActions {
:name<&REQUIRE_IMPORT>,
$compunit_past,
),'require');
if $target_package && !$longname.contains_indirect_lookup() {
my $stub := $*W.pkg_create_mo($/, %*HOW<package>, :name($longname.name));
$*W.pkg_compose($/, $stub);
my $stubvar := QAST::Var.new(
:name($longname.name),
:scope('lexical'),
:decl('var'),
);
$lexpad[0].unshift($stubvar);
$lexpad.symbol($longname.name, :scope('lexical'), :value($stub));
$require_past.push($target_package);
}
else {
$require_past.push($*W.symbol_lookup(['Any'], $/));
}
if $<EXPR> {
my $p6_argiter := $*W.compile_time_evaluate($/, $<EXPR>.ast).eager.iterator;
my $IterationEnd := $*W.find_symbol(['IterationEnd']);
Expand Down
3 changes: 2 additions & 1 deletion src/core/operators.pm
Expand Up @@ -604,12 +604,13 @@ sub INDIRECT_NAME_LOOKUP($root, *@chunks) is raw {
)
}

sub REQUIRE_IMPORT($compunit, *@syms --> Nil) {
sub REQUIRE_IMPORT($compunit, $module, *@syms --> Nil) {
my $handle := $compunit.handle;
my $DEFAULT := $handle.export-package()<DEFAULT>.WHO;
my $GLOBALish := $handle.globalish-package;
my @missing;
# Set the runtime values for compile time stub symbols
CALLER::{$module} := $GLOBALish{$module} if $module;
for @syms {
unless $DEFAULT.EXISTS-KEY($_) {
@missing.push: $_;
Expand Down

0 comments on commit 3e86d0f

Please sign in to comment.