pub struct Chunk<'lua, 'a> { /* private fields */ }
Expand description
Returned from Context::load
and is used to finalize loading and executing Lua main chunks.
Implementations§
source§impl<'lua, 'a> Chunk<'lua, 'a>
impl<'lua, 'a> Chunk<'lua, 'a>
sourcepub fn set_name<S: ?Sized + AsRef<[u8]>>(
self,
name: &S
) -> Result<Chunk<'lua, 'a>>
pub fn set_name<S: ?Sized + AsRef<[u8]>>( self, name: &S ) -> Result<Chunk<'lua, 'a>>
Sets the name of this chunk, which results in more informative error traces.
sourcepub fn set_environment<V: ToLua<'lua>>(self, env: V) -> Result<Chunk<'lua, 'a>>
pub fn set_environment<V: ToLua<'lua>>(self, env: V) -> Result<Chunk<'lua, 'a>>
Sets the first upvalue (_ENV
) of the loaded chunk to the given value.
Lua main chunks always have exactly one upvalue, and this upvalue is used as the _ENV
variable inside the chunk. By default this value is set to the global environment.
Calling this method changes the _ENV
upvalue to the value provided, and variables inside
the chunk will refer to the given environment rather than the global one.
All global variables (including the standard library!) are looked up in _ENV
, so it may be
necessary to populate the environment in order for scripts using custom environments to be
useful.
sourcepub fn exec(self) -> Result<()>
pub fn exec(self) -> Result<()>
Execute this chunk of code.
This is equivalent to calling the chunk function with no arguments and no return values.
sourcepub fn eval<R: FromLuaMulti<'lua>>(self) -> Result<R>
pub fn eval<R: FromLuaMulti<'lua>>(self) -> Result<R>
Evaluate the chunk as either an expression or block.
If the chunk can be parsed as an expression, this loads and executes the chunk and returns
the value that it evaluates to. Otherwise, the chunk is interpreted as a block as normal,
and this is equivalent to calling exec
.
sourcepub fn call<A: ToLuaMulti<'lua>, R: FromLuaMulti<'lua>>(
self,
args: A
) -> Result<R>
pub fn call<A: ToLuaMulti<'lua>, R: FromLuaMulti<'lua>>( self, args: A ) -> Result<R>
Load the chunk function and call it with the given arguemnts.
This is equivalent to into_function
and calling the resulting function.
sourcepub fn into_function(self) -> Result<Function<'lua>>
pub fn into_function(self) -> Result<Function<'lua>>
Load this chunk into a regular Function
.
This simply compiles the chunk without actually executing it.
sourcepub unsafe fn into_function_allow_binary(self) -> Result<Function<'lua>>
pub unsafe fn into_function_allow_binary(self) -> Result<Function<'lua>>
Load this chunk into a regular Function
.
This simply compiles the chunk without actually executing it.
Unlike into_function
, this method allows loading code previously
compiled and saved with Function::dump
or string.dump()
.
This method is unsafe because there is no check that the precompiled
Lua code is valid; if it is not this may cause a crash or other
undefined behaviour.