WebAssembly 的 Table 对象怎么在 React 里调用?
我在 React 项目里尝试使用 WebAssembly 的 Table 对象来管理函数引用,但一直报错说 “table.get is not a function”。明明我已经从 wasm 模块导出了 table,也确认它是个 WebAssembly.Table 实例,可就是没法正常调用。
下面是我简化后的代码,加载 wasm 后想通过 table 调用一个函数,但卡在这一步了:
useEffect(() => {
fetch('example.wasm')
.then(res => res.arrayBuffer())
.then(bytes => WebAssembly.instantiate(bytes))
.then(results => {
const { instance } = results;
const table = instance.exports.table;
console.log(table); // 确实是 Table 对象
const fn = table.get(0); // 这里报错:table.get is not a function
fn();
});
}, []);
试试这样:
另外检查一下你的wasm导出是否正确,确保导出的是table本身而不是table.length之类的属性。