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 ;
10
- import org .aya .compiler .FileSerializer ;
11
- import org .aya .compiler .ModuleSerializer ;
9
+ import org .aya .compiler .*;
12
10
import org .aya .resolve .ResolveInfo ;
13
11
import org .aya .resolve .context .EmptyContext ;
14
12
import org .aya .resolve .module .ModuleLoader ;
20
18
import org .jetbrains .annotations .NotNull ;
21
19
import org .jetbrains .annotations .Nullable ;
22
20
21
+ import javax .tools .ToolProvider ;
23
22
import java .io .IOException ;
23
+ import java .net .URL ;
24
+ import java .net .URLClassLoader ;
24
25
import java .nio .file .Files ;
25
26
import java .nio .file .Path ;
26
27
@@ -57,7 +58,7 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
57
58
}
58
59
@ Override public @ Nullable ResolveInfo doLoadCompiledCore (
59
60
@ NotNull Reporter reporter ,
60
- @ NotNull ModulePath mod ,
61
+ @ NotNull LibraryOwner owner , @ NotNull ModulePath mod ,
61
62
@ Nullable Path sourcePath ,
62
63
@ Nullable Path corePath ,
63
64
@ NotNull ModuleLoader recurseLoader
@@ -66,10 +67,13 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
66
67
if (!Files .exists (corePath )) return null ;
67
68
68
69
var context = new EmptyContext (reporter , sourcePath ).derive (mod );
69
- // TODO: load defs
70
70
try (var inputStream = FileUtil .ois (corePath )) {
71
71
var compiledAya = (CompiledModule ) inputStream .readObject ();
72
- 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
+ }
73
77
}
74
78
}
75
79
@@ -83,13 +87,19 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
83
87
.serialize (new FileSerializer .FileResult (file .moduleName ().dropLast (1 ), new ModuleSerializer .ModuleResult (
84
88
name , defs .filterIsInstance (TopLevelDef .class ), ImmutableSeq .empty ())))
85
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 ();
86
98
var coreFile = file .compiledCorePath ();
87
- // try {
88
- // var compiled = Compiler.java().from(STR."\{AyaSerializer.PACKAGE_BASE}.\{name}", javaCode).compile();
89
- // compiled.saveTo(coreFile.resolveSibling(STR."\{name}.compiled"));
90
- // } catch (Compiler.CompileException e) {
91
- // throw new Panic(e);
92
- // }
93
99
CompilerUtil .saveCompiledCore (coreFile , resolveInfo );
94
100
}
101
+
102
+ private static @ NotNull Path computeBaseDir (@ NotNull LibraryOwner owner ) {
103
+ return owner .outDir ().resolve ("compiled" );
104
+ }
95
105
}
0 commit comments