Chicory Enables Native WebAssembly Execution on JVM
These articles are AI-generated summaries. Please check the original sources for full details.
Introduction to the Chicory Native JVM WebAssembly Runtime
Chicory, a JVM-native WebAssembly runtime, allows executing .wasm modules without JNI or external dependencies. A JUnit 5 test successfully called the add(2, 40) function from a compiled .wasm module, returning 42.
Why This Matters
WebAssembly modules are inert without a host runtime, requiring imports to be resolved at instantiation. Traditional approaches rely on native runtimes or JNI, which add complexity. Chicory eliminates this by exposing WebAssembly exports as Java APIs, reducing integration overhead and enabling seamless JVM interoperability.
Key Insights
- “Chicory requires Java 11 LTS or later” (source: article)
- “Host functions fulfill imports, enabling JVM-JavaScript interoperability” (example: imports.wasm module)
- “Chicory used by Baeldung to demonstrate JVM WebAssembly integration” (tool + user)
Working Example
@Test
void givenAddModule_whenCallingAddWithTwoAndForty_thenResultIsFortyTwo() {
InputStream wasm = getClass().getResourceAsStream("/chicory/add.wasm");
assertNotNull(wasm);
WasmModule module = Parser.parse(wasm);
Instance instance = Instance.builder(module).build();
ExportFunction add = instance.export("add");
long[] result = add.apply(2, 40);
assertEquals(42, (int) result[0]);
}
@Test
void givenImportDouble_whenCallingUseDouble_thenResultIsDoubled() {
InputStream wasm = getClass().getResourceAsStream("/chicory/imports.wasm");
assertNotNull(wasm);
HostFunction doubleFn = new HostFunction(
"host", "double", FunctionType.of(List.of(ValType.I32), List.of(ValType.I32)),
(Instance instance, long... args) -> new long[] { args[0] * 2 }
);
Store store = new Store();
store.addFunction(doubleFn);
WasmModule module = Parser.parse(wasm);
Instance instance = store.instantiate("imports", module);
ExportFunction useDouble = instance.export("useDouble");
long[] result = useDouble.apply(21);
assertEquals(42L, result[0]);
}
Practical Applications
- Use Case: Java applications integrating WebAssembly modules for cross-platform computation (e.g., add.wasm for arithmetic).
- Pitfall: Unresolved imports during instantiation or missing export names at call time cause runtime exceptions.
References:
Continue reading
Next article
Google Antigravity IDE: Agentic Coding Environment
Related Content
JUnit 6.0.0 Released with Java 17 Baseline, Kotlin Suspend Support, and Enhanced Features
JUnit 6.0.0 introduces significant improvements including Java 17 baseline, native Kotlin suspend test support, a new CancellationToken API for fail-fast execution, built-in Java Flight Recorder (JFR) listeners, and upgraded CSV parsing with FastCSV. The deprecated JUnit 4 runner (junit-platform-runner) is removed, with Vintage remaining as a temporary bridge.
Find Out What Keystore the JVM Is Using
Locate the JVM’s default keystore (cacerts) using JAVA_HOME, system properties, or user-specific paths.
Wildcard Search in Elasticsearch: Techniques and Java Implementation
Master wildcard queries in Elasticsearch using Java with code examples for prefix, regexp, and fuzzy searches.