Bare Docs

bare-wasm

WebAssembly JavaScript API for Bare

Documented against v0.1.1
experimental

bare-wasm — WebAssembly JavaScript API for Bare. Each export is the same-named member of the WebAssembly global. Where the global is unavailable, such as when V8 runs without a JIT on iOS, 0.1.0 throws a ReferenceError when it loads. From 0.1.1, the exports still load, but the constructors, the static methods of Module and validate() throw an error with the code WEBASSEMBLY_UNAVAILABLE, and compile() and instantiate() reject with it. Bare 1.36.0 bundles 0.1.0.

npm i bare-wasm

Usage

const { Module, Instance } = require('bare-wasm')

const module = new Module(bytes)
const instance = new Instance(module, imports)

API

wasm

wasm.BufferSource

type BufferSource = ArrayBuffer | ArrayBufferView

The bytes of a WebAssembly module in its binary format.

wasm.compile(bytes: BufferSource): Promise<Module>

Asynchronously compile a module.

Parameters

ParameterTypeDefaultDescription
bytesBufferSource—The binary format of the module.

wasm.Exports

type Exports = Record<string, ExportValue>

The exports of an instance, keyed by name.

wasm.ExportValue

type ExportValue = Function | Global | Memory | Table | Tag

A value exported by an instance.

wasm.ImportExportKind

type ImportExportKind = 'function' | 'table' | 'memory' | 'global' | 'tag'

The kind of an import or export of a module.

wasm.Imports

type Imports = Record<string, ModuleImports>

The imports of a module, keyed by import module name.

wasm.ImportValue

type ImportValue = ExportValue | number | bigint

A value that may be provided to satisfy an import.

wasm.instantiate(bytes: BufferSource, imports?: Imports): Promise<InstantiatedSource>

Asynchronously compile and instantiate a module.

Overloads:

wasm.instantiate(bytes: BufferSource, imports?: Imports): Promise<InstantiatedSource>
wasm.instantiate(module: Module, imports?: Imports): Promise<Instance>

Parameters

ParameterTypeDefaultDescription
bytesBufferSource—The binary format of the module.
imports?Imports—The values to satisfy the imports of the module with.

wasm.ModuleImports

type ModuleImports = Record<string, ImportValue>

The imports of a single import module, keyed by name.

wasm.TableKind

type TableKind = 'externref' | 'anyfunc'

The type of the elements of a table.

wasm.validate(bytes: BufferSource): boolean

Check whether bytes is a valid module.

Parameters

ParameterTypeDefaultDescription
bytesBufferSource——

wasm.ValueType

type ValueType = 'i32' | 'i64' | 'f32' | 'f64' | 'v128' | 'externref' | 'anyfunc'

The type of a WebAssembly value.

Module

new Module(bytes: BufferSource)

Synchronously compile a module.

Parameters

ParameterTypeDefaultDescription
bytesBufferSource—The binary format of the module.

Throws

  • CompileError — thrown if bytes is not a valid module.

Module.customSections(module: Module, sectionName: string): ArrayBuffer[]

Get the contents of the custom sections of module named sectionName.

Parameters

ParameterTypeDefaultDescription
moduleModule——
sectionNamestring——

Module.exports(module: Module): ModuleExportDescriptor[]

Get descriptors of the exports of module.

Parameters

ParameterTypeDefaultDescription
moduleModule——

Module.imports(module: Module): ModuleImportDescriptor[]

Get descriptors of the imports of module.

Parameters

ParameterTypeDefaultDescription
moduleModule——

Instance

new Instance(module: Module, imports?: Imports)

Synchronously instantiate a module.

Parameters

ParameterTypeDefaultDescription
moduleModule—The module to instantiate.
imports?Imports—The values to satisfy the imports of the module with.

Throws

  • LinkError — thrown if the imports of the module cannot be satisfied.
  • RuntimeError — thrown if the start function of the module traps.

exports: Exports

The exports of the instance.

Memory

new Memory(descriptor: MemoryDescriptor)

Create a memory of descriptor.initial pages.

Parameters

ParameterTypeDefaultDescription
descriptorMemoryDescriptor——

buffer: ArrayBuffer | SharedArrayBuffer

The buffer backing the memory, which is detached and replaced whenever it grows.

grow(delta: number): number

Grow the memory.

Parameters

ParameterTypeDefaultDescription
deltanumber—The number of pages of 64 KiB to grow the memory by.

Returns number — The previous size of the memory in pages.

Throws

  • RangeError — thrown if the memory cannot grow by delta pages.

Table

new Table(descriptor: TableDescriptor, value?: any)

Create a table of descriptor.initial elements, each set to value.

Parameters

ParameterTypeDefaultDescription
descriptorTableDescriptor—The type and limits of the table.
value?any—The value to initialize the elements of the table with.

get(index: number): any

Get the element at index.

Parameters

ParameterTypeDefaultDescription
indexnumber——

grow(delta: number, value?: any): number

Grow the table.

Parameters

ParameterTypeDefaultDescription
deltanumber—The number of elements to grow the table by.
value?any—The value to initialize the new elements with.

Returns number — The previous number of elements of the table.

Throws

  • RangeError — thrown if the table cannot grow by delta elements.

length: number

The number of elements of the table.

set(index: number, value?: any): void

Set the element at index to value.

Parameters

ParameterTypeDefaultDescription
indexnumber——
value?any——

Global

new Global(descriptor: GlobalDescriptor, value?: T)

Create a global holding value.

Parameters

ParameterTypeDefaultDescription
descriptorGlobalDescriptor—The type and mutability of the global.
value?T—The initial value of the global.

value: T

The value of the global.

valueOf(): T

Get the value of the global.

Tag

new Tag(type: TagType)

Create an exception tag.

Parameters

ParameterTypeDefaultDescription
typeTagType——

Exception

new Exception(tag: Tag, payload: any[], options?: ExceptionOptions)

Create an exception of tag carrying payload.

Parameters

ParameterTypeDefaultDescription
tagTag—The tag identifying the type of the exception.
payloadany[]—The values carried by the exception, matching the parameters of tag.
options?ExceptionOptions—Options for the exception.

getArg(tag: Tag, index: number): any

Get a value carried by the exception.

Parameters

ParameterTypeDefaultDescription
tagTag—The tag the exception must have.
indexnumber—The index of the value in the payload.

Throws

  • TypeError — thrown if the exception does not have tag.

is(tag: Tag): boolean

Check whether the exception has tag.

Parameters

ParameterTypeDefaultDescription
tagTag——

stack: string

The stack trace of the exception, if requested with traceStack.

Types

ModuleExportDescriptor

interface ModuleExportDescriptor {
  name: string
  kind: ImportExportKind
}

An export of a module, as listed by Module.exports().

ModuleImportDescriptor

interface ModuleImportDescriptor {
  module: string
  name: string
  kind: ImportExportKind
}

An import of a module, as listed by Module.imports().

MemoryDescriptor

interface MemoryDescriptor {
  initial: number
  maximum?: number
  shared?: boolean
}

The size, in 64 KiB pages, and sharing of a memory.

TableDescriptor

interface TableDescriptor {
  element: TableKind
  initial: number
  maximum?: number
}

The element type and size of a table.

GlobalDescriptor

interface GlobalDescriptor {
  value: ValueType
  mutable?: boolean
}

The value type and mutability of a global.

TagType

interface TagType {
  parameters: ValueType[]
}

The parameter types of an exception tag.

ExceptionOptions

interface ExceptionOptions {
  traceStack?: boolean
}

Options for an Exception.

InstantiatedSource

interface InstantiatedSource {
  module: Module
  instance: Instance
}

The result of instantiating the bytes of a module: the compiled module and its instance.

Classes

CompileError

class CompileError {
}

An error thrown when a module fails to decode or validate.

LinkError

class LinkError {
}

An error thrown when the imports of a module cannot be satisfied.

RuntimeError

class RuntimeError {
}

An error thrown when WebAssembly code traps.

See also

Last updated on

Was this helpful?

On this page