Skip to main content

rustia/
profile.rs

1/// Selects language compatibility and the allowed host capabilities.
2///
3/// Use builder presets for ordinary setup. `live()` uses
4/// [`ReproducibleStandard`](Self::ReproducibleStandard) with real host services.
5#[non_exhaustive]
6#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
7pub enum Profile {
8    /// Standalone Luau compatibility, including Luau-style table iteration.
9    LuauCompat,
10    /// Standalone Luau with reproducible defaults and insertion-ordered tables.
11    #[default]
12    ReproducibleStandard,
13    /// Replay-oriented execution that rejects external mutable-state capabilities.
14    DeterministicSim,
15    /// PUC Lua 5.1.5 table layout, sorting, and number formatting.
16    Puc51Compat,
17}
18
19impl Profile {
20    pub const fn is_deterministic_sim(self) -> bool {
21        matches!(self, Self::DeterministicSim)
22    }
23
24    pub const fn is_luau_compat(self) -> bool {
25        matches!(self, Self::LuauCompat)
26    }
27
28    pub const fn is_puc51_compat(self) -> bool {
29        matches!(self, Self::Puc51Compat)
30    }
31
32    pub(crate) const fn to_core(self) -> rustia_vm::Profile {
33        match self {
34            Self::LuauCompat => rustia_vm::Profile::LuauCompat,
35            Self::ReproducibleStandard => rustia_vm::Profile::ReproducibleStandard,
36            Self::DeterministicSim => rustia_vm::Profile::DeterministicSim,
37            Self::Puc51Compat => rustia_vm::Profile::Puc51Compat,
38        }
39    }
40
41    pub(crate) const fn from_core(profile: rustia_vm::Profile) -> Self {
42        match profile {
43            rustia_vm::Profile::LuauCompat => Self::LuauCompat,
44            rustia_vm::Profile::ReproducibleStandard => Self::ReproducibleStandard,
45            rustia_vm::Profile::DeterministicSim => Self::DeterministicSim,
46            rustia_vm::Profile::Puc51Compat => Self::Puc51Compat,
47        }
48    }
49}
50
51/// The script environment selected by an execution preset.
52#[non_exhaustive]
53#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
54pub enum GuestSurface {
55    /// The selected profile's ordinary globals and libraries.
56    #[default]
57    Standard,
58    /// The frozen, restricted environment used for stateless decisions.
59    HardenedLeaf,
60}
61
62impl GuestSurface {
63    pub const fn is_hardened_leaf(self) -> bool {
64        matches!(self, Self::HardenedLeaf)
65    }
66
67    pub(crate) const fn to_core(self) -> rustia_vm::GuestSurface {
68        match self {
69            Self::Standard => rustia_vm::GuestSurface::Standard,
70            Self::HardenedLeaf => rustia_vm::GuestSurface::HardenedLeaf,
71        }
72    }
73
74    pub(crate) const fn from_core(surface: rustia_vm::GuestSurface) -> Self {
75        match surface {
76            rustia_vm::GuestSurface::Standard => Self::Standard,
77            rustia_vm::GuestSurface::HardenedLeaf => Self::HardenedLeaf,
78        }
79    }
80}
81
82/// A Rust callback's declared access to application state and effects.
83///
84/// Rustia checks the declaration against the selected profile. Captured state
85/// remains the application's responsibility.
86#[non_exhaustive]
87#[derive(Clone, Copy, Debug, Eq, PartialEq)]
88pub enum OpCapability {
89    Pure,
90    ReadsDeterministicInput,
91    ReadsSlotState,
92    EmitsDeterministicEffect,
93    WritesSlotEffect,
94    ReadsExternalMutableState,
95    NondeterministicEffect,
96}
97
98impl OpCapability {
99    /// Stable tag for replay or package identities; independent of enum layout.
100    pub const fn identity_tag(self) -> u32 {
101        match self {
102            Self::Pure => 1,
103            Self::ReadsDeterministicInput => 2,
104            Self::ReadsSlotState => 3,
105            Self::EmitsDeterministicEffect => 4,
106            Self::WritesSlotEffect => 5,
107            Self::ReadsExternalMutableState => 6,
108            Self::NondeterministicEffect => 7,
109        }
110    }
111
112    pub(crate) const fn to_core(self) -> rustia_vm::OpCapability {
113        match self {
114            Self::Pure => rustia_vm::OpCapability::Pure,
115            Self::ReadsDeterministicInput => rustia_vm::OpCapability::ReadsDeterministicInput,
116            Self::ReadsSlotState => rustia_vm::OpCapability::ReadsSlotState,
117            Self::EmitsDeterministicEffect => rustia_vm::OpCapability::EmitsDeterministicEffect,
118            Self::WritesSlotEffect => rustia_vm::OpCapability::WritesSlotEffect,
119            Self::ReadsExternalMutableState => rustia_vm::OpCapability::ReadsExternalMutableState,
120            Self::NondeterministicEffect => rustia_vm::OpCapability::NondeterministicEffect,
121        }
122    }
123
124    pub(crate) const fn from_core(capability: rustia_vm::OpCapability) -> Self {
125        match capability {
126            rustia_vm::OpCapability::Pure => Self::Pure,
127            rustia_vm::OpCapability::ReadsDeterministicInput => Self::ReadsDeterministicInput,
128            rustia_vm::OpCapability::ReadsSlotState => Self::ReadsSlotState,
129            rustia_vm::OpCapability::EmitsDeterministicEffect => Self::EmitsDeterministicEffect,
130            rustia_vm::OpCapability::WritesSlotEffect => Self::WritesSlotEffect,
131            rustia_vm::OpCapability::ReadsExternalMutableState => Self::ReadsExternalMutableState,
132            rustia_vm::OpCapability::NondeterministicEffect => Self::NondeterministicEffect,
133        }
134    }
135}