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§
source§impl<'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§
source§impl<'a> Callback<'a> for LuaMachine<'a>
impl<'a> Callback<'a> for LuaMachine<'a>
source§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>>,
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>>,
Machine::Datum
representing a callback (which invokes the func
closure)source§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
Callback::callback
but expects a fixed number of argumentssource§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, F: 'a + Fn(Vec<Self::Datum<'b, 'static>, Global>) -> Result<R, Box<dyn Error + 'static, Global>>,
Callback::callback
but expects a minimum number of argumentssource§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, F: 'a + Fn(Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
Callback::callback
but expects exactly one argumentsource§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
Callback::callback
but expects exactly two argumentssource§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, F: 'a + Fn(Self::Datum<'b, 'static>, Self::Datum<'b, 'static>, Self::Datum<'b, 'static>) -> Result<R, Box<dyn Error + 'static, Global>>,
Callback::callback
but expects exactly three argumentssource§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
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>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, 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>>,
Callback::callback
but expects exactly four argumentssource§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>>,
<R as IntoIterator>::IntoIter: ExactSizeIterator,
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>>,
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>>, <R as IntoIterator>::IntoIter: ExactSizeIterator, 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>>,
Callback::callback
but expects exactly five argumentssource§impl<'a, 'b> Compile<'a, &'b str> for LuaMachine<'a>
impl<'a, 'b> Compile<'a, &'b str> for LuaMachine<'a>
source§fn 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>
source§impl<'a> Compile<'a, String> for LuaMachine<'a>
impl<'a> Compile<'a, String> for LuaMachine<'a>
source§fn 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>
source§impl<'a> Debug for LuaMachine<'a>
impl<'a> Debug for LuaMachine<'a>
source§impl<'a> Drop for LuaMachine<'a>
impl<'a> Drop for LuaMachine<'a>
source§impl<'a> Globals<'a> for LuaMachine<'a>
impl<'a> Globals<'a> for LuaMachine<'a>
source§impl<'a> HasArray<'a> for LuaMachine<'a>
impl<'a> HasArray<'a> for LuaMachine<'a>
source§fn 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>
source§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>: for<'d> 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>: for<'d> MaybeArray<Element<'d> = Self::Datum<'b, 'd>>,
source§impl<'a> HasModules<'a> for LuaMachine<'a>
impl<'a> HasModules<'a> for LuaMachine<'a>
source§impl<'a> HasStringMap<'a> for LuaMachine<'a>
impl<'a> HasStringMap<'a> for LuaMachine<'a>
source§fn 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>
source§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>: for<'e> 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>: for<'e> MaybeStringMap<Value<'e> = Self::Datum<'b, 'e>>,
source§impl<'a> Hash for LuaMachine<'a>
impl<'a> Hash for LuaMachine<'a>
source§impl<'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>
§type Function<'b>
where
Self: 'b = LuaFunction<'a, 'b>
type Function<'b> where Self: 'b = LuaFunction<'a, 'b>
Compile::compile
)
which can be executed by the machinesource§impl<'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