@ 260918.1raw
current

binaryen.js

binaryen.js is a port of Binaryengithub 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 toolsgithub: 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 APIgithub.

Contents

Future features 🦄 might not be supported by all runtimes.

Types

Module construction

Module manipulation

Module validation

Module optimization

Module creation

Expression construction

Control flow

Variable accesses

Integer operations

Floating point operations

Datatype conversions

Function calls

Linear memory accesses

Host operations

Vector operationsgithub 🦄

Atomic memory accessesgithub 🦄

Atomic read-modify-write operationsgithub 🦄

Atomic wait and notify operationsgithub 🦄

Sign extension operationsgithub 🦄

Multi-value operationsgithub 🦄

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

Exception handling operationsgithub 🦄

Reference types operationsgithub 🦄

Bulk memory operationsgithub 🦄

Expression manipulation

Relooper

Source maps

Debugging