luau-rs compiles WebAssembly (wasm32) modules built from Rust into --!strict Luau for Roblox.
Rust source --cargo build --target wasm32-unknown-unknown--> module.wasm
module.wasm --luau-rs decode--> DecodedModule
DecodedModule --luau-rs translate--> strict Luau module (instantiate factory)
Because compilation starts from WebAssembly, Rust source keeps full rustc semantics, including the borrow checker and Cargo dependencies. The Luau backend models linear memory as a buffer, globals and indirect-call tables as Luau tables, and the operand stack explicitly per function.
Build the CLI from source with Rust (the repository pins its toolchain in rust-toolchain.toml):
cargo install --path .To use luau-rs as a Rust library, add it as a dependency in Cargo.toml.
Compile a Rust crate to wasm32-unknown-unknown, then translate the resulting .wasm file into a Roblox script layout:
rustup target add wasm32-unknown-unknown
cargo build --release --target wasm32-unknown-unknown \
--manifest-path fixtures/rust-hello/Cargo.toml \
--target-dir fixtures/rust-hello/target
luau-rs build fixtures/rust-hello/target/wasm32-unknown-unknown/release/rust_hello.wasm \
--out build \
--entrypointAvailable flags for build:
luau-rs build <module.wasm> --out <dir> [--entrypoint] [--side server|client] [--module-path <path>]
Create a luau-rs.toml manifest with paths relative to the manifest file:
[project]
source_root = "wasm"
output_root = "build"Place .wasm files under source_root using the Roblox DataModel layout:
<source_root>/<RobloxService>/<module-path>.wasm
For example:
wasm/ServerScriptService/game/main.wasmemitsbuild/ServerScriptService/game/main.server.luau.wasm/ReplicatedStorage/math/core.wasmemitsbuild/ReplicatedStorage/math/core.luau.
Entrypoint scripts belong under ServerScriptService, Workspace, ReplicatedFirst, StarterGui, StarterPlayer/StarterPlayerScripts, or StarterPlayer/StarterCharacterScripts. Libraries belong under ServerStorage or ReplicatedStorage. Existing .luau, .rbxm, .rbxmx, .rbxl, .rbxlx, and *.model.json files are copied through at their DataModel paths.
Validate or compile the project:
luau-rs check --manifest-path luau-rs.toml
luau-rs compile --manifest-path luau-rs.toml--manifest-path defaults to luau-rs.toml. check validates discovery, decoding, and translation without writing files. compile stages every generated file and retained asset and atomically swaps output_root only after all modules succeed. Unknown services, invalid StarterPlayer paths, duplicate outputs, and overlapping source/output roots are rejected before any files are written.
use luau_rs::{
decode_module, translate_module, DecodeOutcome, MainInvocation, TranslateOptions,
TranslateOutcome,
};
fn compile_wasm(wasm_bytes: &[u8]) -> Result<String, String> {
let decoded = match decode_module(wasm_bytes) {
DecodeOutcome::Decoded(module) => module,
DecodeOutcome::Rejected(rejection) => {
return Err(format!("{:?}", rejection.diagnostics()));
}
};
let options = TranslateOptions::with_main_invocation(MainInvocation::LeaveIdle);
match translate_module(&decoded, options) {
TranslateOutcome::Translated(luau) => Ok(luau.into_text()),
TranslateOutcome::Rejected(rejection) => Err(format!("{:?}", rejection.diagnostics())),
}
}WasmDecodeRejection::diagnostics() and TranslationRejection::diagnostics() return a serializable DiagnosticReport. Each diagnostic includes code, message, and a location with optional file, line, column, function, and wasm_offset fields. When DWARF debug info is present in the .wasm binary, decode diagnostics populate Rust source locations (source_available = true); otherwise hint explains how to recover source lines with debug info.
Generated modules return an instantiation factory that accepts an imports table. The bundled runtime/roblox.luau bridges wasm imports to Roblox APIs using integer handles (0 is null):
local module = require(path.to.module)
local runtime = require(path.to.roblox_runtime).new({
game = game,
workspace = workspace,
Instance = Instance,
Vector3 = Vector3,
print = print,
})
local exports = module(runtime.imports)
runtime.bind_memory(exports.memory)runtime/roblox.luau currently covers service lookup, instance creation and destruction, Name/ClassName/Anchored/Size properties, event connections, remote calls, and print.
Supported:
- Numeric instructions (
i32,i64,f32,f64), comparisons, and conversions - Locals, globals, linear memory loads/stores,
memory.grow, and bulk memory (memory.copy,memory.fill,memory.init, passive data segments) call,call_indirect,block,loop,if,br,br_table, andselect- Active data segments, active element segments, start functions, function imports, and exports
Rejected with typed errors:
- SIMD (
v128), atomics, and exception-handling tags - Shared or 64-bit memories and multiple memories
- Non-function imports, global/table/tag exports, and passive or declarative element segments
Current behavioral notes:
i64values are represented exactly as pairs of unsigned 32-bit Luau numbers across locals, globals, calls, arithmetic, bitwise operations, comparisons, conversions, and memory access.f32single-precision rounding is not modeled, and integer division by zero does not trap.- The emitter models the wasm operand stack as a Luau table indexed by a stack pointer and lowers structured branches into nested loops with exit flags.
Integration tests validate generated Luau with Full Moon and the pinned official luau, luau-analyze, and luau-compile binaries. Build the pinned Luau tools once before running tests:
python3 scripts/build_pinned_luau.py
cargo fmt --check
cargo clippy --all-targets --all-features --locked -- -D warnings
cargo test --all-targets --all-features --lockedSet LUAU_BIN, LUAU_ANALYZE_BIN, and LUAU_COMPILE_BIN to point at prebuilt Luau binaries if you prefer not to build them under references/checkouts/luau.
luau-rs is at 0.x; minor releases may change the CLI, Rust API, or generated Luau shape.
MIT. See LICENSE.