Skip to content

Instantly share code, notes, and snippets.

@Hywan
Last active May 12, 2020 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hywan/6ffb1a7ff59c9df9ff5dc760692b21cf to your computer and use it in GitHub Desktop.
Save Hywan/6ffb1a7ff59c9df9ff5dc760692b21cf to your computer and use it in GitHub Desktop.
Execute a WebAssembly module from Java
import org.wasmer.Instance;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
class SimpleExample {
public static void main(String[] args) throws IOException {
// Read the WebAssembly bytes.
byte[] bytes = Files.readAllBytes(Paths.get("simple.wasm"));
// Instantiate the WebAssembly module.
Instance instance = new Instance(bytes);
// Get the `sum` exported function, call it by passing 5 and 37, and get the result.
Integer result = (Integer) instance.exports.getFunction("sum").apply(5, 37)[0];
assert result == 42;
instance.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment