使用 Firefox 调试 WASM:导入大小不兼容的表



我正在尝试学习Web组装。我能够从 C 代码编译 wasm。但是,我在尝试让我的代码在 Firefox 中运行时遇到了很多困难。我的代码非常基本:

你好.html

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
</body>
<script async type="text/javascript" src="hello.js"></script>
</html>

你好.js

"use strict";
const imports = {
env: {
"abort": function() {},
"memoryBase": 0,
"tableBase": 0,
"memory": new WebAssembly.Memory({ initial: 4 }),
"table": new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
}
}
WebAssembly.instantiateStreaming(fetch('hello.wasm'), imports)
.then(obj => console.log(obj.instance.exports._add(1, 2)))
.catch(error => console.log(error));

你好.c

#include <emscripten.h>
EMSCRIPTEN_KEEPALIVE int add(x, y) { return x + y; }

我像这样编译我的代码:

emcc hello.c -O1 -g4 -s WASM=1 -s SIDE_MODULE=1 -o hello.wasm --source-map-base http://localhost:8080/ --emrun

我使用 emrun 来提供我的文件

emrun --no_browser --port 8080 .

问题所在

最初,Firefox 抱怨LinkError: "import object field 'abort' is not a Function".检查 hello.wast,在编译过程中生成,看起来确实需要中止函数(我猜abort()是 C 运行时的预期部分(。因此,我将"abort": function() {},行添加到imports的环境部分。

但是现在我得到了一个LinkError: "imported Table with incompatible size".我对这个错误试图表明什么感到茫然。如何让我的 wasm 代码运行?

我正在尝试使用 Firefox 开发人员版 63.0b8(64 位(进行调试。 EMC 是 1.38.11。

我是个白痴。将此处的 0 更改为任何其他数字。例如:

"table": new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
|
v
"table": new WebAssembly.Table({ initial: 2, element: 'anyfunc' }),

这个你好.js文件应该可以工作。

"use strict";
const imports = {
env: {
"abort": function() {},
"memoryBase": 0,
"tableBase": 0,
"memory": new WebAssembly.Memory({ initial: 4 }),
"table": new WebAssembly.Table({ initial: 4, element: 'anyfunc' }),
}
}
WebAssembly.instantiateStreaming(fetch('hello.wasm'), imports)
.then(obj => console.log(obj.instance.exports._add(1, 2)))
.catch(error => console.log(error));

相关内容

最新更新