QuickBEAM.WASM (QuickBEAM v0.10.1)

Copy Markdown View Source

WebAssembly runtime for the BEAM.

Compile .wasm binaries and run them as supervised WASM instances, or use disasm/1 to decode them into structured Elixir data.

Quick start

wasm = File.read!("add.wasm")
{:ok, pid} = QuickBEAM.WASM.start(module: wasm)
{:ok, 42} = QuickBEAM.WASM.call(pid, "add", [40, 2])
QuickBEAM.WASM.stop(pid)

Supervision

children = [
  {QuickBEAM.WASM, name: :renderer, module: File.read!("priv/wasm/md.wasm")}
]
Supervisor.start_link(children, strategy: :one_for_one)

QuickBEAM.WASM.call(:renderer, "render", [...])

Disassembly

{:ok, mod} = QuickBEAM.WASM.disasm(wasm_bytes)
hd(mod.functions).opcodes
# [{0, :local_get, 0}, {2, :local_get, 1}, {4, :i32_add}, {5, :end}]

Options

  • :module — WASM binary (required)
  • :name — GenServer name registration
  • :stack_size — execution stack in bytes (default: 65536)
  • :heap_size — auxiliary heap in bytes (default: 65536)

Summary

Functions

Call an exported WASM function by name.

Disassemble a .wasm binary into a %QuickBEAM.WASM.Module{} struct.

List exports from a .wasm binary or a parsed module.

List imports from a .wasm binary or a parsed module.

Grow the memory of a WASM instance by delta pages (64KB each).

Get the current memory size of a WASM instance in bytes.

Read bytes from a WASM instance's linear memory.

Start a WASM instance.

Start a WASM instance linked to the calling process.

Stop a WASM instance.

Validate a .wasm binary (structural validation).

Write bytes to a WASM instance's linear memory.

Types

instance()

@type instance() :: GenServer.server()

Functions

call(instance, func_name, params \\ [])

@spec call(instance(), String.t(), [number() | integer()]) ::
  {:ok, term()} | {:error, String.t()}

Call an exported WASM function by name.

{:ok, 42} = QuickBEAM.WASM.call(pid, "add", [40, 2])

disasm(wasm_bytes)

@spec disasm(binary()) :: {:ok, QuickBEAM.WASM.Module.t()} | {:error, String.t()}

Disassemble a .wasm binary into a %QuickBEAM.WASM.Module{} struct.

Decodes all sections including function bodies with opcodes. Does not require a running instance.

{:ok, mod} = QuickBEAM.WASM.disasm(File.read!("add.wasm"))
hd(mod.functions).opcodes
# [{0, :local_get, 0}, {2, :local_get, 1}, {4, :i32_add}, {5, :end}]

exports(mod)

@spec exports(binary() | QuickBEAM.WASM.Module.t()) ::
  [QuickBEAM.WASM.Module.export_desc()] | {:error, String.t()}

List exports from a .wasm binary or a parsed module.

imports(mod)

@spec imports(binary() | QuickBEAM.WASM.Module.t()) ::
  [QuickBEAM.WASM.Module.import_desc()] | {:error, String.t()}

List imports from a .wasm binary or a parsed module.

memory_grow(instance, delta)

@spec memory_grow(instance(), non_neg_integer()) ::
  {:ok, non_neg_integer()} | {:error, String.t()}

Grow the memory of a WASM instance by delta pages (64KB each).

memory_size(instance)

@spec memory_size(instance()) :: {:ok, non_neg_integer()}

Get the current memory size of a WASM instance in bytes.

read_memory(instance, offset, length)

@spec read_memory(instance(), non_neg_integer(), non_neg_integer()) ::
  {:ok, binary()} | {:error, String.t()}

Read bytes from a WASM instance's linear memory.

start(opts)

@spec start(keyword()) :: GenServer.on_start()

Start a WASM instance.

{:ok, pid} = QuickBEAM.WASM.start(module: wasm_bytes)
{:ok, 42} = QuickBEAM.WASM.call(pid, "add", [40, 2])

start_link(opts)

@spec start_link(keyword()) :: GenServer.on_start()

Start a WASM instance linked to the calling process.

Same as start/1 — the instance is always linked. Use in supervision trees via child_spec/1.

stop(instance)

@spec stop(instance()) :: :ok

Stop a WASM instance.

validate(wasm_bytes)

@spec validate(binary()) :: boolean()

Validate a .wasm binary (structural validation).

QuickBEAM.WASM.validate(File.read!("add.wasm"))  # => true
QuickBEAM.WASM.validate("not wasm")               # => false

write_memory(instance, offset, data)

@spec write_memory(instance(), non_neg_integer(), binary()) ::
  :ok | {:error, String.t()}

Write bytes to a WASM instance's linear memory.