diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java index 8ad6f2c..c23fccc 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI.java @@ -22,11 +22,12 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.Objects; @HostModule("env") class ABI { - private Handler handler; + private final Handler handler; private Memory memory; ExportFunction initializeFn; ExportFunction mainFn; @@ -58,12 +59,8 @@ class ABI { ExportFunction proxyOnQueueReadyFn; ExportFunction proxyOnForeignFunctionFn; - Handler getHandler() { - return handler; - } - - void setHandler(Handler handler) { - this.handler = handler; + ABI(Handler handler) { + this.handler = Objects.requireNonNull(handler); } void setInstance(Instance instance) { diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI_WASI.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI_WASI.java index bd22831..2abc079 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI_WASI.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ABI_WASI.java @@ -11,6 +11,7 @@ import io.roastedroot.proxywasm.LogLevel; import io.roastedroot.proxywasm.WasmException; import java.nio.charset.StandardCharsets; +import java.util.Objects; /** * Implements a restricted subset of the WASI ABI, specifically so that a WASM module can @@ -29,7 +30,7 @@ public class ABI_WASI { private final Handler handler; public ABI_WASI(Handler handler) { - this.handler = handler; + this.handler = Objects.requireNonNull(handler); } //////////////////////////////////////////////////////////////////////////// diff --git a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java index 54e8df9..f6006ae 100644 --- a/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java +++ b/proxy-wasm-java-host/src/main/java/io/roastedroot/proxywasm/internal/ProxyWasm.java @@ -36,19 +36,7 @@ public final class ProxyWasm implements Closeable { private ProxyWasm(Builder other) throws StartException { this.pluginHandler = Objects.requireNonNullElse(other.pluginHandler, new Handler() {}); - this.abi = other.abi; - this.abi.setHandler(createImportsHandler()); - - // initialize/start the vm - if (this.abi.initialize()) { - this.abi.main(0, 0); - } else { - this.abi.start(); - } - - if (other.start) { - start(); - } + this.abi = new ABI(createImportsHandler()); } public Handler getPluginHandler() { @@ -251,13 +239,13 @@ ABI abi() { public static class Builder implements Cloneable { - private final ABI abi = new ABI(); - private Handler pluginHandler; private ImportMemory memory; private boolean start = true; private Function machineFactory; + private Builder() {} + @Override @SuppressWarnings("NoClone") protected Builder clone() { @@ -288,24 +276,16 @@ public ProxyWasm.Builder withMachineFactory(Function machineF return this; } - Builder() {} - - public ProxyWasm build(Instance instance) throws StartException { - abi.setInstance(instance); - return new ProxyWasm(this.clone()); - } - public ProxyWasm build(WasmModule module) throws StartException { - return this.build(Instance.builder(module)); - } - - public ProxyWasm build(Instance.Builder instanceBuilder) throws StartException { - var imports = ImportValues.builder(); - + var instanceBuilder = Instance.builder(module); if (this.machineFactory != null) { instanceBuilder.withMachineFactory(this.machineFactory); } + var proxyWasm = new ProxyWasm(this.clone()); + ABI abi = proxyWasm.abi; + + var imports = ImportValues.builder(); imports.addMemory(Objects.requireNonNullElseGet(memory, this::defaultImportMemory)); imports.addFunction(abi.toHostFunctions()); @@ -315,7 +295,18 @@ public ProxyWasm build(Instance.Builder instanceBuilder) throws StartException { .withImportValues(imports.build()) .build(); - return build(instance); + abi.setInstance(instance); + + if (abi.initialize()) { + abi.main(0, 0); + } else { + abi.start(); + } + if (start) { + proxyWasm.start(); + } + + return proxyWasm; } ImportMemory defaultImportMemory() { diff --git a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java index 4284025..78507ee 100644 --- a/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java +++ b/proxy-wasm-java-host/src/test/java/io/roastedroot/proxywasm/examples/DispatchCallOnTickTest.java @@ -2,8 +2,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; -import com.dylibso.chicory.experimental.aot.AotMachine; -import com.dylibso.chicory.runtime.Instance; +import com.dylibso.chicory.compiler.MachineFactoryCompiler; import com.dylibso.chicory.wasm.Parser; import com.dylibso.chicory.wasm.WasmModule; import io.roastedroot.proxywasm.StartException; @@ -24,9 +23,11 @@ public class DispatchCallOnTickTest { @Test public void testOnTick() throws StartException { var handler = new MockHandler(); - ProxyWasm.Builder builder = ProxyWasm.builder().withPluginHandler(handler); - var instanceBuilder = Instance.builder(module).withMachineFactory(AotMachine::new); - try (var host = builder.build(instanceBuilder)) { + ProxyWasm.Builder builder = + ProxyWasm.builder() + .withPluginHandler(handler) + .withMachineFactory(MachineFactoryCompiler::compile); + try (var host = builder.build(module)) { assertEquals(tickMilliseconds, handler.getTickPeriodMilliseconds()); for (int i = 1; i <= 10; i++) {