[英]Wasm: Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function Promise.then (async) (anonymous) @ (index):9
I'm new to emscripten and find it very hard... I have obligation to work on windows because i have to test .exe versions of my apps.我是 emscripten 的新手,发现它很难......我有义务在 Windows 上工作,因为我必须测试我的应用程序的 .exe 版本。 I'm on windows 7.
我在 Windows 7 上。
I can compile wasm but javascript cannot read it.我可以编译 wasm 但 javascript 无法读取它。 Here's my code.
这是我的代码。
C code:代码:
char * HelloWorld ()
{
return "Hello World !";
}
Emscripten command-line: Emscripten 命令行:
emcc hello.c -O2 -s ONLY_MY_CODE=1 -s WASM=1 -s SIDE_MODULE=1 -s EXPORTED_FUNCTIONS="['_HelloWorld']" -o hello.wasm
Wat result:结果:
(module
(type $t0 (func (result i32)))
(type $t1 (func))
(import "env" "memory" (memory $env.memory 256))
(import "env" "memoryBase" (global $env.memoryBase i32))
(func $_HelloWorld (export "_HelloWorld") (type $t0) (result i32)
(get_global $env.memoryBase))
(func $__post_instantiate (export "__post_instantiate") (type $t1)
(set_global $g1
(i32.add
(get_global $env.memoryBase)
(i32.const 16)))
(set_global $g2
(i32.add
(get_global $g1)
(i32.const 5242880))))
(global $g1 (mut i32) (i32.const 0))
(global $g2 (mut i32) (i32.const 0))
(data (get_global $env.memoryBase) "Hello World !"))
Javascript: Javascript:
importObject = {};
fetch('hello.wasm').then(response =>
response.arrayBuffer()
).then(bytes =>
WebAssembly.instantiate(bytes, importObject)
).then(results => {
console.log("loaded");
});
Error message:错误信息:
Uncaught (in promise) TypeError: Import #0 module="env" error: module is not an object or function
Promise.then (async)
(anonymous) @ (index):9
Can you tell me what's wrong in my code ?你能告诉我我的代码有什么问题吗?
I think the challenge is this: WebAssembly isn't fully baked yet. 我认为挑战在于:WebAssembly尚未完全成熟。
The examples show their loader from what looks like an NPM package. 这些示例从看起来像NPM软件包的位置显示了它们的加载程序。 In that package, importObj is optional.
在该软件包中,importObj是可选的。
You and I are trying to use WebAssembly.instantiate() - I'm using a web worker. 您和我正在尝试使用WebAssembly.instantiate()-我正在使用网络工作者。
So we need to pass a more complete imports - specifically an env with an abort methoed. 因此,我们需要传递更完整的导入信息-特别是带有异常终止方法的环境。 Here's an example from https://webassembly.studio 's assemblyscript starter project:
这是来自https://webassembly.studio的assemblyscript入门项目的示例:
main.html main.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body style="background: #fff">
<span id="container"></span>
<script src="./main.js"></script>
</body>
</html>
main.js main.js
WebAssembly.instantiateStreaming(fetch("../out/main.wasm"), {
main: {
sayHello() {
console.log("Hello from WebAssembly!");
}
},
env: {
abort(_msg, _file, line, column) {
console.error("abort called at main.ts:" + line + ":" + column);
}
},
}).then(result => {
const exports = result.instance.exports;
document.getElementById("container").textContent = "Result: " + exports.add(19, 23);
}).catch(console.error);
main.ts 主要
declare function sayHello(): void;
sayHello();
export function add(x: i32, y: i32): i32 {
return x + y;
}
Note the env.abort function in importObject 注意importObject中的env.abort函数
you should write importObject like this你应该像这样写 importObject
const importObject = {
env: {
__memory_base: 0,
__table_base: 0,
memory: new WebAssembly.Memory({initial: 1})
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.