Skip to content

Instantly share code, notes, and snippets.

@Hywan
Created May 12, 2020 09:31
Show Gist options
  • Save Hywan/db1e93a7185242b03379cb35293a0763 to your computer and use it in GitHub Desktop.
Save Hywan/db1e93a7185242b03379cb35293a0763 to your computer and use it in GitHub Desktop.
WebAssembly exporte function as regular Java function
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