Struct rlua::prelude::LuaChunk

source ·
pub struct LuaChunk<'lua, 'a> { /* private fields */ }
Expand description

Returned from Lua::load and is used to finalize loading and executing Lua main chunks.

Implementations§

source§

impl<'lua, 'a> Chunk<'lua, 'a>

source

pub fn set_name(self, name: impl Into<String>) -> Chunk<'lua, 'a>

Sets the name of this chunk, which results in more informative error traces.

source

pub fn set_environment<V>(self, env: V) -> Chunk<'lua, 'a>
where V: IntoLua<'lua>,

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.

source

pub fn set_mode(self, mode: ChunkMode) -> Chunk<'lua, 'a>

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.

source

pub fn exec(self) -> Result<(), Error>

Execute this chunk of code.

This is equivalent to calling the chunk function with no arguments and no return values.

source

pub fn eval<R>(self) -> Result<R, Error>
where R: FromLuaMulti<'lua>,

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.

source

pub fn call<A, R>(self, args: A) -> Result<R, Error>
where A: IntoLuaMulti<'lua>, R: FromLuaMulti<'lua>,

Load the chunk function and call it with the given arguments.

This is equivalent to into_function and calling the resulting function.

source

pub fn into_function(self) -> Result<Function<'lua>, Error>

Load this chunk into a regular Function.

This simply compiles the chunk without actually executing it.

Auto Trait Implementations§

§

impl<'lua, 'a> !RefUnwindSafe for Chunk<'lua, 'a>

§

impl<'lua, 'a> !Send for Chunk<'lua, 'a>

§

impl<'lua, 'a> !Sync for Chunk<'lua, 'a>

§

impl<'lua, 'a> Unpin for Chunk<'lua, 'a>

§

impl<'lua, 'a> !UnwindSafe for Chunk<'lua, 'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.