Skip to content

Commit

Permalink
Switch symbol importation to lexical by default; actually load module…
Browse files Browse the repository at this point in the history
…s during the compile. Probably has quirks, but a step towards what we want.
  • Loading branch information
jnthn committed Mar 10, 2010
1 parent 8d76887 commit a11211c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/Perl6/Actions.pm
Expand Up @@ -353,17 +353,20 @@ method statement_control:sym<use>($/) {
}

# Need to immediately load module and get lexicals stubbed in.
# XXX TODO
Perl6::Module::Loader.need($name, %adverbs);
Perl6::Module::Loader.stub_lexical_imports($name, @BLOCK[0]);

# Also need code to do the actual loading and import at runtime.
# Also need code to do the actual loading and import at runtime (though
# need won't repeat its work if already carried out; we mainly need
# this for pre-compilation to PIR to work).
my @ns := pir::split__PSS('::', 'Perl6::Module');
@BLOCK[0].loadinit.push(
PAST::Op.new( :pasttype('callmethod'), :name('need'),
PAST::Var.new( :name('Loader'), :namespace(@ns), :scope('package') ),
$name,
PAST::Op.new( :pirop('getattribute PPS'), $adverbs_ast, '$!storage' )
));
@BLOCK[0].loadinit.push(
@BLOCK[0].push(
PAST::Op.new( :pasttype('callmethod'), :name('import'),
PAST::Var.new( :name('Loader'), :namespace(@ns), :scope('package') ),
$name
Expand Down
23 changes: 19 additions & 4 deletions src/Perl6/Module/Loader.pm
Expand Up @@ -2,6 +2,8 @@
# and import.
class Perl6::Module::Loader;

our %LOADED;

method need($name, %name_adverbs) {
# Use locator to find the module.
my @inc := pir::get_hll_global__PS('@INC');
Expand All @@ -18,7 +20,10 @@ method need($name, %name_adverbs) {
unless pir::stat__ISI($pir_file, 0) {
pir::die("Sorry, for now you must manually compile .pm modules to .pir (missing for $name).");
}
pir::load_bytecode__vS($pir_file);
unless %LOADED{$pir_file} {
pir::load_bytecode__vS($pir_file);
%LOADED{$pir_file} := 1;
}
1;
}

Expand All @@ -32,14 +37,24 @@ method get_imports($name) {
}

method stub_lexical_imports($name, $block_ast) {
my %imports := self.get_imports($name);
unless pir::isnull__IP(%imports) {
for %imports {
$block_ast[0].push(PAST::Var.new(
:name(~$_), :scope('lexical'), :isdecl(1),
:viviself(PAST::Op.new( :pirop('null P')) )
));
$block_ast.symbol(~$_, :scope('lexical'));
}
}
}

method import($name) {
# XXX For now, target is always namespace of the caller. In the
# future we need to be much more smart and handle lexical imports.
# XXX For now, target is always lexpad of the caller. But we may
# have a variety of import descriptors...needs more work later.
my $targetns := Q:PIR {
%r = getinterp
%r = %r['namespace';1]
%r = %r['lexpad';1]
};
# Get imports.
Expand Down

0 comments on commit a11211c

Please sign in to comment.