Skip to content

Commit 6e52895

Browse files
committed
jit: call javac in DiskCompilerAdvisor
1 parent 707c9c1 commit 6e52895

File tree

8 files changed

+34
-24
lines changed

8 files changed

+34
-24
lines changed

Diff for: cli-impl/src/main/java/module-info.java

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
requires aya.compiler;
1010
requires org.jetbrains.annotations;
1111
requires org.commonmark;
12+
requires java.compiler;
1213

1314
exports org.aya.cli.interactive;
1415
exports org.aya.cli.library.incremental;

Diff for: cli-impl/src/main/java/org/aya/cli/library/LibraryModuleLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public boolean existsFileLevelModule(@NotNull ModulePath path) {
9090
@NotNull ModulePath mod, @Nullable Path sourcePath,
9191
@Nullable Path corePath, @NotNull ModuleLoader recurseLoader
9292
) {
93-
return advisor.loadCompiledCore(reporter, mod, sourcePath, corePath, recurseLoader);
93+
return advisor.loadCompiledCore(reporter, owner, mod, sourcePath, corePath, recurseLoader);
9494
}
9595

9696
private void saveCompiledCore(

Diff for: cli-impl/src/main/java/org/aya/cli/library/incremental/CompilerAdvisor.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ default void notifyIncrementalJob(
6363
*/
6464
@Nullable ResolveInfo doLoadCompiledCore(
6565
@NotNull Reporter reporter,
66-
@NotNull ModulePath mod,
66+
@NotNull LibraryOwner owner, @NotNull ModulePath mod,
6767
@Nullable Path sourcePath,
6868
@Nullable Path corePath,
6969
@NotNull ModuleLoader recurseLoader
@@ -78,14 +78,14 @@ void doSaveCompiledCore(
7878
@ApiStatus.NonExtendable
7979
default @Nullable ResolveInfo loadCompiledCore(
8080
@NotNull Reporter reporter,
81-
@NotNull ModulePath mod,
81+
@NotNull LibraryOwner owner, @NotNull ModulePath mod,
8282
@Nullable Path sourcePath,
8383
@Nullable Path corePath,
8484
@NotNull ModuleLoader recurseLoader
8585
) {
8686
assert recurseLoader instanceof CachedModuleLoader<?>;
8787
try {
88-
return doLoadCompiledCore(reporter, mod, sourcePath, corePath, recurseLoader);
88+
return doLoadCompiledCore(reporter, owner, mod, sourcePath, corePath, recurseLoader);
8989
} catch (IOException | ClassNotFoundException e) {
9090
throw new Panic("Compiled aya found but cannot be loaded", e);
9191
}

Diff for: cli-impl/src/main/java/org/aya/cli/library/incremental/DelegateCompilerAdvisor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public void notifyIncrementalJob(@NotNull ImmutableSeq<LibrarySource> modified,
5252
}
5353

5454
@Override public @Nullable ResolveInfo
55-
doLoadCompiledCore(@NotNull Reporter reporter, @NotNull ModulePath mod, @Nullable Path sourcePath, @Nullable Path corePath, @NotNull ModuleLoader recurseLoader) throws IOException, ClassNotFoundException {
56-
return delegate.doLoadCompiledCore(reporter, mod, sourcePath, corePath, recurseLoader);
55+
doLoadCompiledCore(@NotNull Reporter reporter, @NotNull LibraryOwner owner, @NotNull ModulePath mod, @Nullable Path sourcePath, @Nullable Path corePath, @NotNull ModuleLoader recurseLoader) throws IOException, ClassNotFoundException {
56+
return delegate.doLoadCompiledCore(reporter, owner, mod, sourcePath, corePath, recurseLoader);
5757
}
5858

5959
@Override public void

Diff for: cli-impl/src/main/java/org/aya/cli/library/incremental/DiskCompilerAdvisor.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import org.aya.cli.library.source.LibraryOwner;
77
import org.aya.cli.library.source.LibrarySource;
88
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.*;
1210
import org.aya.resolve.ResolveInfo;
1311
import org.aya.resolve.context.EmptyContext;
1412
import org.aya.resolve.module.ModuleLoader;
@@ -20,7 +18,10 @@
2018
import org.jetbrains.annotations.NotNull;
2119
import org.jetbrains.annotations.Nullable;
2220

21+
import javax.tools.ToolProvider;
2322
import java.io.IOException;
23+
import java.net.URL;
24+
import java.net.URLClassLoader;
2425
import java.nio.file.Files;
2526
import java.nio.file.Path;
2627

@@ -57,7 +58,7 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
5758
}
5859
@Override public @Nullable ResolveInfo doLoadCompiledCore(
5960
@NotNull Reporter reporter,
60-
@NotNull ModulePath mod,
61+
@NotNull LibraryOwner owner, @NotNull ModulePath mod,
6162
@Nullable Path sourcePath,
6263
@Nullable Path corePath,
6364
@NotNull ModuleLoader recurseLoader
@@ -66,10 +67,13 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
6667
if (!Files.exists(corePath)) return null;
6768

6869
var context = new EmptyContext(reporter, sourcePath).derive(mod);
69-
// TODO: load defs
7070
try (var inputStream = FileUtil.ois(corePath)) {
7171
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+
}
7377
}
7478
}
7579

@@ -83,13 +87,19 @@ public class DiskCompilerAdvisor implements CompilerAdvisor {
8387
.serialize(new FileSerializer.FileResult(file.moduleName().dropLast(1), new ModuleSerializer.ModuleResult(
8488
name, defs.filterIsInstance(TopLevelDef.class), ImmutableSeq.empty())))
8589
.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();
8698
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-
// }
9399
CompilerUtil.saveCompiledCore(coreFile, resolveInfo);
94100
}
101+
102+
private static @NotNull Path computeBaseDir(@NotNull LibraryOwner owner) {
103+
return owner.outDir().resolve("compiled");
104+
}
95105
}

Diff for: cli-impl/src/main/java/org/aya/cli/library/incremental/InMemoryCompilerAdvisor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public class InMemoryCompilerAdvisor implements CompilerAdvisor {
6363
@Override
6464
public @Nullable ResolveInfo doLoadCompiledCore(
6565
@NotNull Reporter reporter,
66-
@NotNull ModulePath mod,
66+
@NotNull LibraryOwner owner, @NotNull ModulePath mod,
6767
@Nullable Path sourcePath,
6868
@Nullable Path corePath,
6969
@NotNull ModuleLoader recurseLoader

Diff for: jit-compiler/src/main/java/org/aya/compiler/CompiledModule.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ private void serOp(@NotNull TyckDef def) {
166166
}
167167

168168
public @NotNull ResolveInfo toResolveInfo(
169-
@NotNull ModuleLoader loader, @NotNull PhysicalModuleContext context
169+
@NotNull ModuleLoader loader, @NotNull PhysicalModuleContext context, ClassLoader classLoader
170170
) {
171-
var state = new DeState(getClass().getClassLoader());
171+
var state = new DeState(classLoader);
172172
return toResolveInfo(loader, context, state, new PrimFactory(), new ShapeFactory());
173173
}
174174
public @NotNull ResolveInfo toResolveInfo(

Diff for: jit-compiler/src/test/java/CompileTester.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
import org.aya.compiler.AyaSerializer;
66
import org.aya.resolve.module.DumbModuleLoader;
77
import org.aya.syntax.compile.JitDef;
8+
import org.aya.util.FileUtil;
89
import org.jetbrains.annotations.NotNull;
910

1011
import javax.tools.ToolProvider;
1112
import java.io.IOException;
1213
import java.lang.reflect.Field;
1314
import java.net.URL;
1415
import java.net.URLClassLoader;
15-
import java.nio.file.Files;
1616
import java.nio.file.Path;
1717
import java.nio.file.Paths;
1818

@@ -23,8 +23,7 @@ public class CompileTester {
2323
public CompileTester(@NotNull String code) throws IOException {
2424
var root = Paths.get("build/tmp/testGenerated");
2525
var genDir = root.resolve(AyaSerializer.PACKAGE_BASE);
26-
Files.createDirectories(genDir);
27-
Files.writeString(baka = genDir.resolve("baka.java"), code);
26+
FileUtil.writeString(baka = genDir.resolve("baka.java"), code);
2827
cl = new URLClassLoader(new URL[]{root.toUri().toURL()});
2928
}
3029

0 commit comments

Comments
 (0)