/
File.pm6
114 lines (99 loc) · 3.31 KB
/
File.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
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
my class CompUnit::PrecompilationUnit::File does CompUnit::PrecompilationUnit {
has CompUnit::PrecompilationId:D $.id is built(:bind) is required;
has IO::Path $.path is built(:bind);
has Str $!checksum is built;
has Str $!source-checksum is built;
has CompUnit::PrecompilationDependency @!dependencies is built(:bind);
has $!bytecode is built(:bind);
has $!store is built(:bind);
has Bool $!initialized;
has IO::Handle $!handle;
has $!update-lock;
submethod TWEAK(--> Nil) {
if $!bytecode {
$!checksum = nqp::sha1($!bytecode.decode('iso-8859-1'));
$!initialized := True;
}
else {
$!initialized := False;
}
$!update-lock := Lock.new;
}
method modified(--> Instant:D) {
$!path.modified
}
method !read-dependencies(--> Nil) {
$!initialized || $!update-lock.protect: {
unless $!initialized { # another thread beat us
$!handle := $!path.open(:r) unless $!handle;
$!checksum = $!handle.get;
$!source-checksum = $!handle.get;
my $dependency := $!handle.get;
my $dependencies := nqp::create(IterationBuffer);
while $dependency {
nqp::push(
$dependencies,
CompUnit::PrecompilationDependency::File.deserialize($dependency)
);
$dependency := $!handle.get;
}
nqp::bindattr(@!dependencies,List,'$!reified',$dependencies);
$!initialized := True;
}
}
}
method dependencies(--> Array[CompUnit::PrecompilationDependency]) {
self!read-dependencies;
@!dependencies
}
method bytecode(--> Buf:D) {
$!update-lock.protect: {
unless $!bytecode {
self!read-dependencies;
$!bytecode := $!handle.slurp(:bin,:close)
}
$!bytecode
}
}
method bytecode-handle(--> IO::Handle:D) {
self!read-dependencies;
$!handle
}
method source-checksum() is rw {
self!read-dependencies;
$!source-checksum
}
method checksum() is rw {
self!read-dependencies;
$!checksum
}
method Str(--> Str:D) {
self.path.Str
}
method close(--> Nil) {
$!update-lock.protect: {
$!handle.close if $!handle;
$!handle := IO::Handle;
$!initialized := False;
}
}
method save-to(IO::Path $precomp-file) {
my $handle := $precomp-file.open(:w);
$handle.print($!checksum ~ "\n");
$handle.print($!source-checksum ~ "\n");
$handle.print($_.serialize ~ "\n") for @!dependencies;
$handle.print("\n");
$handle.write($!bytecode);
$handle.close;
$!path := $precomp-file;
}
method is-up-to-date(
CompUnit::PrecompilationDependency:D $dependency,
Bool :$check-source
--> Bool:D) {
my $result := self.CompUnit::PrecompilationUnit::is-up-to-date($dependency, :$check-source);
$!store.remove-from-cache($.id) unless $result;
$result
}
}
# vim: expandtab shiftwidth=4