WebAssembly.Instance.prototype.exports
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since October 2017.
WebAssembly.Instance
对象原型的 exports
只读属性返回一个对象,该对象将从 WebAssembly 模块实例中导出的所有函数作为其成员,让 JavaScript 能访问和使用这些函数。
示例
使用 exports
使用 fetch 获取一些 WebAssembly 字节码之后,我们使用 WebAssembly.instantiateStreaming()
函数编译和实例化模块,在该过程中将一个 JavaScript 函数导入到 WebAssembly 模块中。然后调用一个 Instance
导出的导出 WebAssembly 函数。
js
const importObject = {
my_namespace: {
imported_func(arg) {
console.log(arg);
},
},
};
WebAssembly.instantiateStreaming(fetch("simple.wasm"), importObject).then(
(obj) => obj.instance.exports.exported_func(),
);
备注: 你也可以在 Github 上的 instantiate-streaming.html 找到这个示例(也可以在线查看)。
规范
Specification |
---|
WebAssembly JavaScript Interface # dom-instance-exports |