Struct sandkiste_lua::v5_3::LuaMachine
source · [−]pub struct LuaMachine<'a> { /* private fields */ }
Expand description
Virtual machine that executes Lua code
Basic usage
- A new virtual machine can be created with
LuaMachine::new
. - Use
LuaMachine::load_stdlib_sealed
to load a sealed version of the Lua standard library; that is a modified version which does not allow breaking out of the sandbox. Alternatively, a full version of the standard library can be loaded withLuaMachine::load_stdlib
, which, for example, can be used to load further dependencies, and then be sealed afterwards withLuaMachine::seal
. Note, however, that this may introduce security vulnerabilites depending on which libraries have been loaded or how the state has been modified prior to callingseal
. - Set resource limits with
LuaMachine::set_memory_limit
andLuaMachine::set_execution_limit
, if desired. - Use the API provided by the
sandkiste
crate to compile and execute Lua programs. For a concrete example using Lua, see the top-level module documentation ofsandkiste_lua
.
Lifetimes
Lifetime argument 'a
is a lower bound for closures passed to the Lua
machine. It can be inferred automatically.
Implementations
sourceimpl<'a> LuaMachine<'a>
impl<'a> LuaMachine<'a>
sourcepub fn assert_empty_stack(&self)
pub fn assert_empty_stack(&self)
Assert that Lua stack is empty
sourcepub fn set_execution_limit(&self, instr: u64)
pub fn set_execution_limit(&self, instr: u64)
Set limit for executed instructions
sourcepub fn set_memory_limit(&self, bytes: usize)
pub fn set_memory_limit(&self, bytes: usize)
Set memory limit
sourcepub fn load_stdlib(&self) -> Result<(), MachineError>
pub fn load_stdlib(&self) -> Result<(), MachineError>
Load standard library
sourcepub fn load_stdlib_sealed(&self) -> Result<(), MachineError>
pub fn load_stdlib_sealed(&self) -> Result<(), MachineError>
Load sealed version of standard library
sourcepub fn seal(&self) -> Result<(), MachineError>
pub fn seal(&self) -> Result<(), MachineError>
Remove certain parts of standard library to seal machine as sandbox
sourcepub fn new_table<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
pub fn new_table<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
Create empty table
sourcepub fn get_reference_key<'b>(
&'b self,
lua_ref: &LuaReference<'a, 'b>
) -> LuaReferenceKey
pub fn get_reference_key<'b>(
&'b self,
lua_ref: &LuaReference<'a, 'b>
) -> LuaReferenceKey
Get LuaReferenceKey
for LuaReference
sourcepub unsafe fn pop_error_message(&self) -> String
pub unsafe fn pop_error_message(&self) -> String
Pop error value from Lua stack and convert into String
sourcepub unsafe fn pop_error(&self, status: c_int) -> Result<(), MachineError>
pub unsafe fn pop_error(&self, status: c_int) -> Result<(), MachineError>
If status != LUA_OK
, pop error value from Lua stack and convert into MachineError
sourcepub unsafe fn pop_error_with_backtrace(
&self,
status: c_int
) -> Result<(), MachineError>
pub unsafe fn pop_error_with_backtrace(
&self,
status: c_int
) -> Result<(), MachineError>
Same as LuaMachine::pop_error
but also extract traceback
(requires cmach_lua_errmsgh
to be used during lua_pcall
)
sourcepub unsafe fn extract_datum<'b>(
&'b self,
index: c_int
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
pub unsafe fn extract_datum<'b>(
&'b self,
index: c_int
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
Convert value on Lua stack into LuaDatum
NOTE: requires some free space on Lua stack for operation
sourcepub unsafe fn pop_datum<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
pub unsafe fn pop_datum<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
Pop value from Lua stack and convert into LuaDatum
NOTE: requires some free space on Lua stack for operation
sourcepub unsafe fn push_reference<'b>(&'b self, lua_ref: &LuaReference<'a, 'b>)
pub unsafe fn push_reference<'b>(&'b self, lua_ref: &LuaReference<'a, 'b>)
Push LuaReference
on top of Lua stack
sourcepub unsafe fn push_bin(&self, binary: &[u8]) -> Result<(), MachineError>
pub unsafe fn push_bin(&self, binary: &[u8]) -> Result<(), MachineError>
Push binary data on top of Lua stack
NOTE: requires some free space on Lua stack for operation
sourcepub unsafe fn push_str(&self, string: &str) -> Result<(), MachineError>
pub unsafe fn push_str(&self, string: &str) -> Result<(), MachineError>
Push string on top of Lua stack
NOTE: requires some free space on Lua stack for operation
sourcepub unsafe fn push_datum<'b, 'c>(
&'b self,
datum: &LuaDatum<'a, 'b, 'c>
) -> Result<(), MachineError>
pub unsafe fn push_datum<'b, 'c>(
&'b self,
datum: &LuaDatum<'a, 'b, 'c>
) -> Result<(), MachineError>
Push LuaDatum
on top of Lua stack
NOTE: requires some free space on Lua stack for operation
Trait Implementations
sourceimpl<'a> Callback<'a> for LuaMachine<'a>
impl<'a> Callback<'a> for LuaMachine<'a>
sourcefn callback<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Vec<LuaDatum<'a, 'b, 'static>>) -> Result<R, Box<dyn Error>>,
fn callback<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Vec<LuaDatum<'a, 'b, 'static>>) -> Result<R, Box<dyn Error>>,
Create a Machine::Datum
representing a callback (which invokes the func
closure)
sourcefn callback_expect_args<'b, 'c, F, R>(
&'b self,
argc: usize,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_expect_args<'b, 'c, F, R>(
&'b self,
argc: usize,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects a fixed number of arguments
sourcefn callback_expect_min_args<'b, 'c, F, R>(
&'b self,
argc: usize,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_expect_min_args<'b, 'c, F, R>(
&'b self,
argc: usize,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects a minimum number of arguments
sourcefn callback_1arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_1arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects exactly one argument
sourcefn callback_2arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_2arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects exactly two arguments
sourcefn callback_3arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_3arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects exactly three arguments
sourcefn callback_4arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_4arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects exactly four arguments
sourcefn callback_5arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
fn callback_5arg<'b, 'c, F, R>(
&'b self,
func: F
) -> Result<Self::Datum<'b, 'static>, MachineError> where
R: IntoIterator<Item = Self::Datum<'b, 'c>>,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
Same as Callback::callback
but expects exactly five arguments
sourceimpl<'a, 'b> Compile<'a, &'b str> for LuaMachine<'a>
impl<'a, 'b> Compile<'a, &'b str> for LuaMachine<'a>
sourcefn compile(
&self,
chunk_name: Option<String>,
code: &'b str
) -> Result<LuaFunction<'a, '_>, MachineError>
fn compile(
&self,
chunk_name: Option<String>,
code: &'b str
) -> Result<LuaFunction<'a, '_>, MachineError>
Compiles code, returns the compiled function or a compilation error Read more
sourceimpl<'a> Compile<'a, String> for LuaMachine<'a>
impl<'a> Compile<'a, String> for LuaMachine<'a>
sourcefn compile(
&self,
chunk_name: Option<String>,
code: String
) -> Result<LuaFunction<'a, '_>, MachineError>
fn compile(
&self,
chunk_name: Option<String>,
code: String
) -> Result<LuaFunction<'a, '_>, MachineError>
Compiles code, returns the compiled function or a compilation error Read more
sourceimpl<'a> Debug for LuaMachine<'a>
impl<'a> Debug for LuaMachine<'a>
sourceimpl<'a> Drop for LuaMachine<'a>
impl<'a> Drop for LuaMachine<'a>
sourceimpl<'a> Globals<'a> for LuaMachine<'a>
impl<'a> Globals<'a> for LuaMachine<'a>
sourceimpl<'a> HasArray<'a> for LuaMachine<'a>
impl<'a> HasArray<'a> for LuaMachine<'a>
sourcefn new_empty_array<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
fn new_empty_array<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
Create datum that is an empty array
sourcefn new_array<'b, 'c, I>(
&'b self,
elements: I
) -> Result<Self::Datum<'b, 'static>, MachineError> where
I: IntoIterator<Item = Self::Datum<'b, 'c>>,
Self::Datum<'b, 'static>: MaybeArray,
<Self::Datum<'b, 'static> as MaybeArray>::Element<'d> == Self::Datum<'b, 'd>,
fn new_array<'b, 'c, I>(
&'b self,
elements: I
) -> Result<Self::Datum<'b, 'static>, MachineError> where
I: IntoIterator<Item = Self::Datum<'b, 'c>>,
Self::Datum<'b, 'static>: MaybeArray,
<Self::Datum<'b, 'static> as MaybeArray>::Element<'d> == Self::Datum<'b, 'd>,
Create datum that is an array
sourceimpl<'a> HasModules<'a> for LuaMachine<'a>
impl<'a> HasModules<'a> for LuaMachine<'a>
sourceimpl<'a> HasStringMap<'a> for LuaMachine<'a>
impl<'a> HasStringMap<'a> for LuaMachine<'a>
sourcefn new_empty_string_map<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
fn new_empty_string_map<'b>(
&'b self
) -> Result<LuaDatum<'a, 'b, 'static>, MachineError>
Create datum that is an empty string map
sourcefn new_string_map<'b, 'c, 'd, I>(
&'b self,
entries: I
) -> Result<Self::Datum<'b, 'static>, MachineError> where
I: IntoIterator<Item = (&'d str, Self::Datum<'b, 'c>)>,
Self::Datum<'b, 'static>: MaybeStringMap,
<Self::Datum<'b, 'static> as MaybeStringMap>::Value<'e> == Self::Datum<'b, 'e>,
fn new_string_map<'b, 'c, 'd, I>(
&'b self,
entries: I
) -> Result<Self::Datum<'b, 'static>, MachineError> where
I: IntoIterator<Item = (&'d str, Self::Datum<'b, 'c>)>,
Self::Datum<'b, 'static>: MaybeStringMap,
<Self::Datum<'b, 'static> as MaybeStringMap>::Value<'e> == Self::Datum<'b, 'e>,
Create datum that is a string map
sourceimpl<'a> Hash for LuaMachine<'a>
impl<'a> Hash for LuaMachine<'a>
sourceimpl<'a> Machine<'a> for LuaMachine<'a>
impl<'a> Machine<'a> for LuaMachine<'a>
type Datum<'b, 'c>
where
Self: 'b = LuaDatum<'a, 'b, 'c>
type Datum<'b, 'c>
where
Self: 'b = LuaDatum<'a, 'b, 'c>
Data type representing values passed to or returned from machine
type Function<'b>
where
Self: 'b = LuaFunction<'a, 'b>
type Function<'b>
where
Self: 'b = LuaFunction<'a, 'b>
Function (for example returned by Compile::compile
)
which can be executed by the machine Read more
sourceimpl<'a> PartialEq<LuaMachine<'a>> for LuaMachine<'a>
impl<'a> PartialEq<LuaMachine<'a>> for LuaMachine<'a>
A LuaMachine
is only equal to itself
impl<'a> Eq for LuaMachine<'a>
A LuaMachine
is only equal to itself
Auto Trait Implementations
impl<'a> RefUnwindSafe for LuaMachine<'a>
impl<'a> !Send for LuaMachine<'a>
impl<'a> !Sync for LuaMachine<'a>
impl<'a> Unpin for LuaMachine<'a>
impl<'a> UnwindSafe for LuaMachine<'a>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more