Last active
May 12, 2020 09:23
-
-
Save Hywan/6ffb1a7ff59c9df9ff5dc760692b21cf to your computer and use it in GitHub Desktop.
Execute a WebAssembly module from Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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