#[no_mangle]pubexternfnsum(x:i32,y:i32)-i32{x+y}编译为WebAssembly后,得到一个名为simple.wasm的文件。接着Java程序通过传递5和37这两个参数来执行sum函数:
importorg.wasmer.Instance;importjava.io.IOException;importjava.nio.file.Files;importjava.nio.file.Paths;classSimpleExample{publicstaticvoidmain(String[]args)throwsIOException{//ReadtheWebAssemblybytes.byte[]bytes=Files.readAllBytes(Paths.get("simple.wasm"));//InstantiatetheWebAssemblymodule.Instanceinstance=newInstance(bytes);//Getthe`sum`exportedfunction,callitbypassing5and37,andgettheresult.Integerresult=(Integer)instance.exports.getFunction("sum").apply(5,37)[0];assertresult==42;instance.close();}}可以看到,WasmerJNI的API与标准JavaScriptAPI非常相似。关于WasmerJNI的更多信息,可以查看官方公告: