You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

能否在Flutter Android应用的WebView中使用Wasm(WebAssembly)文件?

能否在Flutter Android应用的WebView组件中使用Wasm(WebAssembly)文件?

可以实现,以下是具体的解决方案:我已将C++代码转换为sum.wasm文件,对应的JS调用函数如下:

function sum(x, y) {   
  const request = new XMLHttpRequest();    
  request.open('GET', 'sum.wasm');      
  request.responseType = 'arraybuffer';      
  request.onload = () => {   
    const wasmBytes = request.response;   
    const imports = {
      env: {
        __stack_pointer: new WebAssembly.Global({ value: 'i32', mutable: true }, 1024),   
        __memory_base: 0,   
        memory: new WebAssembly.Memory({ initial: 128 ,maximum : 1024}),   
        __table_base: 0,   
        __indirect_function_table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' }),
      }   
    };   
    WebAssembly.instantiate(wasmBytes, imports).then(result => {
      const sum = result.instance.exports.add; // "add" 是C++代码中定义的函数
      const result2 = sum(x,y);
      console.log(result2);
      myJavaScriptChannel.postMessage(result2);
    }).catch(console.error);   
  };   
  request.send();   
}

关键说明

  • 通过XMLHttpRequest以arraybuffer类型加载Wasm二进制文件
  • 配置Wasm运行所需的环境依赖,包括内存空间、全局栈指针和函数表
  • 实例化Wasm模块后,调用导出的C++函数计算结果,并通过myJavaScriptChannel将结果传递回Flutter端

内容的提问来源于stack exchange,提问作者behzad ahmad ali nejad

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 18:02:41