Please note that the Binaryen API is evolving fast. Definitions and documentation provided by the C++ codebase donβt always immediately find their way here into TypeScript. If you rely on Binaryen.TS and spot an issue, please consider sending a PR our way. Thank you!
Import binaryen as a namespace:
import * as binaryen from "binaryen.ts";
or import ES module components individually:
import {type Type, type ExpressionRef, i32} from "binaryen.ts";
Type: a WASM type; the type of the constants named i32, i64, etc.HeapType: a WASM heap type created in a TypeBuilderPackedType: an allowed type of a struct or array field; one of three constants: notPacked, i8, i16ExpressionRef: an expression, e.g. the type of i32.const()TagRefGlobalRefMemoryRefTableRefFunctionRefDataSegmentRefElementSegmentRefImportRefExportRefunreachable: type of an unreachable instruction (stack effect [t*] -> [t*])none: void type (stack effect [t*] -> []); not to be confused with WASMβs heap type called noneauto: special type used in ExpressionBuilder#block() exclusively; automatically detects a blockβs result type based on its contentsi32: 32-bit integeri64: 64-bit integerf32: 32-bit floatf64: 64-bit floatv128: 128-bit vector (SIMD)anyref: (ref null any)eqref: (ref null eq)i31ref: (ref null i31)structref: (ref null struct)arrayref: (ref null array)funcref: (ref null func)exnrefexternref: (ref null extern)nullref: (ref null none)nullfuncref: (ref null nofunc)nullexnrefnullexternref: (ref null noextern)stringref: π± planned for (ref null string)notPacked (PackedType): unaltered type in the struct/array fieldi8 (PackedType): 8-bit integeri16 (PackedType): 16-bit integerExpressionId: an enumeration of values returned by getExpressionId()
SideEffect: an enumeration of values returend by getSideEffects()ExternalKind: an enumeration of kinds of exports; serves as type of the Module.Export#kind fieldMemoryOrder: an enumeration of values used in atomic expression methodsFunctions (see generated docs for descriptions):
emitText(expr: ExpressionRef): stringreadBinary(data: Uint8Array): ModulereadBinaryWithFeatures(data: Uint8Array, features: Feature): ModuleparseText(text: string): Moduleexit(status: number): voidcreateType(types: readonly Type[]): TypeexpandType(typ: Type): Type[]getTypeFromHeapType(heapType: HeapType, nullable: boolean): TypegetHeapType(typ: Type): HeapTypegetExpressionId(expr: ExpressionRef): ExpressionIdgetExpressionType(expr: ExpressionRef): TypegetExpressionInfo(expr: ExpressionRef): ExpressiongetSideEffects(expr: ExpressionRef, mod: Module): SideEffectcopyExpression(expr: ExpressionRef, mod: Module): ExpressionRefObjects:
settings: global settings manager; see SettingsService docsProperties of Module as a namespace:
new Module.Tag(ref: TagRef): an object containing information about a Tagnew Module.Global(ref: GlobalRef): an object containing information about a Globalnew Module.Memory(mod: Module, name: string): an object containing information about a Memorynew Module.Table(ref: TableRef): an object containing information about a Tablenew Module.Function(ref: FunctionRef): an object containing information about a Functionnew Module.DataSegment(mod: Module, ref: DataSegmentRef): an object containing information about a Data Segmentnew Module.ElementSegment(ref: ElementSegmentRef): an object containing information about an Element Segmentnew Module.Import(): an object containing information about an Import (π± empty for now)new Module.Export(ref: ExportRef): an object containing information about an ExportProperties of Module instances (see full list of methods in generated docs):
Module#wasm: build WASM expressionsModule#tags: Tag manipulationModule#globals: Global manipulationModule#memories: Memory manipulationModule#tables: Table manipulationModule#functions: Function manipulationModule#dataSegments: Data Segment manipulationModule#elementSegments: Element Segment manipulationModule#imports: Import manipulationModule#exports: Export manipulationModule methods (see signatures and descriptions in generated docs):
.emitText().emitStackIR().emitAsmjs().emitBinary().interpret().dispose().validate().optimize().optimizeFunction().runPasses().runPassesOnFunction().addDebugInfoFileName().getDebugInfoFileName().setDebugLocation().setTypeName().setFieldName().addCustomSection().updateMaps()Each of these functions is bound to Module#wasm (of type ExpressionBuilder) and returns an ExpressionRef.
See the generated ExpressionBuilder docs for all available functions and details.
Note: For brevity, glob-like syntax _{s,u} is used to mean β_s and _uβ.
.nop().unreachable().drop().select().block().loop().if().br(), .br_if(), .br_table().br_on_null(), .br_on_non_null(), .br_on_cast(), .br_on_cast_fail().call(), .call_ref(), .call_indirect().return(), .return_call(), .return_call_ref(), .return_call_indirect().throw(), .throw_ref(), .try_table().catch(), .catch_ref(), .catch_all(), .catch_all_ref().local.get().local.set().local.tee().global.get().global.set().table.get(), .table.set(), .table.size(), .table.grow().memory.size(), .memory.grow(), .memory.fill(), .memory.copy(), .memory.init(),.elem.drop(), .data.drop().ref.func(), .ref.null(), .ref.is_null(), .ref.as_non_null(), .ref.eq(), .ref.test(), .ref.cast().ref.i31(), i31.get_{s,u}().extern.convert_any(), .any.convert_extern().tuple.make().tuple.extract().struct.new(), .struct.new_default().struct.get(), .struct.get_{s,u}().struct.set().array.new(), .array.new_default(), .array.new_fixed(), .array.new_data(), .array.new_elem().array.get(), .array.get_{s,u}().array.set().array.len().array.fill().array.copy().array.init_data(), .array.init_elem().{i32,i64}.load(), .{i32,i64}.load8_{s,u}(), .{i32,i64}.load16_{s,u}(), .i64.load32_{s,u}().{i32,i64}.store(), .{i32,i64}.store8(), .{i32,i64}.store16(), .i64.store32().{i32,i64}.const().{i31,i32}.clz(), .{i32,i64}.ctz(), .{i32,i64}.popcnt().{i32,i64}.extend8_s(), .{i32,i64}.extend16_s(), .i64.extend32_s().{i31,i32}.add(), .{i32,i64}.sub(), .{i32,i64}.mul(), .{i31,i32}.div_{s,u}(), .{i32,i64}.rem_{s,u}().{i31,i32}.and(), .{i32,i64}.or(), .{i32,i64}.xor(), .{i31,i32}.shl(), .{i31,i32}.shr{s,u}(), .{i32,i64}.rotl(), .{i32,i64}.rotr().{i32,i64}.eqz().{i32,i64}.eq(), .{i32,i64}.ne().{i32,i64}.lt_{s,u}(), .{i32,i64}.gt_{s,u}(), .{i32,i64}.le_{s,u}(), .{i32,i64}.ge_{s,u}().i32.wrap_i64(), .i64.extend_i32_{s,u}().i32.trunc_f32_{s,u}(), .i32.trunc_f64_{s,u}().i64.trunc_f32_{s,u}(), .i64.trunc_f64_{s,u}().i32.trunc_sat_f32_{s,u}(), .i32.trunc_sat_f64_{s,u}().i64.trunc_sat_f32_{s,u}(), .i64.trunc_sat_f64_{s,u}().i32.reinterpret_f32(), .i64.reinterpret_f64().add128(), .sub128(), .mul_wide_{s,u}().{f32,f64}.load(), .{f32,f64}.store().{f32,f64}.const().{f32,f64}.abs(), .{f32,f64}.neg(), .{f32,f64}.sqrt(), .{f32,f64}.ceil(), .{f32,f64}.floor(), .{f32,f64}.trunc(), .{f32,f64}.nearest().{f32,f64}.add(), .{f32,f64}.sub(), .{f32,f64}.mul(), .{f32,f64}.div(), .{f32,f64}.min(), .{f32,f64}.max(), .{f32,f64}.copysign().{f32,f64}.eq(), .{f32,f64}.lt(), .{f32,f64}.gt(), .{f32,f64}.le(), .{f32,f64}.ge().{f32,f64}.convert_i32_s(), .{f32,f64}.convert_i32_u(), .{f32,f64}.convert_i64_s(), .{f32,f64}.convert_i64_u().f32.demote_f64(), .f64.promote_f32().v128.load().v128.load{8x8,16x4,32x2}_{s,u}().v128.load{8,16,32,64}_splat().v128.load{32,64}_zero().v128.load{8,16,32,64}_lane().v128.store().v128.store{8,16,32,64}_lane().v128.const().v128.not().v128.and(), .v128.andnot(), .v128.or(), .v128.xor().v128.bitselect().v128.anytrue().{i8x16,i16x8,i32x4,i64x2}.abs(), .{i8x16,i16x8,i32x4,i64x2}.neg(), .i8x16.popcnt().{i8x16,i16x8,i32x4,i64x2}.add().{i8x16,i16x8,i32x4,i64x2}.sub().{i8x16,i16x8}.add_sat_{s,u}().{i8x16,i16x8}.sub_sat_{s,u}().{i16x8,i32x4,i64x2}.mul().{i8x16,i16x8}.avgr_u().i16x8.q15mulr_sat_s().i16x8.relaxed_q15mulr_s().{i8x16,i16x8,i32x4}.min{s,u}().{i8x16,i16x8,i32x4}.max{s,u}().{i8x16,i16x8,i32x4,i64x2}.relaxed_laneselect().{i8x16,i16x8,i32x4,i64x2}.all_true().{i8x16,i16x8,i32x4,i64x2}.eq().{i8x16,i16x8,i32x4,i64x2}.ne().{i8x16,i16x8,i32x4}.lt_{s,u}(), .i64x2.lt_s().{i8x16,i16x8,i32x4}.gt_{s,u}(), .i64x2.gt_s().{i8x16,i16x8,i32x4}.le_{s,u}(), .i64x2.le_s().{i8x16,i16x8,i32x4}.ge_{s,u}(), .i64x2.ge_s().{i8x16,i16x8,i32x4,i64x2}.shl().{i8x16,i16x8,i32x4,i64x2}.shr{s,u}().{i8x16,i16x8,i32x4,i64x2}.bitmask().i8x16.swizzle(), .i8x16.relaxed_swizzle().i8x16.shuffle().i16x8.extadd_pairwise_i8x16_{s,u}(), .i32x4.extadd_pairwise_i16x8_{s,u}().i16x8.extmul_{low,high}_i8x16_{s,u}(), .i32x4.extmul_{low,high}_i16x8_{s,u}(), .i64x2.extmul_{low,high}_i32x4_{s,u}().i32x4.dot_i16x8_s().i16x8.relaxed_dot_i8x16_i7x16_s().i32x4.relaxed_dot_i8x16_i7x16_add_s().i8x16.narrow_i16x8_{s,u}(), .i16x8.narrow_i32x4_{s,u}().i16x8.extend_{low,high}_i8x16_{s,u}(), .i32x4.extend_{low,high}_i16x8_{s,u}(), .i64x2.extend_{low,high}_i32x4_{s,u}().i32x4.trunc_sat_f32x4_{s,u}(), .i32x4.trunc_sat_f64x2_{s,u}_zero().i32x4.relaxed_trunc_f32x4_{s,u}(), .i32x4.relaxed_trunc_f64x2_{s,u}_zero().{f32x4,f64x2}.abs(), .{f32x4,f64x2}.neg(), .{f32x4,f64x2}.sqrt(), .{f32x4,f64x2}.ceil(), .{f32x4,f64x2}.floor(), .{f32x4,f64x2}.trunc(), .{f32x4,f64x2}.nearest().{f32x4,f64x2}.add(), .{f32x4,f64x2}.sub(), .{f32x4,f64x2}.mul(), .{f32x4,f64x2}.div(), .{f32x4,f64x2}.min(), .{f32x4,f64x2}.max(), .{f32x4,f64x2}.pmin(), .{f32x4,f64x2}.pmax(), .{f32x4,f64x2}.relaxed_min(), .{f32x4,f64x2}.relaxed_max().{f32x4,f64x2}.relaxed_madd(), .{f32x4,f64x2}.relaxed_nmadd().{f32x4,f64x2}.eq(), .{f32x4,f64x2}.ne(), .{f32x4,f64x2}.lt(), .{f32x4,f64x2}.gt(), .{f32x4,f64x2}.le(), .{f32x4,f64x2}.ge().f32x4.convert_i32x4_{s,u}(), .f64x2.convert_low_i32x4_{s,u}().f32x4.demote_f64x2_zero(), .f64x2.promote_low_f32x4().{i8x16,i16x8,i32x4,i64x2,f32x4,f64x2}.splat().{i8x16,i16x8}.extract_lane_{s,u}(), .{i32x4,i64x2,f32x4,f64x2}.extract_lane().{i8x16,i16x8,i32x4,i64x2,f32x4,f64x2}.replace_lane()Expression info classes all live under the global expressions namespace.
They can be used to inspect and manipulate expressions.
See generated docs for fields, methods, and descriptions of each.
expressions.Expression (root class)expressions.Dropexpressions.Selectexpressions.Blockexpressions.Loopexpressions.Breakexpressions.LocalGetexpressions.LocalSetexpressions.GlobalGetexpressions.GlobalSetexpressions.ConstEnum names have been singularized.
ExpressionIds β ExpressionIdSideEffects β SideEffectExternalKinds β ExternalKindFeatures β Feature*Info types have been merged with their respective classes in the Module namespace.
TagInfo β Module.TagGlobalInfo β Module.GlobalMemoryInfo β Module.MemoryTableInfo β Module.TableFunctionInfo β Module.FunctionElementSegmentInfo β Module.ElementSegmentExportInfo β Module.ExportExpressionInfo and related types are now classes in the expressions namespace:
ExpressionInfo β expressions.ExpressionBlockInfo β expressions.BlockLoopInfo β expressions.LoopIfInfo β expressions.If β has been removed.MemorySegmentInfo
Module components previously at the top level have been moved under the Module namespace.
Function β Module.FunctionTable β Module.TableMost get*Info() functions have been replaced by their corresponding class constructors.
getTagInfo(tag) β new Module.Tag(tag);getGlobalInfo(global) β new Module.Global(global)getTableInfo(table) β new Module.Table(table)getFunctionInfo(func) β new Module.Function(func)getElementSegmentInfo(segment) β new Module.ElementSegment(segment)getExportInfo(xport) β new Module.Export(xport)Module#getMemoryInfo(name) has not changed.Module#getDataSegmentInfo(name) has not changed.getExpressionInfo(expr) has not changed.getMemorySegmentInfo()Most of the Module classβs instance methods relating to module component manipulation have been moved.
Module#addTag() β Module#tags.add()Module#setGlobal() β Module#globals.set()Module#removeTable() β Module#tables.remove()Thing and things are component types (globals, tables, functions, etc.)):
Module#addThing() β Module#things.add()Module#getThing() β Module#things.get()Module#getThingByIndex() β Module#things.getByIndex()Module#getNumThings() β Module#things.count()Module#removeThing() β Module#things.remove()Module#addThingImport() β Module#imports.addThing()Module#addThingExport() β Module#exports.addThing()Some of Moduleβs instance methods have been converted into getters/setters:
Module#getStart() β Module#startModule#setStart() β Module#startModule#getFeatures() β Module#featuresModule#setFeatures() β Module#featuresGlobal getSideEffects(expr, mod) has been moved to Module#getSideEffects() where it lives alongside Module#copyExpression().
All expression creation methods (.nop(), .drop(), .block(), .call(), etc.) directly on Module
were functions for building expressions, and have migrated to Module#wasm, an Expression Builder.
All βtypeβ properties (.i32, .i64, etc) on Module previously served as namespaces containing similar functions.
(E.g., Module#i32.add() produced an (i32.add) WASM instruction.) These also have all migrated to Module#wasm.
These βtypeβ properties also each contained its own .pop() method, which didnβt build a WASM expression,
but was a pseudo-instruction enabling Binaryen to reason about multiple values on the stack.
They have been combined into one method on Module, Module#pop(t: Type), where t is one of the corresponding type namespaces.
These methods were previously directly on the Module class, and have been moved to Module#wasm.
Some of them have also been renamed to align with the WASM spec.
Note: To improve readability, assume all methods written in this section are bound to an Expression Builder (an object returned by Module#wasm).
.break() β .br().switch() β .br_table().callIndirect() β .call_indirect().returnCall() β .return_call().returnCallIndirect() β .return_call_indirect().rethrow() β .throw_ref().try() β .try_table().{struct,array}.get() no longer take the isSigned argument. For packed signed/unsigned types, use .{struct,array}.get_{s,u}() respectively.
Many numeric methods have been renamed:
.i32.wrap() β .i32.wrap_i64().i32.trunc_s.f32() β .i32.trunc_f32_s().i32.trunc_s.f64() β .i32.trunc_f64_s().i32.trunc_u.f32() β .i32.trunc_f32_u().i32.trunc_u.f64() β .i32.trunc_f64_u().i32.trunc_s_sat.f32() β .i32.trunc_sat_f32_s().i32.trunc_s_sat.f64() β .i32.trunc_sat_f64_s().i32.trunc_u_sat.f32() β .i32.trunc_sat_f32_u().i32.trunc_u_sat.f64() β .i32.trunc_sat_f64_u().i32.reinterpret() β .i32.reinterpret_f32().i64.extend_s() β .i64.extend_i32_s().i64.extend_u() β .i64.extend_i32_u().i64.trunc_s.f32() β .i64.trunc_f32_s().i64.trunc_s.f64() β .i64.trunc_f64_s().i64.trunc_u.f32() β .i64.trunc_f32_u().i64.trunc_u.f64() β .i64.trunc_f64_u().i64.trunc_s_sat.f32() β .i64.trunc_sat_f32_s().i64.trunc_s_sat.f64() β .i64.trunc_sat_f64_s().i64.trunc_u_sat.f32() β .i64.trunc_sat_f32_u().i64.trunc_u_sat.f64() β .i64.trunc_sat_f64_u().i64.reinterpret() β .i64.reinterpret_f64().f32.convert_s.i32() β .f32.convert_i32_s().f32.convert_s.i64() β .f32.convert_i64_s().f32.convert_u.i32() β .f32.convert_i32_u().f32.convert_u.i64() β .f32.convert_i64_u().f32.reinterpret() β .f32.reinterpret_i32().f32.demote() β .f32.demote_f64().f64.convert_s.i32() β .f64.convert_i32_s().f64.convert_s.i64() β .f64.convert_i64_s().f64.convert_u.i32() β .f64.convert_i32_u().f64.convert_u.i64() β .f64.convert_i64_u().f64.reinterpret() β .f64.reinterpret_i64().f64.promote() β .f64.promote_f32()All optimization pass settings have been moved to the global settings object.
get/set with zero/one argument have been converted to getters/setters respectively.
{get,set}OptimizeLevel() β settings.optimizeLevel{get,set}ShrinkLevel() β settings.shrinkLevel{get,set}DebugInfo() β settings.debugInfo{get,set}TrapsNeverHappen() β settings.trapsNeverHappen{get,set}ClosedWorld() β settings.closedWorld{get,set}LowMemoryUnused() β settings.lowMemoryUnused{get,set}ZeroFilledMemory() β settings.zeroFilledMemory{get,set}FastMath() β settings.fastMath{get,set}GenerateStackIR() β settings.generateStackIR{get,set}OptimizeStackIR() β settings.optimizeStackIR{get,set}AlwaysInlineMaxSize() β settings.alwaysInlineMaxSize{get,set}FlexibleInlineMaxSize() β settings.flexibleInlineMaxSize{get,set}OneCallerInlineMaxSize() β settings.oneCallerInlineMaxSize{get,set}AllowInliningFunctionsWithLoops() β settings.allowInliningFunctionsWithLoopsgetPassArgument() β settings.getPassArgument()setPassArgument() β settings.setPassArgument()clearPassArguments() β settings.clearPassArguments()hasPassToSkip() β settings.hasPassToSkip()addPassToSkip() β settings.addPassToSkip()clearPassesToSkip() β settings.clearPassesToSkip()