GraalWasm is the GraalVM implementation of the WebAssembly programming language. It can interpret and compile WebAssembly programs in the binary format or be embedded into other programs.

Note, GraalWasm is currently an experimental feature.

GraalWasm is shipped as a separate installable and can be installed using the GraalVM Updater tool. See gu --help for more information. The wasm launcher which runs WebAssembly modules becomes available.

Running WebAssembly Programs

You can run WebAssembly programs in the binary format, using the wasm launcher. For example, assuming that the following C program was compiled with Emscripten:

#include <stdio.h>

int main() {
  int number = 1;
  int rows = 10;
  for (int i = 1; i <= rows; i++) {
    for (int j = 1; j <= i; j++) {
      printf("%d ", number);
      ++number;
    }
    printf(".\n");
  }
  return 0;
}

You can run the compiled WebAssembly binary as follows:

graalvm/bin/wasm --Builtins=memory,env:emscripten floyd.wasm

In this example, the flag --Builtins specifies builtin modules that the Emscripten toolchain requires.

Embedding WebAssembly Programs

GraalWasm can be accessed programmatically with the Polyglot API, which allows embedding GraalWasm into user programs. Here is a simple example of how to run a WebAssembly program using GraalWasm from a Java application:

import org.graalvm.polyglot.*;
import org.graalvm.polyglot.io.ByteSequence;

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();

context.eval(source);

Value mainFunction = context.getBindings("wasm").getMember("_main");
mainFunction.execute();

For more polyglot examples, consult the reference documentation.