6
6
import org .aya .cli .library .source .LibraryOwner ;
7
7
import org .aya .cli .library .source .LibrarySource ;
8
8
import org .aya .cli .utils .CompilerUtil ;
9
- import org .aya .compiler .CompiledModule ;
9
+ import org .aya .compiler .* ;
10
10
import org .aya .resolve .ResolveInfo ;
11
11
import org .aya .resolve .context .EmptyContext ;
12
12
import org .aya .resolve .module .ModuleLoader ;
13
+ import org .aya .syntax .core .def .TopLevelDef ;
13
14
import org .aya .syntax .core .def .TyckDef ;
14
15
import org .aya .syntax .ref .ModulePath ;
15
16
import org .aya .util .FileUtil ;
16
17
import org .aya .util .reporter .Reporter ;
17
18
import org .jetbrains .annotations .NotNull ;
18
19
import org .jetbrains .annotations .Nullable ;
19
20
21
+ import javax .tools .ToolProvider ;
20
22
import java .io .IOException ;
23
+ import java .net .URL ;
24
+ import java .net .URLClassLoader ;
21
25
import java .nio .file .Files ;
22
26
import java .nio .file .Path ;
23
27
@@ -54,7 +58,7 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
54
58
}
55
59
@ Override public @ Nullable ResolveInfo doLoadCompiledCore (
56
60
@ NotNull Reporter reporter ,
57
- @ NotNull ModulePath mod ,
61
+ @ NotNull LibraryOwner owner , @ NotNull ModulePath mod ,
58
62
@ Nullable Path sourcePath ,
59
63
@ Nullable Path corePath ,
60
64
@ NotNull ModuleLoader recurseLoader
@@ -63,10 +67,13 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
63
67
if (!Files .exists (corePath )) return null ;
64
68
65
69
var context = new EmptyContext (reporter , sourcePath ).derive (mod );
66
- // TODO: load defs
67
70
try (var inputStream = FileUtil .ois (corePath )) {
68
71
var compiledAya = (CompiledModule ) inputStream .readObject ();
69
- return compiledAya .toResolveInfo (recurseLoader , context );
72
+ var baseDir = computeBaseDir (owner );
73
+ try (var cl = new URLClassLoader (new URL []{baseDir .toUri ().toURL ()})) {
74
+ cl .loadClass (AbstractSerializer .getModuleReference (mod ));
75
+ return compiledAya .toResolveInfo (recurseLoader , context , cl );
76
+ }
70
77
}
71
78
}
72
79
@@ -75,8 +82,24 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
75
82
@ NotNull ResolveInfo resolveInfo ,
76
83
@ NotNull ImmutableSeq <TyckDef > defs
77
84
) throws IOException {
78
- // TODO: compile defs
85
+ var name = file .moduleName ().last ();
86
+ var javaCode = new FileSerializer (resolveInfo .shapeFactory ())
87
+ .serialize (new FileSerializer .FileResult (file .moduleName ().dropLast (1 ), new ModuleSerializer .ModuleResult (
88
+ name , defs .filterIsInstance (TopLevelDef .class ), ImmutableSeq .empty ())))
89
+ .result ();
90
+ var javaSrcPath = FileUtil .resolveFile (computeBaseDir (file .owner ()).resolve (AyaSerializer .PACKAGE_BASE ),
91
+ file .moduleName ().module (), ".java" );
92
+ FileUtil .writeString (javaSrcPath , javaCode );
93
+ var compiler = ToolProvider .getSystemJavaCompiler ();
94
+ var fileManager = compiler .getStandardFileManager (null , null , null );
95
+ var compilationUnits = fileManager .getJavaFileObjects (javaSrcPath );
96
+ var task = compiler .getTask (null , fileManager , null , null , null , compilationUnits );
97
+ task .call ();
79
98
var coreFile = file .compiledCorePath ();
80
99
CompilerUtil .saveCompiledCore (coreFile , resolveInfo );
81
100
}
101
+
102
+ private static @ NotNull Path computeBaseDir (@ NotNull LibraryOwner owner ) {
103
+ return owner .outDir ().resolve ("compiled" );
104
+ }
82
105
}
0 commit comments