WebAssembly 的 Table 对象怎么在 React 里调用?

Dev · 子怡 阅读 3

我在 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();
    });
}, []);
我来解答 赞 2 收藏
二维码
手机扫码查看
1 条解答
码农嘉蕊
问题在于导出的table可能不是直接的WebAssembly.Table实例,而是个代理对象。

试试这样:

const table = new WebAssembly.Table({ element: 'anyfunc', initial: 1 });
// 或者直接用 WebAssembly.Table.prototype.get.call
const fn = WebAssembly.Table.prototype.get.call(instance.exports.table, 0);


另外检查一下你的wasm导出是否正确,确保导出的是table本身而不是table.length之类的属性。
点赞
2026-03-12 18:02