pub struct Chunk<'rustia, 'source, C = ()>where
C: HostSchema,{ /* private fields */ }Expand description
Luau source prepared for one execution or module installation.
Created by Rustia::load. Give it a source_name for
useful diagnostics, then choose how to run it:
evalreturns ordinary Rust data.eval_serdedeserializes into a Rust structure.eval_livekeeps a script value alive without copying it.execdiscards the result.installreturns exports and rolls back guest state on failure.
Loading alone does not parse or execute the source. Each operation consumes
the chunk; use Rustia::compile_module to compile once and install repeatedly.
Implementations§
Source§impl<'rustia, 'source, C> Chunk<'rustia, 'source, C>where
C: HostSchema,
impl<'rustia, 'source, C> Chunk<'rustia, 'source, C>where
C: HostSchema,
Sourcepub fn source_name(self, source_name: impl Into<String>) -> Self
pub fn source_name(self, source_name: impl Into<String>) -> Self
Labels the source in errors and module resolution.
Use a filename or another meaningful label, such as "rules.luau".
The default is "chunk". This name is also the referrer passed to the
module resolver for require; setting it does not read a file.
Sourcepub fn source_map(self, mapping: SourceMap) -> Self
pub fn source_map(self, mapping: SourceMap) -> Self
Maps generated wrapper text out of human-facing diagnostics.
This only changes error presentation; it does not change script execution
or the source names and lines visible to scripts. See crate::SourceMap.
Sourcepub fn environment(self, environment: impl Into<Table>) -> Self
pub fn environment(self, environment: impl Into<Table>) -> Self
Selects the exact table used as this chunk’s lexical environment.
Cross-VM and reset-stale handles are rejected before parsing or
compilation. For install, mutations to this table
commit only when the complete installation succeeds.
This replaces the default environment; globals are inherited only if the
supplied table’s metatable provides that behavior.
Sourcepub fn eval<T>(self) -> Result<T>where
T: FromRustia,
pub fn eval<T>(self) -> Result<T>where
T: FromRustia,
Runs the script and converts its first result to Rust data.
Conversion uses FromRustia over an owned snapshot. No returned value
means Luau nil; extra results are ignored. Use eval_live
for functions and cyclic tables, or to preserve shared table identity.
Invalid source, execution failures, and incompatible result types return
Error. Script mutations remain in place even when execution or result
conversion fails; use install for guest-state rollback.
let scores: Vec<i32> = vm.load("return {10, 20, 30}").eval()?;
assert_eq!(scores, vec![10, 20, 30]);Sourcepub fn eval_live(self) -> Result<Value>
pub fn eval_live(self) -> Result<Value>
Runs the script and returns a live handle to its first result.
The handle keeps the value alive in this interpreter and preserves shared
and cyclic objects. It remains usable until Rustia::reset_decision,
and cannot be used with another interpreter. No returned value becomes
a handle to nil. As with eval, errors do not undo mutations.
Sourcepub fn eval_serde<T>(self) -> Result<T>where
T: DeserializeOwned,
pub fn eval_serde<T>(self) -> Result<T>where
T: DeserializeOwned,
Runs the script and deserializes its first result into Rust data.
Reads the live VM graph without an intermediate crate::OwnedValue.
The destination still owns its strings and collections. Invalid source,
execution failures, and data that does not match T return Error;
mutations remain in place on failure.
#[derive(serde::Deserialize)]
struct Settings {
volume: f64,
}
let settings: Settings = vm.load("return { volume = 0.5 }").eval_serde()?;
assert_eq!(settings.volume, 0.5);Sourcepub fn install(self) -> Result<Exports>
pub fn install(self) -> Result<Exports>
Runs the script as a module and keeps its returned exports alive.
Without environment, installation creates a
fresh module environment that inherits VM globals and binds _G to
itself. Compile, runtime, quota, and result-retention failures leave
prior guest-visible state unchanged.
External effects from Rust callbacks or the module resolver cannot be
undone. A successful installation does not invalidate earlier exports;
the application chooses when to replace its active handle.
let exports = vm.load("return { double = function(n) return n * 2 end }")
.source_name("math.luau")
.install()?;
let double = exports.function(&mut vm, "double")?;
let answer: i32 = double.call(&mut vm, (21,))?;
assert_eq!(answer, 42);