lua-rs-runtime
Embed Lua 5.4 in a Rust program. lua-rs-runtime is the embedding API for
lua-rs, a Lua 5.4 implementation written in
safe Rust. Because the whole runtime is Rust, it embeds where a C binding can't:
wasm32-unknown-unknown, no C toolchain, no liblua to link. You give up
LuaJIT speed and years of maturity in exchange.
[]
= "0.0.11"
Calling Rust from Lua
use ;
The API is shaped after mlua: owned GC-rooted handles (Value, Table,
Function, AnyUserData), closure callbacks, conversion traits, and
UserData for binding Rust types with methods, fields, and metamethods. A
#[derive(LuaUserData)] / #[lua_methods] macro pair generates the
boilerplate.
Scope: lending non-'static borrows
Lua::scope lends Lua a value that lives on the Rust stack for one call (the
classic case is a game engine's &mut World). The borrow is invalidated when
the scope returns, so a script that stashes a handle and uses it later gets a
clean Lua error instead of touching freed memory.
lua.scope?;
Scope::create_function does the same for closures that capture non-'static
borrows, and AnyUserData::delegate returns a sub-userdata that re-borrows a
field of its parent per call, so an App -> World -> Component chain stays a
chain of short borrows. Runnable example:
cargo run -p lua-rs-runtime --example scope_world.
Links
- Worked Bevy 0.18 integration, native and in the browser: bevy-lua-rs-starter (live demo).
- WebAssembly distribution:
lua-rs-wasm. - Project, benchmarks, and conformance: github.com/ianm199/lua-rs.
License
A port of Lua (PUC-Rio). MIT-licensed.