pub struct Lua { /* private fields */ }Expand description
Primary owned embedding handle.
Lua is intentionally cheap to clone and single-threaded. State access is
borrowed at the embedding boundary only; opcode dispatch still runs with
direct &mut LuaState access. Captured Rust callbacks will need a call-path
adapter that releases this boundary borrow before invoking user code.
Implementations§
Source§impl Lua
impl Lua
Sourcepub fn with_hooks(hooks: HostHooks) -> Result<Self>
pub fn with_hooks(hooks: HostHooks) -> Result<Self>
Create a Lua runtime with the supplied host capabilities.
Sourcepub fn create_table(&self) -> Result<Table>
pub fn create_table(&self) -> Result<Table>
Create a new empty table.
Sourcepub fn create_string(&self, bytes: impl AsRef<[u8]>) -> Result<LuaString>
pub fn create_string(&self, bytes: impl AsRef<[u8]>) -> Result<LuaString>
Create a new Lua string from bytes.
pub fn create_function<A, R, F>(&self, func: F) -> Result<Function>
pub fn create_function_mut<A, R, F>(&self, func: F) -> Result<Function>where
A: FromLuaMulti + 'static,
R: IntoLuaMulti + 'static,
F: FnMut(&Lua, A) -> Result<R> + 'static,
pub fn create_userdata<T>(&self, data: T) -> Result<AnyUserData>where
T: UserData,
Sourcepub fn scope<F, R>(&self, f: F) -> Result<R>
pub fn scope<F, R>(&self, f: F) -> Result<R>
Run f with a fresh Scope; any AnyUserData created via the
scope is invalidated when f returns, so leaked references fail
cleanly instead of using-after-the-borrow-ended.
use lua_rs_runtime::{Lua, UserData, UserDataMethods};
struct Counter { value: i64 }
impl UserData for Counter {
fn add_methods<M: UserDataMethods<Self>>(methods: &mut M) {
methods.add_method_mut("inc", |_lua, this, delta: i64| {
this.value += delta;
Ok(this.value)
});
}
}
let lua = Lua::new();
let mut counter = Counter { value: 0 };
lua.scope(|scope| {
let ud = scope.create_userdata_ref_mut(&lua, &mut counter)?;
lua.globals().set("c", &ud)?;
lua.load("c:inc(5); c:inc(7)").exec()
}).unwrap();
assert_eq!(counter.value, 12);
// The script can stash the userdata on a global and try to use it
// later, but the call cleanly errors instead of touching the
// dropped `&mut counter`:
lua.scope(|scope| {
let ud = scope.create_userdata_ref_mut(&lua, &mut counter)?;
lua.globals().set("leaked", &ud)
}).unwrap();
assert!(lua.load("leaked:inc(1)").exec().is_err());Sourcepub fn gc_collect(&self)
pub fn gc_collect(&self)
Run a full garbage-collection cycle.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Lua
impl !RefUnwindSafe for Lua
impl !Send for Lua
impl !Sync for Lua
impl Unpin for Lua
impl UnsafeUnpin for Lua
impl !UnwindSafe for Lua
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more