// 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));