WebAssembly engine in GraalVM: Introducing Oracle’s GraalWasm
Oracle has been working on implementing a Wasm engine in GraalVM, and the result is GraalWasm. The project is still in its early stages and marked as experimental. Let’s see what Oracle has done so far, what the anticipated benefits are, and what’s planned for the future.
Oracle announced that it has been working on GraalWasm, a WebAssembly engine implemented in GraalVM. Now GraalWasm has been made publicly available, though still in an experimental state.
WebAssembly is a low-level binary format for the web that other high-level languages compile to in order for them to run in the browser. While C, Rust and C++ compile natively to WebAssembly, C# is supported via Microsoft’s SPA framework Blazor WebAssembly, currently still in preview state.
GraalVM as a universal platform?
By supporting WebAssembly, or Wasm, the set of GraalVM languages can be broadened, thus bringing it further to a “universal platform for programming language execution,” as Oracle’s Aleksandar Prokopec states in a blog post.
How to get started
GraalWasm can be used to interpret and compile Wasm programs in binary format. Just follow the standard workflow for Graal projects for building GraalWasm.
Additionally, GraalWasm can be embedded into other programs, as it can be accessed programmatically with the Polyglot API.
In this example, a WebAssembly program is run using GraalWasm from a Java application:
import org.graalvm.polyglot.*; import org.graalvm.polyglot.io.ByteSequence; &nbsp; byte binary = readBytes("example.wasm"); // You need to load the .wasm contents into a byte array. Context.Builder contextBuilder = Context.newBuilder("wasm"); Source.Builder sourceBuilder = Source.newBuilder("wasm", ByteSequence.create(binary), "example"); Source source = sourceBuilder.build(); Context context = contextBuilder.build(); &nbsp; context.eval(source); &nbsp; Value mainFunction = context.getBindings("wasm").getMember("_main"); mainFunction.execute();
SEE ALSO: GraalVM 19.3 brings support for JDK 11
Some future plans for GraalWasm include performance improvements and the implementation of WASI (WebAssembly System Interface), a set of APIs that enable WebAssembly programs to run outside the web.
Read more about GraalWasm on GitHub, where Oracle is open for suggestions from the community on how to further improve it.