|
1 | 1 | package com.dylibso.chicory.build.time.maven; |
2 | 2 |
|
3 | | -import static java.lang.invoke.MethodHandleProxies.asInterfaceInstance; |
4 | | -import static java.lang.invoke.MethodHandles.publicLookup; |
5 | | -import static java.lang.invoke.MethodType.methodType; |
6 | | - |
7 | 3 | import com.dylibso.chicory.build.time.compiler.Config; |
8 | 4 | import com.dylibso.chicory.build.time.compiler.Generator; |
9 | 5 | import com.dylibso.chicory.compiler.InterpreterFallback; |
10 | | -import com.dylibso.chicory.runtime.Stratum; |
11 | | -import com.dylibso.chicory.wasm.WasmModule; |
| 6 | +import com.dylibso.chicory.runtime.DebugParser; |
12 | 7 | import java.io.File; |
13 | 8 | import java.io.IOException; |
| 9 | +import java.util.ServiceConfigurationError; |
| 10 | +import java.util.ServiceLoader; |
14 | 11 | import java.util.Set; |
15 | | -import java.util.function.Function; |
16 | 12 | import org.apache.maven.model.Resource; |
17 | 13 | import org.apache.maven.plugin.AbstractMojo; |
18 | 14 | import org.apache.maven.plugin.MojoExecutionException; |
@@ -84,14 +80,15 @@ public class ChicoryCompilerGenMojo extends AbstractMojo { |
84 | 80 | @Override |
85 | 81 | public void execute() throws MojoExecutionException { |
86 | 82 |
|
87 | | - Function<WasmModule, Stratum> debugParser; |
88 | | - |
| 83 | + DebugParser debugParser = null; |
89 | 84 | // Use the DebugParser if it's on the classpath. |
90 | 85 | try { |
91 | | - debugParser = createDebugParser("com.dylibso.chicory.dwarf.rust.DebugParser", "parse"); |
92 | | - getLog().info("Debug parser enabled"); |
93 | | - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException ignore) { |
94 | | - getLog().info("Debug parser disabled"); |
| 86 | + for (var service : ServiceLoader.load(DebugParser.class)) { |
| 87 | + debugParser = service; |
| 88 | + getLog().info("Using debug parser: " + debugParser.getClass().getName()); |
| 89 | + break; |
| 90 | + } |
| 91 | + } catch (ServiceConfigurationError ignore) { |
95 | 92 | debugParser = null; |
96 | 93 | } |
97 | 94 |
|
@@ -124,16 +121,4 @@ public void execute() throws MojoExecutionException { |
124 | 121 | project.addResource(resource); |
125 | 122 | project.addCompileSourceRoot(targetSourceFolder.getPath()); |
126 | 123 | } |
127 | | - |
128 | | - private Function<WasmModule, Stratum> createDebugParser(String clazzName, String method) |
129 | | - throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException { |
130 | | - var classLoader = ChicoryCompilerGenMojo.class.getClassLoader(); |
131 | | - var clazz = classLoader.loadClass(clazzName); |
132 | | - var handle = |
133 | | - publicLookup() |
134 | | - .findStatic(clazz, method, methodType(Stratum.class, WasmModule.class)); |
135 | | - @SuppressWarnings("unchecked") |
136 | | - Function<WasmModule, Stratum> function = asInterfaceInstance(Function.class, handle); |
137 | | - return function; |
138 | | - } |
139 | 124 | } |
0 commit comments