WebAssembly 导出函数 returns 未定义
WebAssembly exported function returns undefined
我正尝试在我的 javascript 应用程序的 WebWorker 线程上 运行 wasm-flate。
使用:
<script src="https://unpkg.com/wasm-flate@0.1.11-alpha/dist/bootstrap.js"></script>
将平面对象放入主线程工作正常,但为了让它在 WebWorker 上工作,我使用了
wapm install drbh/flate
然后获取编译后的 .wasm 代码,并尝试将其加载到 WebWorker 上:
fetch("../lib/wasm_flate_bg.wasm")
.then(function(response){
response.arrayBuffer()
.then(function(buffer){
WebAssembly.compile(buffer)
.then(function(obj){
WebAssembly.instantiate(obj)
.then(function(skee){
flate=skee;
console.log(flate);
console.log(flate.exports);
console.log(flate.exports.zlib_encode);
console.log(flate.exports.zlib_encode('420'));
});
});
});
});
这一切都有效,直到我实际上 运行 zlib_encode 功能。由于某种原因,它总是 returns undefined,所有的函数似乎都是。但是,它们在通过 .HTML.
加载时工作正常
所以,我的问题是,我在这里误解了什么,我该如何解决?
谢谢。
WebAssembly 函数仅对数字进行运算。对于像 zlib_encode()
这样的高级接口,您需要 JavaScript 中的包装函数,它与 WebAssembly 模块导出的低级定义接口。
在 wasm-flate 的情况下,这些似乎由 https://unpkg.com/wasm-flate@0.1.11-alpha/dist/0.bootstrap.js, and defined in https://github.com/drbh/wasm-flate/blob/master/src/lib.rs 提供。
我正尝试在我的 javascript 应用程序的 WebWorker 线程上 运行 wasm-flate。
使用:
<script src="https://unpkg.com/wasm-flate@0.1.11-alpha/dist/bootstrap.js"></script>
将平面对象放入主线程工作正常,但为了让它在 WebWorker 上工作,我使用了
wapm install drbh/flate
然后获取编译后的 .wasm 代码,并尝试将其加载到 WebWorker 上:
fetch("../lib/wasm_flate_bg.wasm")
.then(function(response){
response.arrayBuffer()
.then(function(buffer){
WebAssembly.compile(buffer)
.then(function(obj){
WebAssembly.instantiate(obj)
.then(function(skee){
flate=skee;
console.log(flate);
console.log(flate.exports);
console.log(flate.exports.zlib_encode);
console.log(flate.exports.zlib_encode('420'));
});
});
});
});
这一切都有效,直到我实际上 运行 zlib_encode 功能。由于某种原因,它总是 returns undefined,所有的函数似乎都是。但是,它们在通过 .HTML.
加载时工作正常所以,我的问题是,我在这里误解了什么,我该如何解决? 谢谢。
WebAssembly 函数仅对数字进行运算。对于像 zlib_encode()
这样的高级接口,您需要 JavaScript 中的包装函数,它与 WebAssembly 模块导出的低级定义接口。
在 wasm-flate 的情况下,这些似乎由 https://unpkg.com/wasm-flate@0.1.11-alpha/dist/0.bootstrap.js, and defined in https://github.com/drbh/wasm-flate/blob/master/src/lib.rs 提供。