GraalWasm
A high-performance embeddable WebAssembly runtime for Java
Benefits
Wasm for Java
Load and use WebAssembly modules and functions directly in Java
JavaScript integration
Simplifies use of WebAssembly modules with JavaScript wrappers
100% Java
Written in pure Java with zero native dependencies
How to Get Started
1. Add GraalWasm as a dependency from Maven Central
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>polyglot</artifactId>
<version>24.1.0</version>
</dependency>
<dependency>
<groupId>org.graalvm.polyglot</groupId>
<artifactId>wasm</artifactId>
<version>24.1.0</version>
<type>pom</type>
</dependency>
or
implementation("org.graalvm.polyglot:polyglot:24.1.0")
implementation("org.graalvm.polyglot:wasm:24.1.0")
2. Create a WebAssembly module, for example with wat2wasm
(module
(func (export "addTwo") (param i32 i32) (result i32)
local.get 0
local.get 1
i32.add))
3. Embed the WebAssembly module in Java
import java.net.URL;
import org.graalvm.polyglot.Context;
import org.graalvm.polyglot.Source;
import org.graalvm.polyglot.Value;
try (Context context = Context.create()) {
URL wasmFile = Main.class.getResource("add-two.wasm");
String moduleName = "main";
context.eval(Source.newBuilder("wasm", wasmFile).name(moduleName).build());
Value addTwo = context.getBindings("wasm").getMember(moduleName).getMember("addTwo");
System.out.println("addTwo(40, 2) = " + addTwo.execute(40, 2));
}