Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

binaryen

Package Overview
Dependencies
Maintainers
2
Versions
1567
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

binaryen

JavaScript version of Binaryen, a compiler infrastructure and toolchain library for WebAssembly.

  • 44.0.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
2
Created
Source

binaryen.js

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

npm npm (tag) Build Status

Usage

$> npm install binaryen
var binaryen = require("binaryen");

// Create a module with a single function
var myModule = new binaryen.Module();

myModule.addFunction("add", myModule.addFunctionType("iii", binaryen.i32, [ binaryen.i32, binaryen.i32 ]), [ binaryen.i32 ],
  myModule.block(null, [
    myModule.setLocal(2,
      myModule.i32.add(
        myModule.getLocal(0, binaryen.i32),
        myModule.getLocal(1, binaryen.i32)
      )
    ),
    myModule.return(
      myModule.getLocal(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();
...

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.

API

Contents

Future features 🦄 might not be supported by all runtimes.

Types

  • 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.

  • auto: Type
    Special type used in Module#block exclusively. Lets the API figure out a block's result type automatically.

  • unreachable: Type
    Special type indicating unreachable code when obtaining information about an expression.

Module construction

  • 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 manipulation

  • 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#autoDrop(): void
    Enables automatic insertion of drop operations where needed. Lets you not worry about dropping when creating your code.

  • getFunctionTypeInfo(ftype: FunctionType: FunctionTypeInfo
    Obtains information about a function type.

    • FunctionTypeInfo#name: string | null
    • FunctionTypeInfo#params: Type[]
    • FunctionTypeInfo#result: Type
  • getFunctionInfo(ftype: Function: FunctionInfo
    Obtains information about a function.

    • FunctionInfo#name: string | null
    • FunctionInfo#type: FunctionType
    • FunctionInfo#params: Type[]
    • FunctionInfo#result: Type
    • FunctionInfo#vars: Type
    • FunctionInfo#body: Expression
  • getImportInfo(import_: Import): ImportInfo
    Obtains information about an import, always including:

    • ImportInfo#kind: ExternalKind
    • ImportInfo#module: string
    • ImportInfo#base: string
    • ImportInfo#name: string

    Additional properties depend on the expression's kind and are usually equivalent to the respective parameters when creating such an import:

    • GlobalImportInfo#globalType: Type
    • FunctionImportInfo#functionType: FunctionType

    Possible ExternalKind values are:

    • ExternalFunction: ExternalKind
    • ExternalTable: ExternalKind
    • ExternalMemory: ExternalKind
    • ExternalGlobal: ExternalKind
  • getExportInfo(export_: Export): ExportInfo
    Obtains information about an export.

    • ExportInfo#kind: ExternalKind
    • ExportInfo#name: string
    • ExportInfo#value: string

Module validation

  • Module#validate(): boolean
    Validates the module. Returns true if valid, otherwise prints validation errors and returns false.

Module optimization

  • 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.

  • getOptimizeLevel(): number
    Gets the currently set optimize level. 0, 1, 2 correspond to -O0, -O1, -O2 (default), etc.

  • setOptimizeLevel(level: number): void
    Sets the optimization level to use. 0, 1, 2 correspond to -O0, -O1, -O2 (default), etc.

  • getShrinkLevel(): number
    Gets the currently set shrink level. 0, 1, 2 correspond to -O0, -Os (default), -Oz.

  • setShrinkLevel(level: number): void
    Sets the shrink level to use. 0, 1, 2 correspond to -O0, -Os (default), -Oz.

  • getDebugInfo(): boolean
    Gets whether generating debug information is currently enabled or not.

  • setDebugInfo(on: boolean): void
    Enables or disables debug information in emitted binaries.

Module creation

  • Module#emitBinary(): Uint8Array
    Returns the module in binary format.

  • Module#emitBinary(sourceMapUrl: string | null): BinaryWithSourceMap
    Returns the module in binary format with its source map. If sourceMapUrl is null, source map generation is skipped.

    • BinaryWithSourceMap#binary: Uint8Array
    • BinaryWithSourceMap#sourceMap: string | null
  • 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.

Expression construction

Control flow
  • 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.

Constants
  • Module#i32.const(value: number): Expression
  • Module#i64.const(low: number, high: number): Expression
  • Module#f32.const(value: number): Expression
  • Module#f32.const_bits(value: number): Expression
  • Module#f64.const(value: number): Expression
  • Module#f64.const_bits(low: number, high: number): Expression
Variable accesses
  • Module#getLocal/get_local(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/set_local(index: number, value: Expression): Expression
    Creates a set_local for the local at the specified index.

  • Module#teeLocal/tee_local(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/get_global(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/set_global(name: string, value: Expression): Expression
    Creates a set_global for the global with the specified name.

Integer operations
  • Module#i32.clz(value: Expression): Expression
  • Module#i32.ctz(value: Expression): Expression
  • Module#i32.popcnt(value: Expression): Expression
  • Module#i32.eqz(value: Expression): Expression
  • Module#i32.add(left: Expression, right: Expression): Expression
  • Module#i32.sub(left: Expression, right: Expression): Expression
  • Module#i32.mul(left: Expression, right: Expression): Expression
  • Module#i32.div_s(left: Expression, right: Expression): Expression
  • Module#i32.div_u(left: Expression, right: Expression): Expression
  • Module#i32.rem_s(left: Expression, right: Expression): Expression
  • Module#i32.rem_u(left: Expression, right: Expression): Expression
  • Module#i32.and(left: Expression, right: Expression): Expression
  • Module#i32.or(left: Expression, right: Expression): Expression
  • Module#i32.xor(left: Expression, right: Expression): Expression
  • Module#i32.shl(left: Expression, right: Expression): Expression
  • Module#i32.shr_u(left: Expression, right: Expression): Expression
  • Module#i32.shr_s(left: Expression, right: Expression): Expression
  • Module#i32.rotl(left: Expression, right: Expression): Expression
  • Module#i32.rotr(left: Expression, right: Expression): Expression
  • Module#i32.eq(left: Expression, right: Expression): Expression
  • Module#i32.ne(left: Expression, right: Expression): Expression
  • Module#i32.lt_s(left: Expression, right: Expression): Expression
  • Module#i32.lt_u(left: Expression, right: Expression): Expression
  • Module#i32.le_s(left: Expression, right: Expression): Expression
  • Module#i32.le_u(left: Expression, right: Expression): Expression
  • Module#i32.gt_s(left: Expression, right: Expression): Expression
  • Module#i32.gt_u(left: Expression, right: Expression): Expression
  • Module#i32.ge_s(left: Expression, right: Expression): Expression
  • Module#i32.ge_u(left: Expression, right: Expression): Expression
  • Module#i64.clz(value: Expression): Expression
  • Module#i64.ctz(value: Expression): Expression
  • Module#i64.popcnt(value: Expression): Expression
  • Module#i64.eqz(value: Expression): Expression
  • Module#i64.add(left: Expression, right: Expression): Expression
  • Module#i64.sub(left: Expression, right: Expression): Expression
  • Module#i64.mul(left: Expression, right: Expression): Expression
  • Module#i64.div_s(left: Expression, right: Expression): Expression
  • Module#i64.div_u(left: Expression, right: Expression): Expression
  • Module#i64.rem_s(left: Expression, right: Expression): Expression
  • Module#i64.rem_u(left: Expression, right: Expression): Expression
  • Module#i64.and(left: Expression, right: Expression): Expression
  • Module#i64.or(left: Expression, right: Expression): Expression
  • Module#i64.xor(left: Expression, right: Expression): Expression
  • Module#i64.shl(left: Expression, right: Expression): Expression
  • Module#i64.shr_u(left: Expression, right: Expression): Expression
  • Module#i64.shr_s(left: Expression, right: Expression): Expression
  • Module#i64.rotl(left: Expression, right: Expression): Expression
  • Module#i64.rotr(left: Expression, right: Expression): Expression
  • Module#i64.eq(left: Expression, right: Expression): Expression
  • Module#i64.ne(left: Expression, right: Expression): Expression
  • Module#i64.lt_s(left: Expression, right: Expression): Expression
  • Module#i64.lt_u(left: Expression, right: Expression): Expression
  • Module#i64.le_s(left: Expression, right: Expression): Expression
  • Module#i64.le_u(left: Expression, right: Expression): Expression
  • Module#i64.gt_s(left: Expression, right: Expression): Expression
  • Module#i64.gt_u(left: Expression, right: Expression): Expression
  • Module#i64.ge_s(left: Expression, right: Expression): Expression
  • Module#i64.ge_u(left: Expression, right: Expression): Expression
Floating point operations
  • Module#f32.neg(value: Expression): Expression
  • Module#f32.abs(value: Expression): Expression
  • Module#f32.ceil(value: Expression): Expression
  • Module#f32.floor(value: Expression): Expression
  • Module#f32.trunc(value: Expression): Expression
  • Module#f32.nearest(value: Expression): Expression
  • Module#f32.sqrt(value: Expression): Expression
  • Module#f32.add(left: Expression, right: Expression): Expression
  • Module#f32.sub(left: Expression, right: Expression): Expression
  • Module#f32.mul(left: Expression, right: Expression): Expression
  • Module#f32.div(left: Expression, right: Expression): Expression
  • Module#f32.copysign(left: Expression, right: Expression): Expression
  • Module#f32.min(left: Expression, right: Expression): Expression
  • Module#f32.max(left: Expression, right: Expression): Expression
  • Module#f32.eq(left: Expression, right: Expression): Expression
  • Module#f32.ne(left: Expression, right: Expression): Expression
  • Module#f32.lt(left: Expression, right: Expression): Expression
  • Module#f32.le(left: Expression, right: Expression): Expression
  • Module#f32.gt(left: Expression, right: Expression): Expression
  • Module#f32.ge(left: Expression, right: Expression): Expression
  • Module#f64.neg(value: Expression): Expression
  • Module#f64.abs(value: Expression): Expression
  • Module#f64.ceil(value: Expression): Expression
  • Module#f64.floor(value: Expression): Expression
  • Module#f64.trunc(value: Expression): Expression
  • Module#f64.nearest(value: Expression): Expression
  • Module#f64.sqrt(value: Expression): Expression
  • Module#f64.add(left: Expression, right: Expression): Expression
  • Module#f64.sub(left: Expression, right: Expression): Expression
  • Module#f64.mul(left: Expression, right: Expression): Expression
  • Module#f64.div(left: Expression, right: Expression): Expression
  • Module#f64.copysign(left: Expression, right: Expression): Expression
  • Module#f64.min(left: Expression, right: Expression): Expression
  • Module#f64.max(left: Expression, right: Expression): Expression
  • Module#f64.eq(left: Expression, right: Expression): Expression
  • Module#f64.ne(left: Expression, right: Expression): Expression
  • Module#f64.lt(left: Expression, right: Expression): Expression
  • Module#f64.le(left: Expression, right: Expression): Expression
  • Module#f64.gt(left: Expression, right: Expression): Expression
  • Module#f64.ge(left: Expression, right: Expression): Expression
Datatype conversions
  • Module#i32.trunc_s.f32(value: Expression): Expression
  • Module#i32.trunc_s.f64(value: Expression): Expression
  • Module#i32.trunc_u.f32(value: Expression): Expression
  • Module#i32.trunc_u.f64(value: Expression): Expression
  • Module#i32.reinterpret(value: Expression): Expression
  • Module#i32.wrap(value: Expression): Expression
  • Module#i64.trunc_s.f32(value: Expression): Expression
  • Module#i64.trunc_s.f64(value: Expression): Expression
  • Module#i64.trunc_u.f32(value: Expression): Expression
  • Module#i64.trunc_u.f64(value: Expression): Expression
  • Module#i64.reinterpret(value: Expression): Expression
  • Module#i64.extend_s(value: Expression): Expression
  • Module#i64.extend_u(value: Expression): Expression
  • Module#f32.reinterpret(value: Expression): Expression
  • Module#f32.convert_s.i32(value: Expression): Expression
  • Module#f32.convert_s.i64(value: Expression): Expression
  • Module#f32.convert_u.i32(value: Expression): Expression
  • Module#f32.convert_u.i64(value: Expression): Expression
  • Module#f32.demote(value: Expression): Expression
  • Module#f64.reinterpret(value: Expression): Expression
  • Module#f64.convert_s.i32(value: Expression): Expression
  • Module#f64.convert_s.i64(value: Expression): Expression
  • Module#f64.convert_u.i32(value: Expression): Expression
  • Module#f64.convert_u.i64(value: Expression): Expression
  • Module#f64.promote(value: Expression): Expression
Function calls
  • 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/call_import(name: string, operands: Expression[], returnType: Type): Expression
    Similar to call, but calls an imported function.

  • Module#callIndirect/call_indirect(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.

Linear memory accesses
  • Module#i32.load(offset: number, align: number, ptr: Expression): Expression
  • Module#i32.load8_s(offset: number, align: number, ptr: Expression): Expression
  • Module#i32.load8_u(offset: number, align: number, ptr: Expression): Expression
  • Module#i32.load16_s(offset: number, align: number, ptr: Expression): Expression
  • Module#i32.load16_u(offset: number, align: number, ptr: Expression): Expression
  • Module#i32.store(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#i32.store8(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#i32.store16(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#i64.load(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.load8_s(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.load8_u(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.load16_s(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.load16_u(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.load32_s(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.load32_u(offset: number, align: number, ptr: Expression): Expression
  • Module#i64.store(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#i64.store8(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#i64.store16(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#i64.store32(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#f32.load(offset: number, align: number, ptr: Expression): Expression
  • Module#f32.store(offset: number, align: number, ptr: Expression, value: Expression): Expression
  • Module#f64.load(offset: number, align: number, ptr: Expression): Expression
  • Module#f64.store(offset: number, align: number, ptr: Expression, value: Expression): Expression
Host operations
  • Module#currentMemory/current_memory(): Expression
  • Module#growMemory/get_memory(value: number): Expression
  • Module#hasFeature/has_feature(name: string): Expression 🦄
Atomic memory accesses 🦄
  • Module#i32.atomic.load(offset: number, ptr: Expression): Expression
  • Module#i32.atomic.load8_u(offset: number, ptr: Expression): Expression
  • Module#i32.atomic.load16_u(offset: number, ptr: Expression): Expression
  • Module#i32.atomic.store(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.store8(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.store16(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.load(offset: number, ptr: Expression): Expression
  • Module#i64.atomic.load8_u(offset: number, ptr: Expression): Expression
  • Module#i64.atomic.load16_u(offset: number, ptr: Expression): Expression
  • Module#i64.atomic.load32_u(offset: number, ptr: Expression): Expression
  • Module#i64.atomic.store(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.store8(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.store16(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.store32(offset: number, ptr: Expression, value: Expression): Expression
Atomic read-modify-write operations 🦄
  • Module#i32.atomic.rmw.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
  • Module#i32.atomic.rmw8_u.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw8_u.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw8_u.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw8_u.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw8_u.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw8_u.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw8_u.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
  • Module#i32.atomic.rmw16_u.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw16_u.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw16_u.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw16_u.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw16_u.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw16_u.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i32.atomic.rmw16_u.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
  • Module#i64.atomic.rmw.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
  • Module#i64.atomic.rmw8_u.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw8_u.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw8_u.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw8_u.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw8_u.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw8_u.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw8_u.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
  • Module#i64.atomic.rmw16_u.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw16_u.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw16_u.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw16_u.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw16_u.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw16_u.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw16_u.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
  • Module#i64.atomic.rmw32_u.add(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw32_u.sub(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw32_u.and(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw32_u.or(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw32_u.xor(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw32_u.xchg(offset: number, ptr: Expression, value: Expression): Expression
  • Module#i64.atomic.rmw32_u.cmpxchg(offset: number, ptr: Expression, expected: Expression, replacement: Expression): Expression
Atomic wait and wake operations 🦄
  • Module#i32.wait(ptr: Expression, expected: Expression, timeout: Expression): Expression
  • Module#i64.wait(ptr: Expression, expected: Expression, timeout: Expression): Expression
  • Module#wake(ptr: Expression, wakeCount: Expression): Expression

Expression manipulation

  • getExpressionId(expr: Expression): ExpressionId
    Gets the id (kind) of the specified expression. Possible values are:

    • InvalidId: ExpressionId
    • BlockId: ExpressionId
    • IfId: ExpressionId
    • LoopId: ExpressionId
    • BreakId: ExpressionId
    • SwitchId: ExpressionId
    • CallId: ExpressionId
    • CallImportId: ExpressionId
    • CallIndirectId: ExpressionId
    • GetLocalId: ExpressionId
    • SetLocalId: ExpressionId
    • GetGlobalId: ExpressionId
    • SetGlobalId: ExpressionId
    • LoadId: ExpressionId
    • StoreId: ExpressionId
    • ConstId: ExpressionId
    • UnaryId: ExpressionId
    • BinaryId: ExpressionId
    • SelectId: ExpressionId
    • DropId: ExpressionId
    • ReturnId: ExpressionId
    • HostId: ExpressionId
    • NopId: ExpressionId
    • UnreachableId: ExpressionId
    • AtomicCmpxchgId: ExpressionId
    • AtomicRMWId: ExpressionId
    • AtomicWaitId: ExpressionId
    • AtomicWakeId: ExpressionId
  • getExpressionType(expr: Expression): Type
    Gets the type of the specified expression.

  • getExpressionInfo(expr: Expression: ExpressionInfo
    Obtains information about an expression, always including:

    • Info#id: ExpressionId
    • Info#type: Type

    Additional properties depend on the expression's id and are usually equivalent to the respective parameters when creating such an expression:

    • BlockInfo#name: string
    • BlockInfo#children: Expression[]
    • IfInfo#condition: Expression
    • IfInfo#ifTrue: Expression
    • IfInfo#ifFalse: Expression | null
    • LoopInfo#name: string
    • LoopInfo#body: Expression
    • BreakInfo#name: string
    • BreakInfo#condition: Expression | null
    • BreakInfo#value: Expression | null
    • SwitchInfo#names: string[]
    • SwitchInfo#defaultName: string | null
    • SwitchInfo#condition: Expression
    • SwitchInfo#value: Expression | null
    • CallInfo#target: string
    • CallInfo#operands: Expression[]
    • CallImportInfo#target: string
    • CallImportInfo#operands: Expression[]
    • CallIndirectInfo#target: Expression
    • CallIndirectInfo#operands: Expression[]
    • GetLocalInfo#index: number
    • SetLocalInfo#isTee: boolean
    • SetLocalInfo#index: number
    • SetLocalInfo#value: Expression
    • GetGlobalInfo#name: string
    • SetGlobalInfo#name: string
    • SetGlobalValue#value: Expression
    • LoadInfo#isAtomic: boolean
    • LoadInfo#isSigned: boolean
    • LoadInfo#offset: number
    • LoadInfo#bytes: number
    • LoadInfo#align: number
    • LoadInfo#ptr: Expression
    • StoreInfo#isAtomic: boolean
    • StoreInfo#offset: number
    • StoreInfo#bytes: number
    • StoreInfo#align: number
    • StoreInfo#ptr: Expression
    • StoreInfo#value: Expression
    • ConstInfo#value: number | { low: number, high: number }
    • UnaryInfo#op: number
    • UnaryInfo#value: Expression
    • BinaryInfo#op: number
    • BinaryInfo#left: Expression
    • BinaryInfo#right: Expression
    • SelectInfo#ifTrue: Expression
    • SelectInfo#ifFalse: Expression
    • SelectInfo#condition: Expression
    • DropInfo#value: Expression
    • ReturnInfo#value: Expression | null
    • HostInfo#op: number
    • HostInfo#nameOperand: string | null
    • HostInfo#operands: Expression[]
    • AtomicRMWInfo#op: number
    • AtomicRMWInfo#bytes: number
    • AtomicRMWInfo#offset: number
    • AtomicRMWInfo#ptr: Expression
    • AtomicRMWInfo#value: Expression
    • AtomicCmpxchgInfo#bytes: number
    • AtomicCmpxchgInfo#offset: number
    • AtomicCmpxchgInfo#ptr: Expression
    • AtomicCmpxchgInfo#expected: Expression
    • AtomicCmpxchgInfo#replacement: Expression
    • AtomicWaitInfo#ptr: Expression
    • AtomicWaitInfo#expected: Expression
    • AtomicWaitInfo#timeout: Expression
    • AtomicWaitInfo#expectedType: Type
    • AtomicWakeInfo#ptr: Expression
    • AtomicWakeInfo#wakeCount: Expression

    NopInfo and UnreachableInfo do not include any additional properties.

Relooper

  • 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.

Source maps

  • Module#addDebugInfoFileName(filename: string): number
    Adds a debug info file name to the module and returns its index.

  • Module#getDebugInfoFileName(index: number): string | null
    Gets the name of the debug info file at the specified index.

  • Module#setDebugLocation(func: Function, expr: Expression, fileIndex: number, lineNumber: number, columnNumber: number): void
    Sets the debug location of the specified Expression within the specified Function.

Debugging

  • 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.

Building

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

Keywords

FAQs

Package last updated on 17 Feb 2018

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc