我的wasm模块的C源代码必须使用全局变量,因为它使用的是服务器所共有的代码。但我明白:
Uncaught (in promise) TypeError: WebAssembly.instantiate(): Import #2 module="GOT.mem" error: module is not an object or function
当我尝试使用全局。这是密码..。
daft.html
<html>
<head>
<meta charset=utf-8 http-equiv="Content-Language" content="en"/>
<script src="daft.js"></script>
</head>
</html>
daft.js
const heap0 = new Uint8Array(mem.buffer, 0);
function squawk(cbuf,clen) {
var s = new Uint8Array(heap0, cbuf, clen);
let string = '';
for (let i = 0; i < clen; i++) {
string += String.fromCharCode(s[i]);
}
console.log("Squawk: "+string);
}
var imports = {
env: {
'memory': mem,
'squawk': squawk,
'__memory_base': 0,
}
}
async function init() {
wa = await WebAssembly.instantiateStreaming( fetch("./daft.wasm"), imports );
wa.instance.exports.wam();
}
init();
daft.c
#include <string.h>
char m[] = "Hello again. ";
extern void squawk(const char *, int);
void wam() {
char * msg = (char *) 0;
strcpy(msg, "Hello from C!");
squawk(msg, 13);
}
//void sorgenkind() { squawk(m, 13); }
我是这样编译的:
emcc -s ERROR_ON_UNDEFINED_SYMBOLS=0 -s WASM=1 -s SIDE_MODULE -Os -s EXPORTED_FUNCTIONS=_wam --no-entry -o daft.wasm daft.c
因此,上面的代码工作并打印“你好,从C!”在控制台上。但是,如果我取消注释sorgenkind
,就会发生错误。
我曾尝试过这样的做法:
var imports = {
GOT: { mem: blah blah blah},
...
但我尝试过的一切都没有效果。
有趣的是,一个全局整数不会引发这个问题。似乎字符串是最小的。也许这只是因为全局int被优化了,显然,如果sorgenkind
被注释掉的话,这个字符串也是如此。
我该怎么办?
发布于 2021-10-10 21:23:12
https://stackoverflow.com/questions/69499343
复制相似问题