binaryen.js
Using in Deno
esm.shやskypack.devだとbuild errorが出る
おそらく内部でwasmをbase64 encodeするなど特殊なことをしているせい
code:example.ts
// Create a module with a single function
const myModule = new binaryen.Module();
myModule.addFunction(
"add",
binaryen.i32,
myModule.block(null, [
myModule.local.set(
2,
myModule.i32.add(
myModule.local.get(0, binaryen.i32),
myModule.local.get(1, binaryen.i32),
),
),
myModule.return(
myModule.local.get(2, binaryen.i32),
),
]),
);
myModule.addFunctionExport("add", "add");
// Optimize the module using default passes and levels
myModule.optimize();
// Validate the module
if (!myModule.validate()) {
throw new Error("validation error");
}
// Generate text format and binary
const textData = myModule.emitText();
console.log(textData);
const wasmData = myModule.emitBinary();
// Example usage with the WebAssembly API
const compiled = new WebAssembly.Module(wasmData);
const instance = new WebAssembly.Instance(compiled, {});
const exports = instance.exports as { add(a: number, b: number): number };
console.log(exports.add(41, 1));
code:mod.ts