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
@type instance() :: GenServer.server()
Functions
Call an exported WASM function by name.
{:ok, 42} = QuickBEAM.WASM.call(pid, "add", [40, 2])
@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}]
@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.
@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.
@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).
@spec memory_size(instance()) :: {:ok, non_neg_integer()}
Get the current memory size of a WASM instance in bytes.
@spec read_memory(instance(), non_neg_integer(), non_neg_integer()) :: {:ok, binary()} | {:error, String.t()}
Read bytes from a WASM instance's linear memory.
@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])
@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.
@spec stop(instance()) :: :ok
Stop a WASM instance.
Validate a .wasm binary (structural validation).
QuickBEAM.WASM.validate(File.read!("add.wasm")) # => true
QuickBEAM.WASM.validate("not wasm") # => false
@spec write_memory(instance(), non_neg_integer(), binary()) :: :ok | {:error, String.t()}
Write bytes to a WASM instance's linear memory.