Expand description
§luaur-rt
A safe, ergonomic, mlua-style high-level API for
luaur — a pure-Rust translation of
Roblox’s Luau.
The public surface deliberately mirrors mlua’s
interface — the same type names (Lua, Value, Table,
Function, LuaString, MultiValue, Variadic, Error), the
same method names and call shapes (Lua::new, lua.globals(),
lua.create_function, lua.load(src).eval::<T>(), table.set/get,
function.call::<R>(args)), and the same conversion traits (FromLua,
IntoLua, FromLuaMulti, IntoLuaMulti) and userdata traits
(UserData, UserDataMethods). The implementation, however, is
entirely original: it is written directly over luaur’s pure-Rust C API
(lua_*), not over a C FFI.
use luaur_rt::prelude::*;
let lua = Lua::new();
let add = lua
.create_function(|_, (a, b): (i64, i64)| Ok(a + b))
.unwrap();
lua.globals().set("add", add).unwrap();
let sum: i64 = lua.load("return add(2, 3)").eval().unwrap();
assert_eq!(sum, 5);§Single-threaded
Like mlua’s default, Lua is single-threaded: it is built on Rc, so it
is neither Send nor Sync. Clone a Lua to get another handle to the
same VM.
§Deferred (not yet implemented)
The following parts of mlua’s surface are intentionally out of scope and are noted here rather than implemented:
- Multi-VM
Send/Sync(WeakLua, send-able handles) (P4). - Thread event callbacks (
ThreadEvent/ThreadTriggers/set_thread_event_callback) and per-thread hooks. - The
chunk!proc-macro. - Async userdata methods (
add_async_method*) and theObjectLikecall_async_methodsurface (depend on the deferred userdata registry).
Implemented in Phase 1: Thread/coroutine wrappers, public RegistryKey
storage, UserDataFields, typed AnyUserData::borrow/borrow_mut/take/
is, the MetaMethod enum, and Function::info/environment.
Implemented in Phase 2: the Luau-specific runtime types Buffer (the
buffer type) and Vector (the vector type), with their
Value::Buffer/Value::Vector variants and FromLua/IntoLua impls.
Implemented in Phase 4a (behind the serde cargo feature): serde
(de)serialization between Rust types and Lua Values — the LuaSerdeExt
trait on Lua, a serde Serializer/Deserializer over Value/
Table, SerializeOptions/DeserializeOptions, and Serialize impls
for Value/Table with the Value::to_serializable wrapper.
Implemented in Phase 4c (behind the async cargo feature): Rust async/
await support — the Rust-Future ⟷ Lua-coroutine bridge.
Lua::create_async_function / Lua::yield_with, Function::call_async /
Function::wrap_async / Function::wrap_raw_async, Chunk::call_async /
Chunk::exec_async / Chunk::eval_async, and Thread::into_async
producing an AsyncThread that implements Future + Stream.
Executor-agnostic, like mlua: the caller drives the returned futures on
their own runtime.
Re-exports§
Modules§
- ffi
- The public, mlua-style
ffisurface (mounted fromffi_public.rs). The public, mlua-style raw C API surface (re-exported atluaur_rt::ffi). - prelude
- Idiomatic glob-import prelude. Mirrors
mlua::prelude, additionally re-exporting the short names souse luaur_rt::prelude::*;brings the whole ergonomic surface into scope. - state
- The
Luahandle and the shared inner state.
Structs§
- AnyUser
Data - A handle to an arbitrary Lua userdata value.
- AppData
Ref - An immutable borrow of an application-data value of type
T. Mirrorsmlua::AppDataRef. - AppData
RefMut - A mutable borrow of an application-data value of type
T. Mirrorsmlua::AppDataRefMut. - Buffer
- A Luau buffer type.
- Chunk
- A not-yet-executed piece of Lua source.
- Compiler
- Luau bytecode compiler options, mirroring
mlua::Compiler. - Debug
- A snapshot of one activation record, resolved from a stack level via
lua_getinfo. Mirrors the informational subset ofmlua::debug::Debug. - Function
- A handle to a callable Lua value (a Lua closure or a Rust function).
- Function
Info - Debug information about a
Function. Mirrorsmlua::debug::FunctionInfo(the subset Luau reports). - Light
User Data - A Lua light userdata: an opaque raw pointer value. Mirrors
mlua::LightUserData. - LuaOptions
- Per-VM behavioral options. Mirrors
mlua::LuaOptions. - LuaString
- A garbage-collected Lua string.
- Multi
Value - An ordered, growable sequence of Lua values, used to represent the multiple arguments / multiple return values that Lua functions can take and produce.
- Registry
Key - An owned reference to a value stored in the Lua registry.
- Scope
- A scope for creating lifetime-bounded Lua callbacks and userdata.
- StdLib
- A bit-set selecting which standard libraries to open. Mirrors
mlua::StdLib. - Table
- A handle to a Lua table.
- Table
Pairs - Iterator over a table’s key/value pairs (see
Table::pairs). - Table
Sequence - Iterator over a table’s sequence part (see
Table::sequence_values). - Thread
- A handle to a Lua thread (coroutine). Mirrors
mlua::Thread. - User
Data Ref - A RAII guard for an immutable userdata borrow (
AnyUserData::borrow). Mirrorsmlua::UserDataRef. - User
Data RefMut - A RAII guard for a mutable userdata borrow (
AnyUserData::borrow_mut). Mirrorsmlua::UserDataRefMut. - Variadic
- A wrapper collecting “the rest of” a Lua argument list (or producing a variable number of return values).
- Vector
- A Luau vector type.
Enums§
- Chunk
Mode - How a chunk’s bytes are interpreted. Mirrors
mlua::ChunkMode. luaur-rt always loads text source, so this exists for signature parity withChunk::set_mode. - Debug
What - What kind of function an activation record refers to. Mirrors the relevant
part of
mlua::debug::DebugSource::what. - Error
- Errors that can occur when interacting with the Lua engine.
- Meta
Method - Kinds of metamethods that can be overridden on userdata.
- Thread
Status - Status of a Lua thread (coroutine). Mirrors
mlua::ThreadStatus. - Value
- A dynamically typed Lua value.
- VmState
- The action an interrupt callback asks the VM to take. Mirrors
mlua::VmState.
Traits§
- External
Error - Convenience for turning an arbitrary error/displayable into an
Error. - External
Result Resultextension mirroringmlua::ExternalResult: lift anyResult<T, E>into aluaurResultby converting the error.- FromLua
- Convert a single Lua
Valueinto a Rust value. - From
LuaMulti - Convert a sequence of Lua values into a Rust value.
- IntoLua
- Convert a Rust value into a single Lua
Value. - Into
LuaMulti - Convert a Rust value into a sequence of Lua values (multiple returns / args).
- LuaNative
Fn - A function/closure callable with a tuple of
FromLuaMultiarguments, abstracting over arity. Mirrorsmlua::LuaNativeFn. LetsFunction::wrapaccept||,|a|,|a, b|, … closures uniformly (the closure receives the converted args directly, not theLua). - Maybe
Send - See the
send-gated variant above. - Maybe
Sync - See the
send-gated variant above. - Type
Metatable - Luau built-in types that have a shared, per-type metatable settable via
Lua::set_type_metatable. Mirrors mlua’s sealedLuauTypetrait. - User
Data - A Rust type that can be exposed to Lua as userdata.
- User
Data Fields - Registrar passed to
UserData::add_fields. - User
Data Methods - Registrar passed to
UserData::add_methods.
Type Aliases§
- Integer
- The integer type exposed by the API. Mirrors
mlua::Integer(i64). - Number
- The float type exposed by the API. Mirrors
mlua::Number(f64). - Result
- The result type used throughout
luaur-rt, mirroring [mlua::Result]. - lua_
State - A raw
lua_Statepointer type alias, re-exported at the crate root for signature parity withmlua::lua_State.