运行编译的 WASM 报错
WebAssembly.instantiate(): Import #0 module=“spectest”: module is not an object or function
node版本:v22.2.0
系统版本:win11专业版
main.mbt
fn main {
let mut value = 0
for index = 0; index < 10000000; index = index + 1 {
value = value + 1;
}
println(123)
}
moon.pkg.json
{
"is-main": true
}
main.ts
import fs from "fs";
(async () => {
try {
// 读取 WebAssembly 文件
const wasmBuffer = fs.readFileSync("./wasm/main2.wasm");
// 使用 WebAssembly.instantiate 直接实例化
//@ts-ignore
const wasmModule = await WebAssembly.instantiate(wasmBuffer, {
Date: {
now: () => Date.now(),
},
});
// 调用 _start 函数来初始化环境
// 类型断言以确保类型安全
//@ts-ignore
const instance = wasmModule.instance as WebAssembly.Instance & {
exports: { _start: () => void };
};
instance.exports._start();
console.log("WebAssembly module initialized successfully.");
} catch (error) {
console.error(error);
debugger;
}
})();