Skip to content

Commit 09b6a0e

Browse files
committed
jit: use javac API
1 parent 1edefb4 commit 09b6a0e

File tree

3 files changed

+34
-30
lines changed

3 files changed

+34
-30
lines changed

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
22
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33

4-
import com.javax0.sourcebuddy.Compiler;
54
import kala.collection.immutable.ImmutableSeq;
65
import org.aya.compiler.FileSerializer;
76
import org.aya.compiler.ModuleSerializer;
@@ -28,6 +27,7 @@
2827
import org.jetbrains.annotations.NotNull;
2928
import org.junit.jupiter.api.Test;
3029

30+
import java.io.IOException;
3131
import java.nio.file.Path;
3232

3333
public class CompileTest {
@@ -37,19 +37,18 @@ public class CompileTest {
3737
open data Vec (n : Nat) Type
3838
| O, A => vnil
3939
| S n, A => vcons A (Vec n A)
40-
40+
4141
def plus (a b : Nat) : Nat elim a
4242
| O => b
4343
| S n => S (plus n b)
4444
"""); // .filter(x -> x instanceof FnDef || x instanceof DataDef);
4545

4646
var code = serializeFrom(result);
4747

48-
System.out.println(code);
49-
5048
try {
51-
var clazz = Compiler.java().from("AYA.baka", code).compile().load().get();
52-
var loader = clazz.getClassLoader();
49+
var tester = new CompileTester(code);
50+
tester.compile();
51+
var loader = tester.cl;
5352

5453
var fieldO = loader.loadClass("AYA.baka$Nat$O").getField("INSTANCE");
5554
var fieldS = loader.loadClass("AYA.baka$Nat$S").getField("INSTANCE");
@@ -67,7 +66,7 @@ def plus (a b : Nat) : Nat elim a
6766

6867
var mResult = plus.invoke(zero, ImmutableSeq.of(two, three));
6968
System.out.println(mResult.debuggerOnlyToString());
70-
} catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | Compiler.CompileException e) {
69+
} catch (ClassNotFoundException | IllegalAccessException | NoSuchFieldException | IOException e) {
7170
throw new RuntimeException(e);
7271
}
7372

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

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,54 @@
11
// Copyright (c) 2020-2024 Tesla (Yinsen) Zhang.
22
// Use of this source code is governed by the MIT license that can be found in the LICENSE.md file.
33

4-
import com.javax0.sourcebuddy.Compiler;
5-
import com.javax0.sourcebuddy.Fluent;
64
import kala.collection.immutable.ImmutableSeq;
75
import org.aya.compiler.AyaSerializer;
8-
import org.aya.compiler.ModuleSerializer;
9-
import org.aya.generic.NameGenerator;
10-
import org.aya.primitive.ShapeFactory;
116
import org.aya.resolve.module.DumbModuleLoader;
127
import org.aya.syntax.compile.JitDef;
13-
import org.aya.syntax.core.def.DataDef;
14-
import org.aya.syntax.core.def.FnDef;
15-
import org.aya.syntax.core.def.TyckDef;
16-
import org.intellij.lang.annotations.Language;
178
import org.jetbrains.annotations.NotNull;
189

10+
import javax.tools.ToolProvider;
11+
import java.io.IOException;
1912
import java.lang.reflect.Field;
13+
import java.net.URL;
14+
import java.net.URLClassLoader;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.Paths;
2018

2119
public class CompileTester {
22-
public final @Language("Java") @NotNull String code;
23-
private final Fluent.AddSource compiler = Compiler.java();
24-
private Class<?> output = null;
20+
public final @NotNull String code;
21+
private final Path baka;
22+
public final ClassLoader cl;
2523

26-
public CompileTester(@NotNull String code) { this.code = code; }
24+
public CompileTester(@NotNull String code) throws IOException {
25+
this.code = code;
26+
27+
var genDir = Paths.get("src/test/gen");
28+
Files.createDirectories(genDir);
29+
Files.writeString(baka = genDir.resolve("baka.java"), code);
30+
cl = new URLClassLoader(new URL[]{baka.toUri().toURL()});
31+
}
2732

2833
public void compile() {
2934
try {
30-
output = compiler.from(STR."\{AyaSerializer.PACKAGE_BASE}.\{DumbModuleLoader.DUMB_MODULE_NAME}", code)
31-
.compile().load().get();
32-
} catch (ClassNotFoundException | Compiler.CompileException e) {
35+
var compiler = ToolProvider.getSystemJavaCompiler();
36+
var fileManager = compiler.getStandardFileManager(null, null, null);
37+
var compilationUnits = fileManager.getJavaFileObjects(baka);
38+
var task = compiler.getTask(null, fileManager, null, null, null, compilationUnits);
39+
task.call();
40+
var fqName = STR."\{AyaSerializer.PACKAGE_BASE}.\{DumbModuleLoader.DUMB_MODULE_NAME}";
41+
cl.loadClass(fqName);
42+
} catch (ClassNotFoundException e) {
3343
throw new RuntimeException(e);
3444
}
3545
}
3646

3747
@SuppressWarnings("unchecked")
3848
public <T> @NotNull Class<T> load(String... qualified) {
3949
try {
40-
return (Class<T>) output.getClassLoader()
41-
.loadClass(STR."\{AyaSerializer.PACKAGE_BASE}.\{ImmutableSeq.from(qualified).joinToString("$")}");
50+
return (Class<T>) cl.loadClass(
51+
STR."\{AyaSerializer.PACKAGE_BASE}.\{ImmutableSeq.from(qualified).joinToString("$")}");
4252
} catch (ClassNotFoundException e) {
4353
throw new RuntimeException(e);
4454
}

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

-5
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515
import org.junit.jupiter.api.Test;
1616

1717
import java.io.IOException;
18-
import java.nio.file.Files;
19-
import java.nio.file.Paths;
2018
import java.util.Random;
2119
import java.util.function.Function;
2220
import java.util.function.IntFunction;
@@ -33,9 +31,6 @@ public class RedBlackTreeTest {
3331
var result = CompileTest.tyck(code);
3432

3533
var tester = new CompileTester(CompileTest.serializeFrom(result));
36-
var genDir = Paths.get("src/test/gen");
37-
Files.createDirectories(genDir);
38-
Files.writeString(genDir.resolve("baka.java"), tester.code);
3934
tester.compile();
4035

4136
JitData List = tester.loadInstance("baka", "List");

0 commit comments

Comments
 (0)