pub struct Lua { /* private fields */ }Expand description
Safe, embedding-oriented Lua runtime.
This type sits on top of luars::LuaVM and exposes a narrower API that
avoids raw LuaValue plumbing in the common host-facing surface.
Implementations§
Source§impl Lua
impl Lua
Sourcepub fn new(option: SafeOption) -> Self
pub fn new(option: SafeOption) -> Self
Create a new Lua runtime.
Sourcepub fn open_stdlib(&mut self, lib: Stdlib) -> LuaResult<()>
pub fn open_stdlib(&mut self, lib: Stdlib) -> LuaResult<()>
Open standard libraries.
Sourcepub fn install_library<L: LuaLibrary>(&mut self, library: L) -> LuaResult<()>
pub fn install_library<L: LuaLibrary>(&mut self, library: L) -> LuaResult<()>
Install a library provided by luars or an external crate.
Sourcepub fn load_stdlibs(&mut self, lib: Stdlib) -> LuaResult<()>
pub fn load_stdlibs(&mut self, lib: Stdlib) -> LuaResult<()>
Open standard libraries.
Sourcepub fn collect_garbage(&mut self) -> LuaResult<()>
pub fn collect_garbage(&mut self) -> LuaResult<()>
Run a full garbage-collection cycle immediately.
Sourcepub fn execute(&mut self, source: &str) -> LuaResult<()>
pub fn execute(&mut self, source: &str) -> LuaResult<()>
Execute source code and discard raw return values.
Sourcepub fn eval<R: FromLua>(&mut self, source: &str) -> LuaResult<R>
pub fn eval<R: FromLua>(&mut self, source: &str) -> LuaResult<R>
Execute source code and convert the first returned value.
Sourcepub fn eval_multi<R: FromLuaMulti>(&mut self, source: &str) -> LuaResult<R>
pub fn eval_multi<R: FromLuaMulti>(&mut self, source: &str) -> LuaResult<R>
Execute source code and convert all returned values.
Sourcepub async fn exec_async(&mut self, source: &str) -> LuaResult<()>
pub async fn exec_async(&mut self, source: &str) -> LuaResult<()>
Execute source code asynchronously and discard raw return values.
Sourcepub async fn eval_async<R: FromLua>(&mut self, source: &str) -> LuaResult<R>
pub async fn eval_async<R: FromLua>(&mut self, source: &str) -> LuaResult<R>
Execute source code asynchronously and convert the first returned value.
Sourcepub async fn eval_multi_async<R: FromLuaMulti>(
&mut self,
source: &str,
) -> LuaResult<R>
pub async fn eval_multi_async<R: FromLuaMulti>( &mut self, source: &str, ) -> LuaResult<R>
Execute source code asynchronously and convert all returned values.
Sourcepub fn set_global<T: IntoLua>(&mut self, name: &str, value: T) -> LuaResult<()>
pub fn set_global<T: IntoLua>(&mut self, name: &str, value: T) -> LuaResult<()>
Set a global from a Rust value that converts to a single Lua value.
Sourcepub fn get_global<T: FromLua>(&mut self, name: &str) -> LuaResult<Option<T>>
pub fn get_global<T: FromLua>(&mut self, name: &str) -> LuaResult<Option<T>>
Get and convert a global variable.
Sourcepub fn call_global<A: IntoLua, R: FromLuaMulti>(
&mut self,
name: &str,
args: A,
) -> LuaResult<R>
pub fn call_global<A: IntoLua, R: FromLuaMulti>( &mut self, name: &str, args: A, ) -> LuaResult<R>
Call a global function and convert all results.
Sourcepub fn call_global1<A: IntoLua, R: FromLua>(
&mut self,
name: &str,
args: A,
) -> LuaResult<R>
pub fn call_global1<A: IntoLua, R: FromLua>( &mut self, name: &str, args: A, ) -> LuaResult<R>
Call a global function and convert the first result.
Sourcepub async fn call_async<A: IntoLua, R: FromLuaMulti>(
&mut self,
function: &Function,
args: A,
) -> LuaResult<R>
pub async fn call_async<A: IntoLua, R: FromLuaMulti>( &mut self, function: &Function, args: A, ) -> LuaResult<R>
Call a function handle asynchronously and convert all results.
Sourcepub async fn call_async1<A: IntoLua, R: FromLua>(
&mut self,
function: &Function,
args: A,
) -> LuaResult<R>
pub async fn call_async1<A: IntoLua, R: FromLua>( &mut self, function: &Function, args: A, ) -> LuaResult<R>
Call a function handle asynchronously and convert only the first result.
Sourcepub async fn call_async_global<A: IntoLua, R: FromLuaMulti>(
&mut self,
name: &str,
args: A,
) -> LuaResult<R>
pub async fn call_async_global<A: IntoLua, R: FromLuaMulti>( &mut self, name: &str, args: A, ) -> LuaResult<R>
Call a global function asynchronously and convert all results.
Sourcepub async fn call_async_global1<A: IntoLua, R: FromLua>(
&mut self,
name: &str,
args: A,
) -> LuaResult<R>
pub async fn call_async_global1<A: IntoLua, R: FromLua>( &mut self, name: &str, args: A, ) -> LuaResult<R>
Call a global function asynchronously and convert only the first result.
Sourcepub fn register_function<F, Args, R>(
&mut self,
name: &str,
f: F,
) -> LuaResult<()>where
F: LuaTypedCallback<Args, R>,
pub fn register_function<F, Args, R>(
&mut self,
name: &str,
f: F,
) -> LuaResult<()>where
F: LuaTypedCallback<Args, R>,
Register a typed Rust callback as a Lua global.
Sourcepub fn create_function<F, Args, R>(&mut self, f: F) -> LuaResult<Function>where
F: LuaTypedCallback<Args, R>,
pub fn create_function<F, Args, R>(&mut self, f: F) -> LuaResult<Function>where
F: LuaTypedCallback<Args, R>,
Create a typed Rust callback as a standalone Lua function handle.
Sourcepub fn register_async_function<F, Args, R>(
&mut self,
name: &str,
f: F,
) -> LuaResult<()>where
F: LuaTypedAsyncCallback<Args, R>,
pub fn register_async_function<F, Args, R>(
&mut self,
name: &str,
f: F,
) -> LuaResult<()>where
F: LuaTypedAsyncCallback<Args, R>,
Register a typed async Rust callback as a Lua global.
Sourcepub fn register_type_of<T: LuaRegistrable>(
&mut self,
name: &str,
) -> LuaResult<()>
pub fn register_type_of<T: LuaRegistrable>( &mut self, name: &str, ) -> LuaResult<()>
Register a userdata type in the global environment.
Sourcepub fn register_type<T: LuaRegistrable>(
&mut self,
name: &str,
) -> LuaResult<Table>
pub fn register_type<T: LuaRegistrable>( &mut self, name: &str, ) -> LuaResult<Table>
Register a userdata type and return the exported global type table.
pub fn register_enum_of<T: LuaEnum>(&mut self, name: &str) -> LuaResult<()>
Sourcepub fn load<'lua>(&'lua mut self, source: &str) -> Chunk<'lua>
pub fn load<'lua>(&'lua mut self, source: &str) -> Chunk<'lua>
Return a chunk builder that can be executed, evaluated, or compiled.
Sourcepub fn scope<'lua, R>(
&'lua mut self,
f: impl for<'scope> FnOnce(&mut Scope<'scope, 'lua>) -> LuaResult<R>,
) -> LuaResult<R>
pub fn scope<'lua, R>( &'lua mut self, f: impl for<'scope> FnOnce(&mut Scope<'scope, 'lua>) -> LuaResult<R>, ) -> LuaResult<R>
Run a lexical scope that can create non-'static Lua callbacks and borrowed userdata.
Sourcepub fn load_function(&mut self, source: &str) -> LuaResult<Function>
pub fn load_function(&mut self, source: &str) -> LuaResult<Function>
Compile source and return a callable function handle.
Sourcepub fn create_string(&mut self, value: &str) -> LuaResult<LuaString>
pub fn create_string(&mut self, value: &str) -> LuaResult<LuaString>
Create a safe Lua string handle.
Sourcepub fn create_table(&mut self) -> LuaResult<Table>
pub fn create_table(&mut self) -> LuaResult<Table>
Create a new empty table handle.
Sourcepub fn create_table_with_capacity(
&mut self,
narr: usize,
nrec: usize,
) -> LuaResult<Table>
pub fn create_table_with_capacity( &mut self, narr: usize, nrec: usize, ) -> LuaResult<Table>
Create a new table handle with explicit capacities.
Sourcepub fn create_userdata<T: UserDataTrait + 'static>(
&mut self,
data: T,
) -> LuaResult<UserDataRef<T>>
pub fn create_userdata<T: UserDataTrait + 'static>( &mut self, data: T, ) -> LuaResult<UserDataRef<T>>
Create a GC-managed userdata and return a typed handle to it.
Sourcepub unsafe fn create_userdata_ref<T: UserDataTrait + 'static>(
&mut self,
reference: &mut T,
) -> LuaResult<UserDataRef<T>>
pub unsafe fn create_userdata_ref<T: UserDataTrait + 'static>( &mut self, reference: &mut T, ) -> LuaResult<UserDataRef<T>>
Create a GC-managed userdata that borrows a Rust value.
§Safety
The referenced Rust value must outlive all Lua accesses to the returned userdata.
Sourcepub fn create_table_from<K, V, I>(&mut self, iter: I) -> LuaResult<Table>
pub fn create_table_from<K, V, I>(&mut self, iter: I) -> LuaResult<Table>
Create a table and populate it from key-value pairs.
Sourcepub fn create_sequence_from<T, I>(&mut self, iter: I) -> LuaResult<Table>where
T: IntoLua,
I: IntoIterator<Item = T>,
pub fn create_sequence_from<T, I>(&mut self, iter: I) -> LuaResult<Table>where
T: IntoLua,
I: IntoIterator<Item = T>,
Create a table from a sequence of values using 1.. as keys.
Sourcepub fn get_function(&mut self, name: &str) -> LuaResult<Option<Function>>
pub fn get_function(&mut self, name: &str) -> LuaResult<Option<Function>>
Get a global function handle.
Sourcepub fn set_global_table(&mut self, name: &str, table: &Table) -> LuaResult<()>
pub fn set_global_table(&mut self, name: &str, table: &Table) -> LuaResult<()>
Bind a safe table handle into the global environment.
Sourcepub fn set_global_function(
&mut self,
name: &str,
function: &Function,
) -> LuaResult<()>
pub fn set_global_function( &mut self, name: &str, function: &Function, ) -> LuaResult<()>
Bind a safe function handle into the global environment.
Sourcepub fn table_set<T: IntoLua>(
&mut self,
table: &Table,
key: &str,
value: T,
) -> LuaResult<()>
pub fn table_set<T: IntoLua>( &mut self, table: &Table, key: &str, value: T, ) -> LuaResult<()>
Set a string-keyed table field from a Rust value.
Sourcepub fn table_seti<T: IntoLua>(
&mut self,
table: &Table,
key: i64,
value: T,
) -> LuaResult<()>
pub fn table_seti<T: IntoLua>( &mut self, table: &Table, key: i64, value: T, ) -> LuaResult<()>
Set an integer-keyed table field from a Rust value.
Sourcepub fn table_get<T: FromLua>(
&mut self,
table: &Table,
key: &str,
) -> LuaResult<T>
pub fn table_get<T: FromLua>( &mut self, table: &Table, key: &str, ) -> LuaResult<T>
Get and convert a string-keyed table field.
Sourcepub fn table_geti<T: FromLua>(
&mut self,
table: &Table,
key: i64,
) -> LuaResult<T>
pub fn table_geti<T: FromLua>( &mut self, table: &Table, key: i64, ) -> LuaResult<T>
Get and convert an integer-keyed table field.
Sourcepub fn table_push<T: IntoLua>(
&mut self,
table: &Table,
value: T,
) -> LuaResult<()>
pub fn table_push<T: IntoLua>( &mut self, table: &Table, value: T, ) -> LuaResult<()>
Append a Rust value to the array part of a table.
Sourcepub fn table_pairs<K: FromLua, V: FromLua>(
&mut self,
table: &Table,
) -> LuaResult<Vec<(K, V)>>
pub fn table_pairs<K: FromLua, V: FromLua>( &mut self, table: &Table, ) -> LuaResult<Vec<(K, V)>>
Convert a table snapshot into typed key-value pairs.
Sourcepub fn table_array<T: FromLua>(&mut self, table: &Table) -> LuaResult<Vec<T>>
pub fn table_array<T: FromLua>(&mut self, table: &Table) -> LuaResult<Vec<T>>
Read the array portion of a table in order from 1..=#t.
Sourcepub fn pack<T: IntoLua>(&mut self, value: T) -> LuaResult<Value>
pub fn pack<T: IntoLua>(&mut self, value: T) -> LuaResult<Value>
Pack a Rust value into a safe registry-backed Lua value handle.
Sourcepub fn unpack<T: FromLua>(&mut self, value: Value) -> LuaResult<T>
pub fn unpack<T: FromLua>(&mut self, value: Value) -> LuaResult<T>
Unpack a safe Lua value handle into a Rust value.