pub struct Chunk<'a> { /* private fields */ }
Expand description
Returned from Lua::load
and is used to finalize loading and executing Lua main chunks.
Implementations§
Source§impl Chunk<'_>
impl Chunk<'_>
Sourcepub fn set_name(self, name: impl Into<StdString>) -> Self
pub fn set_name(self, name: impl Into<StdString>) -> Self
Sets the name of this chunk, which results in more informative error traces.
Possible name prefixes:
@
- file path (when truncation is needed, the end of the file path is kept, as this is more useful for identifying the file)=
- custom chunk name (when truncation is needed, the beginning of the name is kept)
Sourcepub fn environment(&self) -> Option<&Table>
pub fn environment(&self) -> Option<&Table>
Returns the environment of this chunk.
Sourcepub fn set_environment(self, env: Table) -> Self
pub fn set_environment(self, env: Table) -> Self
Sets the environment of the loaded chunk to the given value.
In Lua >=5.2 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 set_mode(self, mode: ChunkMode) -> Self
pub fn set_mode(self, mode: ChunkMode) -> Self
Sets whether the chunk is text or binary (autodetected by default).
Be aware, Lua does not check the consistency of the code inside binary chunks. Running maliciously crafted bytecode can crash the interpreter.
Sourcepub fn set_compiler(self, compiler: Compiler) -> Self
Available on crate feature luau
only.
pub fn set_compiler(self, compiler: Compiler) -> Self
luau
only.Sets or overwrites a Luau compiler used for this chunk.
See Compiler
for details and possible options.
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 async fn exec_async(self) -> Result<()>
Available on crate feature async
only.
pub async fn exec_async(self) -> Result<()>
async
only.Asynchronously execute this chunk of code.
See exec
for more details.
Sourcepub fn eval<R: FromLuaMulti>(self) -> Result<R>
pub fn eval<R: FromLuaMulti>(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 async fn eval_async<R>(self) -> Result<R>where
R: FromLuaMulti,
Available on crate feature async
only.
pub async fn eval_async<R>(self) -> Result<R>where
R: FromLuaMulti,
async
only.Asynchronously evaluate the chunk as either an expression or block.
See eval
for more details.
Sourcepub fn call<R: FromLuaMulti>(self, args: impl IntoLuaMulti) -> Result<R>
pub fn call<R: FromLuaMulti>(self, args: impl IntoLuaMulti) -> Result<R>
Load the chunk function and call it with the given arguments.
This is equivalent to into_function
and calling the resulting function.
Sourcepub async fn call_async<R>(self, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
Available on crate feature async
only.
pub async fn call_async<R>(self, args: impl IntoLuaMulti) -> Result<R>where
R: FromLuaMulti,
async
only.Load the chunk function and asynchronously call it with the given arguments.
See call
for more details.
Sourcepub fn into_function(self) -> Result<Function>
pub fn into_function(self) -> Result<Function>
Load this chunk into a regular Function
.
This simply compiles the chunk without actually executing it.
Auto Trait Implementations§
impl<'a> Freeze for Chunk<'a>
impl<'a> !RefUnwindSafe for Chunk<'a>
impl<'a> Send for Chunk<'a>
impl<'a> Sync for Chunk<'a>
impl<'a> Unpin for Chunk<'a>
impl<'a> !UnwindSafe for Chunk<'a>
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more