/
Loader.pm
149 lines (126 loc) Β· 4.92 KB
/
Loader.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# This class contains various bits of logic for implemneting need
# and import.
class Perl6::Module::Loader;
our %LOADED;
method need($name, %name_adverbs?) {
# From another HLL?
if %name_adverbs<from> {
return self.need_foreign($name, %name_adverbs);
}
# Otherwise, use the Perl 6 module locator.
my @inc := pir::get_hll_global__PS('@INC');
my $pm_file := %name_adverbs<ver> || %name_adverbs<auth> ??
Perl6::Module::Locator.find_module($name, @inc, %name_adverbs<ver>, %name_adverbs<auth>) !!
Perl6::Module::Locator.find_module_no_conditions($name, @inc);
if $pm_file eq '' {
pir::say("Unable to find module '$name'" ~
(%name_adverbs<ver> ?? " with version '" ~ %name_adverbs<ver> ~ "'" !! "") ~
(%name_adverbs<ver> && %name_adverbs<auth> ?? ' and' !! '') ~
(%name_adverbs<auth> ?? " with authority '" ~ %name_adverbs<auth> ~ "'" !! "") ~
" in the @*INC directories.");
pir::exit__vI( 1 ); # or another more meaningful nonzero status
}
# Need not load file if we already did so.
unless %LOADED{$pm_file} {
# Is there a pre-compiled PIR version?
my $pir_file := pir::substr__SSII($pm_file, 0, pir::length__IS($pm_file) - 2) ~ 'pir';
my $loaded_pir := 0;
if pir::stat__ISI($pir_file, 0) {
# XXX We really should check if it's newer than the .pm file
# to avoid loading out of date versions.
pir::load_bytecode__vS($pir_file);
$loaded_pir := 1;
}
# If we couldn't load a cached PIR version, read in and compile.
# XXX We can try to write the compiled PIR to disk so the next
# time around it's fast.
unless $loaded_pir {
my $fh := pir::open__PSS($pm_file, 'r');
my $source := $fh.readall();
$fh.close();
my $eval := Perl6::Compiler.compile($source);
$eval();
}
# Mark loaded.
%LOADED{$pm_file} := 1;
}
1;
}
method need_foreign($name, %name_adverbs) {
# If it's a foreign module, we delegate most of the work to the
# other language. However, we still need to install a namespace
# alias in order for import and qualified references to work.
my $lang := %name_adverbs<from>;
my $orig_name := $name; # TODO: Implement :from<lang not.coloned.Name>
pir::load_language__vs($lang);
my $lsm := pir::compreg__ps($lang);
my $mod := $lsm.load_module($orig_name);
my $ns := $lsm.get_namespace($mod);
# Perl6's two phase import mechanism complicates things slightly
# We need to remember the $lsm to we can get at the exports *later*
my $exports_closure := pir::newclosure__PP(sub() {
# TODO: Import flags
my %raw_exports := $lsm.get_exports($mod);
my %exports;
my %subs := %raw_exports<sub> // pir::root_new__PP(<parrot Hash>);
for %subs {
%exports{'&' ~ $_.key} := %subs{$_.key};
}
# TODO: Non-sub exports
%exports;
});
pir::setprop($ns, '!rakudo-export-closure', $exports_closure);
# XXX This alias wants to be lexical, but for now we put it into the
# namespace.
my @nsparts := pir::split__PSS('::', $name);
my $lastpart := @nsparts.pop;
pir::set_hll_global__vPSP(@nsparts, '&' ~ $lastpart, sub() { $ns });
pir::set_hll_global__vPSP(@nsparts, $lastpart, $ns);
return 1;
}
method get_imports($name) {
# Look up default export namespace.
my @nsparts := pir::split__PSS('::', $name);
# If it's an alias to a foreign namespace, we attached an export closure
# to it at need time.
my $ns := pir::get_hll_namespace__PP(@nsparts);
unless pir::isnull($ns) {
my $closure := pir::getprop('!rakudo-export-closure', $ns);
unless pir::isnull($closure) {
return $closure();
}
}
# Otherwise, just go looking in EXPORT::DEFAULT namespace.
# XXX Here is where we need to support custom tags.
@nsparts.push('EXPORT');
@nsparts.push('DEFAULT');
return pir::get_hll_namespace__PP(@nsparts);
}
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($_.key), :scope('lexical'), :isdecl(1),
:viviself(PAST::Op.new( :pirop('null P')) )
));
$block_ast.symbol($_.key, :scope('lexical'));
}
}
}
method import($name) {
# 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['lexpad';1]
};
# Get imports.
my %imports := self.get_imports($name);
unless pir::isnull__IP(%imports) {
for %imports {
$targetns{$_.key} := $_.value;
}
}
1;
}