binaryen (original) (raw)

123.0.0 • Public • Published a month ago

binaryen.js

binaryen.js is a port of Binaryen to the Web, allowing you to generate WebAssembly using a JavaScript API.

Build status npm version npm nightly version

Usage

$> npm install binaryen

import binaryen from "binaryen";

// Create a module with a single function var 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 var textData = myModule.emitText(); var wasmData = myModule.emitBinary();

// Example usage with the WebAssembly API var compiled = new WebAssembly.Module(wasmData); var instance = new WebAssembly.Instance(compiled, {}); console.log(instance.exports.add(41, 1));

The buildbot also publishes nightly versions once a day if there have been changes. The latest nightly can be installed through

$> npm install binaryen@nightly

or you can use one of the previous versions instead if necessary.

Usage with a CDN

Replace VERSION with a specific version or omit it (not recommended in production) to use main/latest.

Command line

The package includes Node.js builds of Binaryen's command line tools: wasm-shell, wasm-opt, wasm-metadce, wasm2js, wasm-as, wasm-dis, wasm-ctor-eval, wasm-reduce and wasm-merge.

API

Please note that the Binaryen API is evolving fast and that definitions and documentation provided by the package tend to get out of sync despite our best efforts. It's a bot after all. If you rely on binaryen.js and spot an issue, please consider sending a PR our way by updating index.d.ts and README.md to reflect the current API.

Contents

Future features 🦄 might not be supported by all runtimes.

Types

Module construction

Module manipulation

Module validation

Module optimization

Module creation

Expression construction

Note that these are pseudo instructions enabling Binaryen to reason about multiple values on the stack.

Expression manipulation

Relooper

Source maps

Debugging