binaryen.js
a port of Binaryen to the browser/node
https://github.com/AssemblyScript/binaryen.js
API References
Using in Deno
jsdelivrから使える
esm.shやskypack.devだとbuild errorが出る
おそらく内部でwasmをbase64 encodeするなど特殊なことをしているせい
$ deno check --remote -r=https://scrapbox.io https://scrapbox.io/api/code/takker/binaryen.js/example.ts
$ deno run -r=https://scrapbox.io https://scrapbox.io/api/code/takker/binaryen.js/example.ts
code:example.ts
// based on https://github.com/AssemblyScript/binaryen.js
// @deno-types=https://cdn.jsdelivr.net/npm/binaryen@117.0.0/index.d.ts
import binaryen from "https://cdn.jsdelivr.net/npm/binaryen@117.0.0/index.min.js";
// Create a module with a single function
const myModule = new binaryen.Module();
myModule.addFunction(
"add",
binaryen.createType(binaryen.i32, binaryen.i32),
binaryen.i32,
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
// @deno-types=https://cdn.jsdelivr.net/npm/binaryen@117.0.0/index.d.ts
export { default } from "https://cdn.jsdelivr.net/npm/binaryen@117.0.0/index.min.js";
#2024-06-24 16:58:28
#2024-05-30
#2023-12-16 20:14:09