beamr
A Rust implementation of the BEAM virtual machine, targeting Gleam as its primary source language. Load compiled .beam bytecode and execute it with preemptive scheduling, per-process isolation, garbage collection, and OTP-style supervision — no Erlang runtime required.
Usage
Add beamr to your Cargo.toml:
[]
= "0.1"
Running a Gleam module
use Arc;
use AtomTable;
use load_module;
use ModuleRegistry;
use ;
use ;
use ExitReason;
// Set up the VM
let atom_table = with_common_atoms;
let mut bif_registry = new;
register_gate1_bifs.unwrap;
// Load a .beam file
let bytes = read.unwrap;
let module_registry = new;
let = load_module.unwrap;
// Spawn and run
let registry = new;
let scheduler = new.unwrap;
let main_fn = atom_table.intern;
let pid = scheduler.spawn.unwrap;
let = scheduler.run_until_exit;
scheduler.shutdown;
assert_eq!;
Key types
| Type | Description |
|---|---|
Scheduler |
Preemptive scheduler with work-stealing. Spawn processes, run them, deliver async results. |
Term |
Tagged 64-bit BEAM term — integers, atoms, binaries, tuples, lists, maps, pids, floats, closures. |
AtomTable |
Interned string table for atoms. Thread-safe, used throughout the VM. |
ModuleRegistry |
Loaded module storage. Modules are registered here during loading and looked up during execution. |
BifRegistryImpl |
Registry of native Rust functions callable from BEAM bytecode. |
Scheduler API
// Spawn a process calling module:function(args)
let pid = scheduler.spawn?;
// Block until a process exits, returning its exit reason and result
let = scheduler.run_until_exit;
// Deliver an async result to a suspended process (for NIF bridges)
scheduler.wake_with_result;
// Kill a process from the host side
scheduler.terminate_process;
// Configure I/O output destination
scheduler.set_output_sink;
// Clean shutdown
scheduler.shutdown;
I/O sink
By default, beamr discards all I/O output (NullSink). To capture or redirect output, implement the IoSink trait:
use IoSink;
;
scheduler.set_output_sink;
Feature flags
| Flag | Description |
|---|---|
json |
Enables bidirectional Term to serde_json::Value conversion. Adds base64 and serde_json dependencies. |
[]
= { = "0.1", = ["json"] }
What's included
- Bytecode interpreter: OTP 26
.beamformat, covering the opcode subset Gleam generates - Term system: Full BEAM term representation with 64-bit tagged pointers
- Preemptive scheduler: Configurable thread pool, work-stealing, reduction counting, dirty schedulers
- Garbage collector: Generational copying GC with Fibonacci heap growth
- Process primitives: Spawn, link, monitor, exit signals, process registry
- Supervision: OTP-style links, monitors, restart-on-crash
- Mailboxes: Lock-free with selective receive
- 200+ native BIFs: erlang, lists, maps, string, binary, io, math, and all Gleam stdlib FFI modules
License
Apache-2.0