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

如何通过终端或代码运行遵循WasmGC规范的.wasm文件?

能否运行遵循WasmGC规范的.wasm文件?

可以,目前主流的Wasm运行时和工具链已支持WasmGC规范,不管是终端直接运行还是通过代码调用都能实现,具体方式如下:

终端运行方式

  • 使用wasmtime:这是支持WasmGC的主流运行时之一,安装后直接在终端执行命令:
    wasmtime --enable-gc your-module.wasm
    
  • 使用wasmer:同样原生支持WasmGC,命令格式类似:
    wasmer run --enable-gc your-module.wasm
    

编程代码调用方式

JavaScript(浏览器/Node.js)

现代浏览器(Chrome 91+、Firefox 90+)和Node.js 16.4+已原生支持WasmGC,可通过WebAssembly.instantiate加载调用模块:

async function runWasmGCModule() {
  const response = await fetch('your-module.wasm');
  const bytes = await response.arrayBuffer();
  const { instance } = await WebAssembly.instantiate(bytes);
  // 调用模块导出的函数
  instance.exports.yourExportedFunction();
}
runWasmGCModule();

Rust

借助wasmtime crate作为运行时,在代码中加载并运行WasmGC模块:

use wasmtime::{Config, Engine, Module, Store, TypedFunc};

fn main() -> anyhow::Result<()> {
    let mut config = Config::new();
    config.wasm_gc(true); // 启用WasmGC支持
    let engine = Engine::new(&config)?;
    let module = Module::from_file(&engine, "your-module.wasm")?;
    
    let mut store = Store::new(&engine, ());
    let instance = module.instantiate(&mut store)?;
    
    // 获取并调用导出函数
    let func: TypedFunc<(), ()> = instance.get_typed_func(&mut store, "yourExportedFunction")?;
    func.call(&mut store, ())?;
    
    Ok(())
}

注意事项

  • 确保使用的运行时或环境版本足够新,旧版本可能不支持WasmGC规范;
  • 编译.wasm模块时,需确保工具链(如rustc+wasm-bindgen、binaryen)支持WasmGC,编译时需启用对应参数。

内容的提问来源于stack exchange,提问作者Troels Lund

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.04 19:06:00