From b5178801339d59e9759a1373f75fa5beacdd3162 Mon Sep 17 00:00:00 2001 From: andreatp Date: Tue, 7 Oct 2025 12:15:24 +0100 Subject: [PATCH 1/2] demo: switch to compile time chicory compiler --- .gitignore | 2 +- wasm/java-host/pom.xml | 30 +++++++++++++++++-- .../confidence/wasmresolvepoc/Main.java | 4 +-- .../wasmresolvepoc/ResolverApi.java | 6 ++-- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 1c75a50..26a4e85 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ target/ wasm/rust_guest.wasm - +wasm/confidence_resolver.wasm diff --git a/wasm/java-host/pom.xml b/wasm/java-host/pom.xml index 71b00c0..31d75c9 100644 --- a/wasm/java-host/pom.xml +++ b/wasm/java-host/pom.xml @@ -16,6 +16,18 @@ 0.6.1 + + + + com.dylibso.chicory + bom + 1.5.2 + pom + import + + + + @@ -26,12 +38,10 @@ com.dylibso.chicory runtime - 1.4.0 com.dylibso.chicory compiler - 1.4.0 @@ -77,6 +87,22 @@ versions-maven-plugin 2.16.2 + + com.dylibso.chicory + chicory-compiler-maven-plugin + + + wast2json + + compile + + + com.spotify.confidence.wasmresolvepoc.ConfidenceResolver + ${project.basedir}/../confidence_resolver.wasm + + + + diff --git a/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/Main.java b/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/Main.java index defc35e..aa33bed 100644 --- a/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/Main.java +++ b/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/Main.java @@ -19,9 +19,7 @@ public class Main { private final ResolverApi resolverApi; public Main() { - Path wasmModulePath = Path.of("../confidence_resolver.wasm"); - WasmModule module = Parser.parse(wasmModulePath); - resolverApi = new ResolverApi(module); + resolverApi = new ResolverApi(); } diff --git a/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java b/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java index c67a359..b552034 100644 --- a/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java +++ b/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java @@ -39,16 +39,16 @@ public class ResolverApi { private final ExportFunction wasmMsgGuestResolve; private final ExportFunction wasmMsgGuestResolveSimple; - public ResolverApi(WasmModule module) { + public ResolverApi() { - instance = Instance.builder(module) + instance = Instance.builder(ConfidenceResolver.load()) .withImportValues(ImportValues.builder() .addFunction(createImportFunction("current_time", Messages.Void::parseFrom, this::currentTime)) .addFunction(createImportFunction("log_resolve", Types.LogResolveRequest::parseFrom, this::logResolve)) .addFunction(createImportFunction("log_assign", Types.LogAssignRequest::parseFrom, this::logAssign)) .addFunction(new ImportFunction("wasm_msg", "wasm_msg_current_thread_id", FunctionType.of(List.of(), List.of(ValType.I32)), (instance1, args) -> new long[]{0})) .build()) - .withMachineFactory(MachineFactoryCompiler::compile) + .withMachineFactory(ConfidenceResolver::create) .build(); wasmMsgAlloc = instance.export("wasm_msg_alloc"); wasmMsgFree = instance.export("wasm_msg_free"); From 17b0bdcb12b213231093f69ec16338f7958ce06d Mon Sep 17 00:00:00 2001 From: andreatp Date: Tue, 7 Oct 2025 12:31:02 +0100 Subject: [PATCH 2/2] use WasmModuleInterface --- wasm/java-host/pom.xml | 29 +++++++- .../wasmresolvepoc/WasmResource.java | 7 ++ .../wasmresolvepoc/ResolverApi.java | 70 ++++++------------- 3 files changed, 57 insertions(+), 49 deletions(-) create mode 100644 wasm/java-host/src/main/java-templates/com/spotify/confidence/wasmresolvepoc/WasmResource.java diff --git a/wasm/java-host/pom.xml b/wasm/java-host/pom.xml index 31d75c9..4df56f9 100644 --- a/wasm/java-host/pom.xml +++ b/wasm/java-host/pom.xml @@ -41,7 +41,7 @@ com.dylibso.chicory - compiler + annotations @@ -87,12 +87,37 @@ versions-maven-plugin 2.16.2 + + org.codehaus.mojo + templating-maven-plugin + + + filtering-java-templates + + filter-sources + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + true + + + com.dylibso.chicory + annotations-processor + + + + com.dylibso.chicory chicory-compiler-maven-plugin - wast2json + confidence-resolver compile diff --git a/wasm/java-host/src/main/java-templates/com/spotify/confidence/wasmresolvepoc/WasmResource.java b/wasm/java-host/src/main/java-templates/com/spotify/confidence/wasmresolvepoc/WasmResource.java new file mode 100644 index 0000000..5fb3a86 --- /dev/null +++ b/wasm/java-host/src/main/java-templates/com/spotify/confidence/wasmresolvepoc/WasmResource.java @@ -0,0 +1,7 @@ +package com.spotify.confidence.wasmresolvepoc; + +public final class WasmResource { + public static final String absoluteFile = "file://${project.basedir}/../confidence_resolver.wasm"; + + private WasmResource() {} +} diff --git a/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java b/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java index b552034..2c9b436 100644 --- a/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java +++ b/wasm/java-host/src/main/java/com/spotify/confidence/wasmresolvepoc/ResolverApi.java @@ -1,6 +1,6 @@ package com.spotify.confidence.wasmresolvepoc; -import com.dylibso.chicory.compiler.MachineFactoryCompiler; +import com.dylibso.chicory.annotations.WasmModuleInterface; import com.dylibso.chicory.runtime.ExportFunction; import com.dylibso.chicory.runtime.ImportFunction; import com.dylibso.chicory.runtime.ImportValues; @@ -25,45 +25,19 @@ import java.util.List; import java.util.function.Function; -public class ResolverApi { +@WasmModuleInterface(WasmResource.absoluteFile) +public class ResolverApi implements ResolverApi_ModuleImports, ResolverApi_WasmMsg { private static final FunctionType HOST_FN_TYPE = FunctionType.of(List.of(ValType.I32), List.of(ValType.I32)); private final Instance instance; - - // interop - private final ExportFunction wasmMsgAlloc; - private final ExportFunction wasmMsgFree; - - // api - private final ExportFunction wasmMsgGuestSetResolverState; - private final ExportFunction wasmMsgGuestResolve; - private final ExportFunction wasmMsgGuestResolveSimple; + private final ResolverApi_ModuleExports exports; public ResolverApi() { - instance = Instance.builder(ConfidenceResolver.load()) - .withImportValues(ImportValues.builder() - .addFunction(createImportFunction("current_time", Messages.Void::parseFrom, this::currentTime)) - .addFunction(createImportFunction("log_resolve", Types.LogResolveRequest::parseFrom, this::logResolve)) - .addFunction(createImportFunction("log_assign", Types.LogAssignRequest::parseFrom, this::logAssign)) - .addFunction(new ImportFunction("wasm_msg", "wasm_msg_current_thread_id", FunctionType.of(List.of(), List.of(ValType.I32)), (instance1, args) -> new long[]{0})) - .build()) + .withImportValues(this.toImportValues()) .withMachineFactory(ConfidenceResolver::create) .build(); - wasmMsgAlloc = instance.export("wasm_msg_alloc"); - wasmMsgFree = instance.export("wasm_msg_free"); - wasmMsgGuestSetResolverState = instance.export("wasm_msg_guest_set_resolver_state"); - wasmMsgGuestResolve = instance.export("wasm_msg_guest_resolve"); - wasmMsgGuestResolveSimple = instance.export("wasm_msg_guest_resolve_simple"); - } - - private GeneratedMessage logAssign(Types.LogAssignRequest logAssignRequest) { - System.out.println("logAssign"); - return Messages.Void.getDefaultInstance(); - } - - private GeneratedMessage logResolve(Types.LogResolveRequest logResolveRequest) { - return Messages.Void.getDefaultInstance(); + exports = new ResolverApi_ModuleExports(instance); } private Timestamp currentTime(Messages.Void unused) { @@ -75,19 +49,19 @@ public void setResolverState(SetResolverStateRequest state) { .setData(state.toByteString()) .build().toByteArray(); int addr = transfer(request); - int respPtr = (int) wasmMsgGuestSetResolverState.apply(addr)[0]; + int respPtr = exports.wasmMsgGuestSetResolverState(addr); consumeResponse(respPtr, Messages.Void::parseFrom); } public ResolveFlagsResponse resolve(ResolveFlagsRequest request) { int reqPtr = transferRequest(request); - int respPtr = (int) wasmMsgGuestResolve.apply(reqPtr)[0]; + int respPtr = exports.wasmMsgGuestResolve(reqPtr); return consumeResponse(respPtr, ResolveFlagsResponse::parseFrom); } public ResolvedFlag resolve_simple(ResolveSimpleRequest request) { int reqPtr = transferRequest(request); - int respPtr = (int) wasmMsgGuestResolveSimple.apply(reqPtr)[0]; + int respPtr = exports.wasmMsgGuestResolveSimple(reqPtr); return consumeResponse(respPtr, ResolvedFlag::parseFrom); } @@ -138,27 +112,29 @@ private byte[] consume(int addr) { final Memory mem = instance.memory(); final int len = (int) (mem.readU32(addr - 4) - 4L); final byte[] data = mem.readBytes(addr, len); - wasmMsgFree.apply(addr); + exports.wasmMsgFree(addr); return data; } private int transfer(byte[] data) { final Memory mem = instance.memory(); - int addr = (int) wasmMsgAlloc.apply(data.length)[0]; + int addr = exports.wasmMsgAlloc(data.length); mem.write(addr, data); return addr; } - private ImportFunction createImportFunction(String name, ParserFn reqCodec, Function impl) { - return new ImportFunction("wasm_msg", "wasm_msg_host_" + name, HOST_FN_TYPE, (instance1, args) -> { - try { - final T message = consumeRequest((int) args[0], reqCodec); - final GeneratedMessage response = impl.apply(message); - return new long[]{transferResponseSuccess(response)}; - } catch (Exception e) { - return new long[]{transferResponseError(e.getMessage())}; - } - }); + @Override + public ResolverApi_WasmMsg wasmMsg() { + return this; + } + + @Override + public int wasmMsgHostCurrentTime(int arg0) { + try { + return transferResponseSuccess(currentTime(Messages.Void.getDefaultInstance())); + } catch (Exception e) { + return transferResponseError(e.getMessage()); + } } private interface ParserFn {