-
-
Notifications
You must be signed in to change notification settings - Fork 372
/
Loader.pm6
67 lines (57 loc) · 2.31 KB
/
Loader.pm6
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
class CompUnit::Loader is repr('Uninstantiable') {
# Load a file from source and compile it
method load-source-file(IO::Path $path --> CompUnit::Handle) {
# Get the compiler and compile the code, then run it
# (which runs the mainline and captures UNIT).
my $?FILES := $path.Str;
self.load-source($path.slurp(:bin))
}
# Decode the specified byte buffer as source code, and compile it
method load-source(Blob:D $bytes --> CompUnit::Handle:D) {
my $preserve_global := nqp::ifnull(nqp::gethllsym('Raku', 'GLOBAL'), Mu);
my $handle := CompUnit::Handle.new;
my $*CTXSAVE := $handle;
my $eval := nqp::getcomp('Raku').compile($bytes.decode);
$eval();
nqp::bindhllsym('Raku', 'GLOBAL', $preserve_global);
CATCH {
default {
nqp::bindhllsym('Raku', 'GLOBAL', $preserve_global);
.throw;
}
}
$handle
}
# Load a pre-compiled file
proto method load-precompilation-file(|) {*}
multi method load-precompilation-file(IO::Path $path --> CompUnit::Handle:D) {
my $handle := CompUnit::Handle.new;
my $*CTXSAVE := $handle;
# '%?OPTIONS' is expected by some code; mainly by the World object
my %*COMPILING := nqp::hash('%?OPTIONS', nqp::hash());
nqp::loadbytecode($path.Str);
$handle
}
multi method load-precompilation-file(IO::Handle $file --> CompUnit::Handle:D) {
my $handle := CompUnit::Handle.new;
my $*CTXSAVE := $handle;
my %*COMPILING := nqp::hash('%?OPTIONS', nqp::hash());
#?if !jvm
# Switch file handle to binary mode before passing it off to the VM,
# so we don't lose things hanging around in the decoder.
$file.encoding(Nil);
nqp::loadbytecodefh(nqp::getattr($file, IO::Handle, '$!PIO'), $file.path.Str);
#?endif
$handle
}
# Load the specified byte buffer as if it was the contents of a
# precompiled file
method load-precompilation(Blob:D $bytes --> CompUnit::Handle:D) {
my $handle := CompUnit::Handle.new;
my $*CTXSAVE := $handle;
my %*COMPILING := nqp::hash('%?OPTIONS', nqp::hash());
nqp::loadbytecodebuffer($bytes);
$handle
}
}
# vim: expandtab shiftwidth=4