kohltastrophe/ouro
Self-hosted Luau 0.748 lexer, parser, bytecode compiler, and sandboxed register VM in pure Luau.
Parse. Compile. Execute.
Ouro is a self-hosted Luau 0.748 lexer, lossless parser, bytecode compiler, and sandboxed register VM written in pure --!strict Luau. It runs on Roblox and standalone Luau without FFI, loadstring, or native code-loading services.
Run untrusted Luau code safely in a few lines with Ouro.run or build a reusable policy with Ouro.sandbox. Both default to the "balanced" safety preset (isolated pure-Luau libraries, instruction/call/time budgets, string/table/pattern/memory limits, and clamped compiler directives) and return pcall-style (ok, ...resultsOrErr) tuples so guest errors never crash the host.
--!strict
local Ouro = require("./ouro")
-- One-shot safe execution (isolated env + default budgets & resource limits)
local ok, result = Ouro.run("local x = ...; return x * 2 + 2", {
preset = "strict",
}, 20)
assert(ok and result == 42)
-- Reusable policy with host APIs and per-script isolated forks
local base = Ouro.sandbox({
preset = "strict",
globals = { clamp = math.clamp },
})
local playerEnv = base:fork()
local runOk, value = playerEnv:run("return clamp(150, 0, 100)")
assert(runOk and value == 100)
For zero-overhead execution of trusted or pre-budgeted code, Ouro.load, Ouro.VM.loadBytecode, and Ouro.VM.loadProto run on the unguarded @native dispatch loop unless budgets or hooks are explicitly configured. See Sandboxed VM, Security & Sandbox, and the Cookbook.
Ouro.parse lowers Luau syntax, type annotations, and .d.luau declarations into flat numeric arenas with zero-based half-open byte offsets. Slice node source with parsed:text(nodeId), format human-readable diagnostics with parsed:formatDiagnostics(), enable lossless = true for exact reconstruct() output, or materialize nested tables with Ouro.Ast.toTables.
--!strict
local Ouro = require("./ouro")
local source = "-- keep comments\nlocal x: number = 20 + 22\nreturn x"
local parsed = Ouro.parse(source, { profile = "studio", lossless = true })
if parsed.status == "ok" then
assert(parsed:kindName(parsed.root) == "AstStatBlock")
assert(parsed:reconstruct() == source)
else
print(parsed:formatDiagnostics())
end
See Parsing & AST for Profiles, parseExpression, parseType, and cooperative yielding with Ouro.newContext.
--!strict
local Ouro = require("./ouro")
local bytecode = assert(Ouro.compile("local a, b = ...\nreturn a * b + 2", {
optimizationLevel = 2,
}))
local listing = assert(Ouro.Bytecode.disassemble(bytecode))
assert(#listing > 0)
Ouro.compile emits binary Luau bytecode (v14 default, v100 for classes) across optimization levels 0, 1, and 2, matching upstream constant folding, builtin fastcalls, inlining, and loop specialization. Ouro.Bytecode includes serialize, deserialize, verify (v3–v14), and disassemble, all returning uniform (value?, err?) tuples. See Compiling & Bytecode.
Ouro pairs dual @native fast and guarded VM dispatch loops with lazy submodule loading: root require loads types and a thin facade, deferring Parser, Compiler, Bytecode, and VM until accessed. The Benchmarks workflow verifies host parity across every workload before timing --codegen -O2, -O2 interpreter execution, O2 compilation, and cold startup against Fiu and LuauCeption. See Benchmarks for full charts and methodology.
Pipeline & Entrypoint Guide
Luau Source ──▶ Ouro.parse ──▶ ParseResult (Flat Arena)
│
▼
Ouro.compile ──▶ Binary Bytecode (v3–v14) / ProtoModule
│
▼
Ouro.Bytecode.verify ──▶ Ouro.run / Ouro.sandbox / Ouro.load
| Goal | Recommended API | Return Contract |
|---|---|---|
| Run untrusted text once with safe defaults | Ouro.run | (ok, ...resultsOrErr) |
| Run many scripts under a shared policy & host APIs | Ouro.sandbox (:fork, :run, :load, :reset) | Sandbox handle |
| Load source or bytecode on the fast unguarded loop | Ouro.load | (fn?, err?) |
| Execute precompiled bytecode without loading the compiler | Ouro.VM.loadBytecode / loadProto | (fn?, err?) |
Compile source or ParseResult to binary bytecode | Ouro.compile / Ouro.compileResult | (bytecode?, err?) / CompileResult |
Parse scripts, expressions, types, or .d.luau files | Ouro.parse / parseExpression / parseType | ParseResult |
| Verify, decode, serialize, or disassemble bytecode | Ouro.Bytecode | (okOrValue?, err?) |
Batteries Included
- Safe-by-default sandboxes & presets: Choose
"strict","balanced", or"trusted"inOuro.PRESETS, expose custom host globals, fork isolated per-script overlays with independentPCG32math.randomstate, and reset budgets between runs viaOuro.sandbox. - Arena parser & lossless reconstruction: Parse scripts, expressions, type packs, and
.d.luaufiles, slice node text with:text(), render caret diagnostics with:formatDiagnostics(), preserve exact bytes withlossless = true, and traverse arenas viaOuro.parseandOuro.Ast. - Grammar profiles & strict flags: Switch between
upstream,studio, andexperimentalpresets or toggle individual syntax flags viaOuro.Profiles. - Full O0–O2 bytecode compiler: Inline functions, fold builtins and constants, emit vector, native, and usercall fastcalls, enforce
maxInstructionsandallowDirectives, and verify or inspect bytecode (v3–v14,v100classes) throughOuro.CompilerandOuro.Bytecode. - Isolated register VM & tooling hooks: Enforce instruction, call, timeout, string, table, buffer, pattern-step, and memory caps, intercept global reads and writes, collect line coverage, and attach stack/upvalue debuggers via
Ouro.VM.
Installation
Install kohltastrophe/ouro with Wally or pesde (pesde add kohltastrophe/ouro), download Ouro.rbxm or Ouro-<version>-package.zip from GitHub Releases and place the module in ReplicatedStorage, sync src directly with Rojo, or pull the runtime-only dist branch via git subtree. Standalone Luau callers can require the repository root directly (require("./ouro")).
Read Getting Started, browse the Cookbook, or open the API Reference.
Acknowledgments
Ouro translates lexer, parser, compiler, bytecode, and VM algorithms and confusable mappings from official Luau, pinned to 0.748 (3d91c9d) in UPSTREAM.json. Arena storage, public APIs, the lossless lexical tape, sandbox presets, and coroutine checkpointing are adaptations for this pure-Luau port.
Sponsors
Ouro is free and MIT-licensed. If Ouro is useful to you or your team, you can support its development through GitHub Sponsors or Ko-fi.