Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
binaryen
Advanced tools
JavaScript version of Binaryen, a compiler infrastructure and toolchain library for WebAssembly.
Binaryen is a compiler and toolchain infrastructure library for WebAssembly, written in C++. It is designed to make compiling to WebAssembly easy, fast, and effective. It provides a variety of tools and libraries to optimize and manipulate WebAssembly code.
WebAssembly Module Creation
This feature allows you to create and manipulate WebAssembly modules. The code sample demonstrates creating a simple WebAssembly function that adds two integers.
const binaryen = require('binaryen');
const module = new binaryen.Module();
module.addFunction('add', binaryen.i32, binaryen.i32, [binaryen.i32, binaryen.i32], module.i32.add(module.local.get(0, binaryen.i32), module.local.get(1, binaryen.i32)));
module.validate();
console.log(module.emitText());
Optimization
Binaryen provides optimization capabilities for WebAssembly modules. The code sample shows how to optimize a module, which can improve performance and reduce size.
const binaryen = require('binaryen');
const module = new binaryen.Module();
// Add functions and other elements to the module
module.optimize();
console.log(module.emitText());
Validation
This feature allows you to validate WebAssembly modules to ensure they are well-formed and adhere to the WebAssembly specification. The code sample demonstrates how to validate a module.
const binaryen = require('binaryen');
const module = new binaryen.Module();
// Add functions and other elements to the module
const isValid = module.validate();
console.log('Module is valid:', isValid);
wasm-bindgen is a tool for facilitating high-level interactions between WebAssembly modules and JavaScript. It is particularly focused on Rust to WebAssembly compilation, providing a bridge between the two. Unlike Binaryen, which is more focused on optimization and manipulation of WebAssembly binaries, wasm-bindgen is more about interoperability and ease of use in a JavaScript environment.
AssemblyScript is a TypeScript-like language that compiles to WebAssembly. It is designed to be familiar to JavaScript developers and provides a way to write WebAssembly modules in a high-level language. While Binaryen is a tool for optimizing and manipulating WebAssembly binaries, AssemblyScript is a language that compiles to WebAssembly, offering a different approach to creating WebAssembly modules.
WebAssembly Binary Toolkit (wabt) is a suite of tools for WebAssembly, including a WebAssembly text format parser and a binary format parser. It is similar to Binaryen in that it provides tools for working with WebAssembly, but it focuses more on parsing and converting between text and binary formats rather than optimization and manipulation.
binaryen.js is a port of Binaryen to the Web, allowing you to generate WebAssembly using a JavaScript API.
$> npm install binaryen
var binaryen = require("binaryen");
var myModule = new binaryen.Module();
myModule.addFunction("main", myModule.addFunctionType("i", binaryen.i32, []), [],
myModule.return(
myModule.i32.const(0)
)
);
myModule.addFunctionExport("main", "main");
var textData = myModule.emitText();
var wasmData = myModule.emitBinary();
...
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.
Future features 🦄 might not be supported by all runtimes.
none: Type
The none type, e.g., void
.
i32: Type
32-bit integer type.
i64: Type
64-bit integer type.
f32: Type
32-bit float type.
f64: Type
64-bit float (double) type.
undefined: Type
Special type used with blocks to let the API figure out its result type on its own.
new Module(): Module
Constructs a new module.
parseText(text: string
): Module
Creates a module from Binaryen's s-expression text format (not official stack-style text format).
readBinary(data: Uint8Array
): Module
Creates a module from binary data.
Module#addFunctionType(name: string
, resultType: Type
, paramTypes: Type[]
): Signature
Adds a new function type.
Module#getFunctionTypeBySignature(resultType: Type
, paramTypes: Type[]
): Signature
Gets an existing function type by its parametric signature. Returns 0
if there is no such function type.
Module#addFunction(name: string
, functionType: Signature
, varTypes: Type[]
, body: Expression
): Function
Adds a function. varTypes
indicate additional locals, in the given order.
Module#getFunction(name: string
): Function
Gets a function, by name,
Module#removeFunction(name: string
): void
Removes a function, by name.
Module#addFunctionImport(internalName: string
, externalModuleName: string
, externalBaseName: string
, functionType: Signature
): Import
Adds a function import.
Module#addTableImport(internalName: string
, externalModuleName: string
, externalBaseName: string
): Import
Adds a table import. There's just one table for now, using name "0"
.
Module#addMemoryImport(internalName: string
, externalModuleName: string
, externalBaseName: string
): Import
Adds a memory import. There's just one memory for now, using name "0"
.
Module#addGlobalImport(internalName: string
, externalModuleName: string
, externalBaseName: string
, globalType: Type
): Import
Adds a global variable import. Imported globals must be immutable.
Module#removeImport(internalName: string
): void
Removes an import, by internal name.
Module#addFunctionExport(internalName: string
, externalName: string
): Export
Adds a function export.
Module#addTableExport(internalName: string
, externalName: string
): Export
Adds a table export. There's just one table for now, using name "0"
.
Module#addMemoryExport(internalName: string
, externalName: string
): Export
Adds a memory export. There's just one memory for now, using name "0"
.
Module#addGlobalExport(internalName: string
, externalName: string
): Export
Adds a global variable export. Exported globals must be immutable.
Module#removeExport(externalName: string
): void
Removes an export, by external name.
Module#setFunctionTable(funcs: Function[]
): void
Sets the contents of the function table. There's just one table for now, using name "0"
.
Module#setMemory(initial: number
, maximum: number
, exportName: string | null
, segments: MemorySegment[]
): void
Sets the memory. There's just one memory for now, using name "0"
. Providing exportName
also creates a memory export.
Module#setStart(start: Function
): void
Sets the start function.
Module#optimize(): void
Optimizes the module using the default optimization passes.
Module#optimizeFunction(func: Function | string
): void
Optimizes a single function using the default optimization passes.
Module#runPasses(passes: string[]
): void
Runs the specified passes on the module.
Module#runPassesOnFunction(func: Function | string
, passes: string[]
): void
Runs the specified passes on a single function.
Module#autoDrop(): void
Enables automatic insertion of drop
operations where needed. Lets you not worry about dropping when creating your code.
boolean
true
if valid, otherwise prints validation errors and returns false
.Module#emitBinary(): Uint8Array
Returns the module in binary format.
Module#emitText(): string
Returns the module in Binaryen's s-expression text format (not official stack-style text format).
Module#emitAsmjs(): string
Returns the asm.js representation of the module.
Module#dispose(): void
Releases the resources held by the module once it isn't needed anymore.
Module#setAPITracing(on: boolean
): void
Enables tracing of the C-API in the console. Can be very useful when filing bug reports.
Module#interpret(): void
Runs the module in the interpreter, calling the start function.
Module#block(label: string | null
, children: Expression[]
, resultType?: Type
): Expression
Creates a block. resultType
defaults to none
.
Module#if(condition: Expression
, ifTrue: Expression
, ifFalse?: Expression
): Expression
Creates an if or if/else combination.
Module#loop(label: string | null
, body: Expression
): Expression
Creates a loop.
Module#break(label: string
, condition?: Expression
, value?: Expression
): Expression
Creates a break (br) to a label.
Module#switch(labels: string[]
, defaultLabel: string
, condition: Expression
, value?: Expression
): Expression
Creates a switch (br_table).
Module#nop(): Expression
Creates a no-operation (nop) instruction.
Module#return(value?: Expression
): Expression
Creates a return.
Module#unreachable(): Expression
Creates an unreachable instruction that will always trap.
Module#drop(value: Expression
): Expression
Creates a drop of a value.
Module#select(condition: Expression
, ifTrue: Expression
, ifFalse: Expression
): Expression
Creates a select of one of two values.
number
): I32Expression
number
, high: number
): I64Expression
number
): F32Expression
number
): F32Expression
number
): F64Expression
number
, high: number
): F64Expression
Module#getLocal(index: number
, type: Type
): Expression
Creates a get_local for the local at the specified index. Note that we must specify the type here as we may not have created the local being called yet.
Module#setLocal(index: number
, value: Expression
): Expression
Creates a set_local for the local at the specified index.
Module#teeLocal(index: number
, value: Expression
): Expression
Creates a tee_local for the local at the specified index. A tee differs from a set in that the value remains on the stack.
Module#getGlobal(name: string
, type: Type
): Expression
Creates a get_global for the global with the specified name. Note that we must specify the type here as we may not have created the global being called yet.
Module#setGlobal(name: string
, value: Expression
): Expression
Creates a set_global for the global with the specified name.
Variable access methods above are also aliased in underscore notation, e.g., get_local
.
I32Expression
): I32Expression
I32Expression
): I32Expression
I32Expression
): I32Expression
I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I32Expression
, right: I32Expression
): I32Expression
I64Expression
): I64Expression
I64Expression
): I64Expression
I64Expression
): I64Expression
I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
I64Expression
, right: I64Expression
): I64Expression
F32Expression
): F32Expression
F32Expression
): F32Expression
F32Expression
): F32Expression
F32Expression
): F32Expression
F32Expression
): F32Expression
F32Expression
): F32Expression
F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F32Expression
, right: F32Expression
): F32Expression
F64Expression
): F64Expression
F64Expression
): F64Expression
F64Expression
): F64Expression
F64Expression
): F64Expression
F64Expression
): F64Expression
F64Expression
): F64Expression
F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F64Expression
, right: F64Expression
): F64Expression
F32Expression
): I32Expression
F64Expression
): I32Expression
F32Expression
): I32Expression
F64Expression
): I32Expression
F32Expression
): I32Expression
I64Expression
): I32Expression
F32Expression
): I64Expression
F64Expression
): I64Expression
F32Expression
): I64Expression
F64Expression
): I64Expression
F64Expression
): I64Expression
I32Expression
): I64Expression
I32Expression
): I64Expression
I32Expression
): F32Expression
I32Expression
): F32Expression
I64Expression
): F32Expression
I32Expression
): F32Expression
I64Expression
): F32Expression
F64Expression
): F32Expression
I32Expression
): F64Expression
I32Expression
): F64Expression
I64Expression
): F64Expression
I32Expression
): F64Expression
I64Expression
): F64Expression
F32Expression
): F64Expression
Module#call(name: string
, operands: Expression[]
, returnType: Type
): Expression
Creates a call to a function. Note that we must specify the return type here as we may not have created the function being called yet.
Module#callImport(name: string
, operands: Expression[]
, returnType: Type
): Expression
Similar to call, but calls an imported function.
Module#callIndirect(target: Expression
, operands: Expression[]
, returnType: Type
): Expression
Similar to call, but calls indirectly, i.e., via a function pointer, so an expression replaces the name as the called value.
Function call methods above are also aliased in underscore notation, e.g., call_indirect
.
number
, align: number
, ptr: Expression
): I32Expression
number
, align: number
, ptr: Expression
): I32Expression
number
, align: number
, ptr: Expression
): I32Expression
number
, align: number
, ptr: Expression
): I32Expression
number
, align: number
, ptr: Expression
): I32Expression
number
, align: number
, ptr: Expression
, value: I32Expression
): Expression
number
, align: number
, ptr: Expression
, value: I32Expression
): Expression
number
, align: number
, ptr: Expression
, value: I32Expression
): Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
): I64Expression
number
, align: number
, ptr: Expression
, value: I64Expression
): Expression
number
, align: number
, ptr: Expression
, value: I64Expression
): Expression
number
, align: number
, ptr: Expression
, value: I64Expression
): Expression
number
, align: number
, ptr: Expression
, value: I64Expression
): Expression
number
, align: number
, ptr: Expression
): F32Expression
number
, align: number
, ptr: Expression
, value: F32Expression
): Expression
number
, align: number
, ptr: Expression
): F64Expression
number
, align: number
, ptr: Expression
, value: F64Expression
): Expression
I32Expression
number
): I32Expression
string
): Expression
🦄Host operation methods above are also aliased in underscore notation, e.g., current_memory
.
number
, ptr: Expression
): I32Expression
number
, ptr: Expression
): I32Expression
number
, ptr: Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): Expression
number
, ptr: Expression
, value: I32Expression
): Expression
number
, ptr: Expression
, value: I32Expression
): Expression
number
, ptr: Expression
): I64Expression
number
, ptr: Expression
): I64Expression
number
, ptr: Expression
): I64Expression
number
, ptr: Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): Expression
number
, ptr: Expression
, value: I64Expression
): Expression
number
, ptr: Expression
, value: I64Expression
): Expression
number
, ptr: Expression
, value: I64Expression
): Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, expected: I32Expression
, replacement: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, expected: I32Expression
, replacement: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, value: I32Expression
): I32Expression
number
, ptr: Expression
, expected: I32Expression
, replacement: I32Expression
): I32Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, expected: I64Expression
, replacement: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, expected: I64Expression
, replacement: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, expected: I64Expression
, replacement: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, value: I64Expression
): I64Expression
number
, ptr: Expression
, expected: I64Expression
, replacement: I64Expression
): I64Expression
Expression
, expected: I32Expression
, timeout: I64Expression
): I32Expression
Expression
, expected: I64Expression
, timeout: I64Expression
): I32Expression
Expression
, wakeCount: I64Expression
): I64Expression
getExpressionId(expr: Expression
): ExpressionId
Gets the id (kind) of the specified expression. Possible values are:
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
ExpressionId
getExpressionType(expr: Expression
): Type
Gets the type of the specified expression.
getConstValueI32(expr: Expression
): number
Gets the value of an i32.const (id=ConstId, type=i32) expression.
getConstValueI64(expr: Expression
): { low: number, high: number }
Gets the value of an i64.const (id=ConstId, type=i64) expression.
getConstValueF32(expr: Expression
): number
Gets the value of an f32.const (id=ConstId, type=f32) expression.
getConstValueF64(expr: Expression
): number
Gets the value of an f64.const (id=ConstId, type=f64) expression.
getFunctionBody(func: Function
): Expression
Gets the body of a function.
new Relooper(): Relooper
Constructs a relooper instance. This lets you provide an arbitrary CFG, and the relooper will structure it for WebAssembly.
Relooper#addBlock(code: Expression
): RelooperBlock
Adds a new block to the CFG, containing the provided code as its body.
Relooper#addBranch(from: RelooperBlock
, to: RelooperBlock
, condition: Expression
, code: Expression
): void
Adds a branch from a block to another block, with a condition (or nothing, if this is the default branch to take from the origin - each block must have one such branch), and optional code to execute on the branch (useful for phis).
Relooper#addBlockWithSwitch(code: Expression
, condition: Expression
): RelooperBlock
Adds a new block, which ends with a switch/br_table, with provided code and condition (that determines where we go in the switch).
Relooper#addBranchForSwitch(from: RelooperBlock
, to: RelooperBlock
, indexes: number[]
, code: Expression
): void
Adds a branch from a block ending in a switch, to another block, using an array of indexes that determine where to go, and optional code to execute on the branch.
Relooper#renderAndDispose(entry: RelooperBlock
, labelHelper: number
, module: Module
): Expression
Renders and cleans up the Relooper instance. Call this after you have created all the blocks and branches, giving it the entry block (where control flow begins), a label helper variable (an index of a local we can use, necessary for irreducible control flow), and the module. This returns an expression - normal WebAssembly code - that you can use normally anywhere.
Clone the GitHub repository including submodules and install the development dependencies:
$> git clone --recursive https://github.com/AssemblyScript/binaryen.js.git
$> cd binaryen.js
$> npm install
Make sure Emscripten is properly set up on your system.
Afterwards, to build the binaryen
submodule to index.js
, run:
$> npm run build
To run the tests, do:
$> npm test
FAQs
Browser & Node.js builds of Binaryen, a compiler infrastructure and toolchain library for WebAssembly.
The npm package binaryen receives a total of 102,308 weekly downloads. As such, binaryen popularity was classified as popular.
We found that binaryen demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.