Created
May 12, 2020 09:31
-
-
Save Hywan/db1e93a7185242b03379cb35293a0763 to your computer and use it in GitHub Desktop.
WebAssembly exporte function as regular Java function
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 org.wasmer.exports.Function; | |
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); | |
// Declare the `sum` function, as a regular Java function. | |
Function sum = instance.exports.getFunction("sum"); | |
// Call `sum`. | |
Integer result = (Integer) sum.apply(1, 2)[0]; | |
assert result == 3; | |
instance.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment