Skip to main content

RustiaBuilder

Struct RustiaBuilder 

Source
pub struct RustiaBuilder { /* private fields */ }
Expand description

Chooses a script environment, resource limits, and host services.

Start with Rustia::builder for reproducible defaults, or use live for real clocks, entropy, and loadstring. deterministic rejects nondeterministic input providers and host capabilities. None of these presets sets a fuel or memory limit.

use rustia::Rustia;

let mut lua = Rustia::builder()
    .fuel(100_000)
    .memory_limit_bytes(4 * 1024 * 1024)
    .build()?;
assert_eq!(lua.load("return 6 * 7").eval::<i64>()?, 42);

Use build_for_host when callbacks need borrowed per-invocation state. The more restricted deterministic_leaf preset is for stateless decisions and requires a typed host plus an explicit memory limit.

Implementations§

Source§

impl RustiaBuilder

Source

pub const fn execution_profile(&self) -> RustiaExecutionProfile

Returns the typed core execution semantics currently selected by this builder.

Source

pub fn settings(&self) -> Settings

Returns the effective limits and service declarations selected so far.

Use Settings::identity_bytes when configuration is part of a replay or package identity. Provider data and application code remain separate identity inputs.

Source

pub fn standard() -> Self

Starts with reproducible inputs and the standard script environment.

This uses Profile::ReproducibleStandard: clocks use deterministic defaults, the random seed is zero, and loadstring is disabled. Script print writes to stdout on native/WASI targets and is silent on browser Wasm until a sink is supplied. Real clocks and other nondeterministic providers remain available when explicitly configured. Rustia::builder starts with this preset.

Source

pub fn deterministic() -> Self

Starts with Profile::DeterministicSim and no host services.

Nondeterministic input providers and host capabilities are rejected. Replay-stable providers and a print sink can still be added explicitly.

Source

pub fn deterministic_leaf() -> Self

Starts a restricted environment for independent, stateless decisions.

Uses GuestSurface::HardenedLeaf with Profile::DeterministicSim. Set an explicit memory_limit_bytes and use build_for_host. Execute host-owned decision bodies with Rustia::compile_entry and Rustia::invoke_entry_into.

Source

pub fn live() -> Self

Starts with real clocks, a fresh random seed, output, and loadstring.

Uses Profile::ReproducibleStandard with nondeterministic services; results are not guaranteed to repeat. loadstring lets scripts compile additional code at runtime, so choose this preset deliberately for untrusted scripts.

Native and WASI targets use their system services. With the default web-live feature, wasm32-unknown-unknown uses the JavaScript host (console, Date, performance, and crypto). Bare-Wasm builds can disable default features and plug the three input providers explicitly; build then verifies that none are missing.

Source

pub fn puc51_compat() -> Self

Starts a Lua 5.1 compatibility environment with loadstring enabled.

The profile alone reproduces PUC 5.1.5 table layout, number formatting, and table.sort; this preset adds loadstring so PUC-era code that compiles chunks at runtime works out of the box. Runtime compilation is an extra capability for untrusted scripts; enable it where Lua 5.1 source compatibility requires it. Host services otherwise use the defaults from standard.

Source

pub fn profile(self, profile: Profile) -> Self

Selects the core compatibility and capability policy. Existing settings and services are retained and checked when building.

Source

pub fn diagnostic_limits(self, limits: DiagnosticLimits) -> Self

Configures host-side reports independently of guest heap and fuel limits.

Source

pub fn fuel(self, fuel: u64) -> Self

Limits the instruction work allowed during execution.

Direct Rustia<()> entry points use this as their top-level budget. A typed invocation uses the lower of this ceiling and its explicit crate::InvocationLimits fuel, across every guest call in that invocation. Fuel measures deterministic work rather than elapsed time; script pcall cannot catch fuel exhaustion. The default is unlimited.

Source

pub fn recursion_limit(self, limit: usize) -> Self

Limits nested script calls. The default and maximum are 512.

Larger values are clamped to the engine maximum. Exhaustion returns a distinct error that script pcall cannot catch.

Source

pub fn memory_limit_bytes(self, bytes: usize) -> Self

Limits the VM’s accounted live memory. The default is unlimited.

Accounting is deterministic across native and Wasm targets. It does not bound total process memory, allocator capacity, or memory captured by Rust callbacks. Allocations can fail once the limit is reached.

Source

pub fn memory_tracking(self, enabled: bool) -> Self

Enables tracking of live and reserved memory peaks at instruction boundaries.

Source

pub fn extension(self, extension: RustiaExtension) -> Self

Adds a group of Rust callbacks to install when the VM is built. For a typed host, use crate::HostModule after building instead.

Source

pub fn stdout(self, sink: impl FnMut(&str) + 'static) -> Self

Chooses where script print output goes.

Each call sends one string, with arguments joined by tabs and a trailing newline. The closure cannot return an error. Output sinks are allowed in every ordinary profile, including Profile::DeterministicSim.

Source

pub fn without_stdout(self) -> Self

Discards script print output by removing the configured sink.

Source

pub fn module_resolver(self, resolver: ServiceModuleResolver) -> Self

Installs the host-owned resolver behind script-visible require.

The resolver chooses a canonical module identity and source for every request. Successful results are cached by identity within the calling function’s module cohort: the cache shared by a module and its dependencies. Each installation starts a fresh cohort; retained functions keep using their original cohort. A module can therefore execute again in a new installation before Rustia::reset_decision clears all guest state. See ServiceModuleResolver for loading, failure, and suspension rules.

Source

pub fn wall_clock(self, clock: ServiceClock) -> Self

Supplies the clock for no-argument os.time() and the default os.date timestamp. A nondeterministic provider is rejected at build on DeterministicSim.

Source

pub fn monotonic_clock(self, clock: ServiceClock) -> Self

Supplies the clock for os.clock(), overriding the VM’s built-in logical clock. Same DeterministicSim validation as wall_clock.

Source

pub fn entropy(self, entropy: ServiceEntropy) -> Self

Supplies the seed read once at VM construction for the initial math.random sequence. Calling math.randomseed later still replaces that state. A nondeterministic provider is rejected by deterministic.

Source

pub fn build(self) -> Result<Rustia>

Validates the configuration and builds a VM without a typed host context.

Returns an error for incompatible services, invalid extensions, or a hardened-leaf preset, which requires build_for_host.

Source

pub fn build_for_host<H>(self) -> Result<Rustia<H>>
where H: HostSchema,

Builds a typed VM while preserving this builder’s profile, settings, and host services.

Legacy RustiaExtension values deliberately do not cross into the typed capability surface; install a crate::HostModule<H> after building instead.

Auto Trait Implementations§

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>,

Source§

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>,

Source§

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.