rustia 0.2.0

Rustia is a pure-Rust interpreter for embedding standalone Luau in games and tools. Edit scripts without a rebuild and let players write mods. Rustia grew out of a game by Slowchop Studios that needed to script character decisions and replay them on desktop and in a browser.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
use crate::function::Function;
use crate::serde_value::from_live;
use crate::value::{load_owned_arguments, load_owned_value, snapshot_value};
use crate::{Error, HostSchema, IntoRustia, OwnedValue, Result, Rustia};
use rustia_vm::{RetainedValue, Value as RawValue, ValueKind, Vm};
use std::fmt;

/// The Luau type of a live [`Value`].
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum ValueType {
    Nil,
    Boolean,
    Number,
    String,
    Table,
    Function,
    Userdata,
    Buffer,
    Vector,
}

impl ValueType {
    /// Returns the Luau runtime type name.
    pub const fn name(self) -> &'static str {
        match self {
            Self::Nil => "nil",
            Self::Boolean => "boolean",
            Self::Number => "number",
            Self::String => "string",
            Self::Table => "table",
            Self::Function => "function",
            Self::Userdata => "userdata",
            Self::Buffer => "buffer",
            Self::Vector => "vector",
        }
    }
}

/// A script value kept alive in its original Rustia VM.
///
/// Unlike [`OwnedValue`], this is a handle rather than a copy. Tables, functions,
/// and userdata keep their identity; strings can contain arbitrary bytes; tables
/// can share values or refer to themselves. Cloning a handle keeps the same
/// value alive and does not copy its contents.
///
/// Every operation requires the VM that created the handle. A
/// [`Rustia::reset_decision`] invalidates all earlier handles; using one after
/// reset or with another VM returns an error. To keep ordinary data across a
/// reset, copy it with [`to_owned`](Self::to_owned) or
/// [`deserialize`](Self::deserialize).
#[derive(Clone)]
pub struct Value {
    value: RetainedValue,
    decision_epoch: u64,
}

impl Value {
    pub(crate) fn new(value: RetainedValue, decision_epoch: u64) -> Self {
        Self {
            value,
            decision_epoch,
        }
    }

    pub(crate) fn retain(vm: &mut Vm, value: RawValue) -> Result<Self> {
        let decision_epoch = vm.decision_epoch();
        vm.retain_value(value)
            .map(|value| Self::new(value, decision_epoch))
            .map_err(Error::from_engine)
    }

    pub(crate) fn retain_many(vm: &mut Vm, values: &[RawValue]) -> Result<Vec<Self>> {
        let decision_epoch = vm.decision_epoch();
        vm.retain_values(values)
            .map(|values| {
                values
                    .into_iter()
                    .map(|value| Self::new(value, decision_epoch))
                    .collect()
            })
            .map_err(Error::from_engine)
    }

    /// Returns the decision epoch in which this value was retained.
    pub const fn decision_epoch(&self) -> u64 {
        self.decision_epoch
    }

    /// Returns this value's runtime kind.
    pub fn kind<C>(&self, rustia: &Rustia<C>) -> Result<ValueType>
    where
        C: HostSchema,
    {
        let value = self.raw(rustia.vm(), "value handle")?;
        raw_value_type(value)
    }

    /// Returns this value's Luau runtime type name.
    pub fn type_name<C>(&self, rustia: &Rustia<C>) -> Result<&'static str>
    where
        C: HostSchema,
    {
        self.kind(rustia).map(ValueType::name)
    }

    /// Returns whether this is the nil value.
    pub fn is_nil<C>(&self, rustia: &Rustia<C>) -> Result<bool>
    where
        C: HostSchema,
    {
        self.kind(rustia).map(|kind| kind == ValueType::Nil)
    }

    /// Tests Luau truthiness: only `nil` and `false` are false.
    /// This does not invoke guest code.
    pub fn truthy<C>(&self, rustia: &Rustia<C>) -> Result<bool>
    where
        C: HostSchema,
    {
        self.raw(rustia.vm(), "value handle").map(RawValue::truthy)
    }

    /// Returns a boolean, or `None` for another value kind. No coercion is applied.
    pub fn as_boolean<C>(&self, rustia: &Rustia<C>) -> Result<Option<bool>>
    where
        C: HostSchema,
    {
        self.raw(rustia.vm(), "value handle").map(RawValue::as_bool)
    }

    /// Returns a number, or `None` for another value kind. No coercion is applied.
    pub fn as_number<C>(&self, rustia: &Rustia<C>) -> Result<Option<f64>>
    where
        C: HostSchema,
    {
        self.raw(rustia.vm(), "value handle")
            .map(RawValue::as_number)
    }

    /// Copies the exact binary string bytes, or returns `None` for another kind.
    pub fn string_bytes<C>(&self, rustia: &mut Rustia<C>) -> Result<Option<Vec<u8>>>
    where
        C: HostSchema,
    {
        let value = self.raw(rustia.vm(), "value handle")?;
        rustia
            .vm_mut()
            .string_value_bytes(value)
            .map_err(Error::from_engine)
    }

    /// Copies the current buffer bytes, or returns `None` for another kind.
    pub fn buffer_bytes<C>(&self, rustia: &mut Rustia<C>) -> Result<Option<Vec<u8>>>
    where
        C: HostSchema,
    {
        let value = self.raw(rustia.vm(), "value handle")?;
        rustia
            .vm_mut()
            .buffer_value_bytes(value)
            .map_err(Error::from_engine)
    }

    /// Returns the three vector components, or `None` for another value kind.
    pub fn vector_components<C>(&self, rustia: &mut Rustia<C>) -> Result<Option<[f32; 3]>>
    where
        C: HostSchema,
    {
        let value = self.raw(rustia.vm(), "value handle")?;
        rustia
            .vm_mut()
            .vector_value_components(value)
            .map_err(Error::from_engine)
    }

    /// Copies this value and any nested tables into Rust-owned data.
    ///
    /// Non-UTF-8 strings, buffers, vectors, userdata, functions, keys unsupported
    /// by [`crate::OwnedKey`], and cyclic graphs return conversion errors.
    /// Shared tables are copied independently; table identity is not preserved.
    /// Deeply nested tables also return an error once the snapshot depth limit
    /// is reached.
    pub fn to_owned<C>(&self, rustia: &mut Rustia<C>) -> Result<OwnedValue>
    where
        C: HostSchema,
    {
        let value = self.raw(rustia.vm(), "value handle")?;
        snapshot_value(rustia.vm_mut(), value)
    }

    /// Deserializes this value without first building an [`OwnedValue`] copy.
    ///
    /// The returned type must own its data; it cannot borrow strings or other
    /// contents from the VM.
    ///
    /// ```compile_fail
    /// use rustia::{Rustia, Value};
    ///
    /// fn cannot_borrow(value: &Value, rustia: &mut Rustia) {
    ///     let _: &str = value.deserialize(rustia).unwrap();
    /// }
    /// ```
    pub fn deserialize<T>(&self, rustia: &mut Rustia<impl HostSchema>) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        let value = self.raw(rustia.vm(), "value handle")?;
        from_live(rustia.vm_mut(), value)
    }

    /// Returns a table handle, or `None` if this is another value kind.
    pub fn as_table<C>(&self, rustia: &Rustia<C>) -> Result<Option<Table>>
    where
        C: HostSchema,
    {
        Ok((self.kind(rustia)? == ValueType::Table).then(|| Table::new(self.clone())))
    }

    /// Returns a function handle, or `None` if this is another value kind.
    pub fn as_function<C>(&self, rustia: &Rustia<C>) -> Result<Option<Function>>
    where
        C: HostSchema,
    {
        Ok((self.kind(rustia)? == ValueType::Function).then(|| Function::from_value(self.clone())))
    }

    /// Compares values using raw Luau equality without invoking `__eq`.
    pub fn raw_equal<C>(&self, rustia: &mut Rustia<C>, other: &Self) -> Result<bool>
    where
        C: HostSchema,
    {
        let lhs = self.raw(rustia.vm(), "left value handle")?;
        let rhs = other.raw(rustia.vm(), "right value handle")?;
        rustia
            .vm_mut()
            .raw_equal_values(lhs, rhs)
            .map_err(Error::from_engine)
    }

    pub(crate) fn raw(&self, vm: &Vm, label: &str) -> Result<RawValue> {
        self.value
            .value(vm)
            .map_err(|error| Error::from_engine(error).with_context(label))
    }
}

impl fmt::Debug for Value {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Value")
            .field("decision_epoch", &self.decision_epoch)
            .finish_non_exhaustive()
    }
}

/// A live table that Rust and Luau can both read and change.
///
/// Clones refer to the same table. All lookups and writes are raw: they bypass
/// `__index` and `__newindex`. Like [`Value`], a table belongs to one VM and
/// becomes invalid after [`Rustia::reset_decision`].
///
/// ```
/// use rustia::Rustia;
///
/// let mut lua = Rustia::builder().build()?;
/// let scores = lua.create_table()?;
/// scores.set(&mut lua, "Ada", 42)?;
/// assert_eq!(scores.get(&mut lua, "Ada")?.as_number(&lua)?, Some(42.0));
/// # Ok::<(), rustia::Error>(())
/// ```
#[derive(Clone, Debug)]
pub struct Table {
    value: Value,
}

impl Table {
    pub(crate) const fn new(value: Value) -> Self {
        Self { value }
    }

    pub(crate) fn raw(&self, vm: &Vm, label: &str) -> Result<RawValue> {
        self.value.raw(vm, label)
    }

    /// Clones this table's underlying live value handle.
    pub fn as_value(&self) -> Value {
        self.value.clone()
    }

    /// Deserializes this table without first building an [`OwnedValue`] copy.
    pub fn deserialize<T>(&self, rustia: &mut Rustia<impl HostSchema>) -> Result<T>
    where
        T: serde::de::DeserializeOwned,
    {
        self.value.deserialize(rustia)
    }

    /// Returns the decision epoch in which this handle was retained.
    pub const fn decision_epoch(&self) -> u64 {
        self.value.decision_epoch()
    }

    /// Looks up a live key without invoking `__index`. Missing keys return `nil`.
    pub fn get_value<C>(&self, rustia: &mut Rustia<C>, key: &Value) -> Result<Value>
    where
        C: HostSchema,
    {
        let table = self.value.raw(rustia.vm(), "table handle")?;
        let key = key.raw(rustia.vm(), "table key handle")?;
        let value = rustia
            .vm_mut()
            .raw_table_get_value(table, key)
            .map_err(Error::from_engine)?;
        Value::retain(rustia.vm_mut(), value)
    }

    /// Looks up a Rust key without invoking `__index`. Missing keys return `nil`.
    pub fn get<K, C>(&self, rustia: &mut Rustia<C>, key: K) -> Result<Value>
    where
        K: IntoRustia,
        C: HostSchema,
    {
        let table = self.value.raw(rustia.vm(), "table handle")?;
        let key = key.into_rustia()?;
        let key = load_owned_value(rustia.vm_mut(), &key)?;
        let value = rustia
            .vm_mut()
            .raw_table_get_value(table, key)
            .map_err(Error::from_engine)?;
        Value::retain(rustia.vm_mut(), value)
    }

    /// Performs a raw write with live key/value handles, bypassing `__newindex`.
    pub fn set_value<C>(&self, rustia: &mut Rustia<C>, key: &Value, value: &Value) -> Result<()>
    where
        C: HostSchema,
    {
        let table = self.value.raw(rustia.vm(), "table handle")?;
        let key = key.raw(rustia.vm(), "table key handle")?;
        let value = value.raw(rustia.vm(), "table value handle")?;
        rustia
            .vm_mut()
            .raw_table_set_value(table, key, value)
            .map_err(Error::from_engine)
    }

    /// Stores a live value under a Rust key without invoking `__newindex`.
    ///
    /// The stored value keeps its identity; it is not copied into a new object.
    pub fn set_live<K, C>(&self, rustia: &mut Rustia<C>, key: K, value: &Value) -> Result<()>
    where
        K: IntoRustia,
        C: HostSchema,
    {
        // Validate both live handles before key conversion can allocate or
        // mutate VM-owned storage. The retained handles keep these compact raw
        // values rooted if loading the owned key triggers quota collection.
        let table = self.value.raw(rustia.vm(), "table handle")?;
        let value = value.raw(rustia.vm(), "table value handle")?;
        let key = key.into_rustia()?;
        let key = load_owned_value(rustia.vm_mut(), &key)?;
        rustia
            .vm_mut()
            .raw_table_set_value(table, key, value)
            .map_err(Error::from_engine)
    }

    /// Converts and stores a Rust key and value without invoking `__newindex`.
    ///
    /// Use [`set_live`](Self::set_live) to store an existing script object.
    pub fn set<K, V, C>(&self, rustia: &mut Rustia<C>, key: K, value: V) -> Result<()>
    where
        K: IntoRustia,
        V: IntoRustia,
        C: HostSchema,
    {
        let table = self.value.raw(rustia.vm(), "table handle")?;
        let key = key.into_rustia()?;
        let value = value.into_rustia()?;
        let (values, _argument_roots) = load_owned_arguments(rustia.vm_mut(), &[key, value])?;
        rustia
            .vm_mut()
            .raw_table_set_value(table, values[0], values[1])
            .map_err(Error::from_engine)
    }

    /// Returns the current entries as live key/value handles. Array slots come
    /// first, in index order. The order of other keys is unspecified.
    ///
    /// Mutating the table later does not change which key/value handles this
    /// returned vector contains, though those handles still refer to their
    /// original live objects.
    pub fn entries<C>(&self, rustia: &mut Rustia<C>) -> Result<Vec<(Value, Value)>>
    where
        C: HostSchema,
    {
        let table = self.value.raw(rustia.vm(), "table handle")?;
        let entries = rustia
            .vm_mut()
            .raw_table_entries_value(table)
            .map_err(Error::from_engine)?;
        let raw_values = entries
            .into_iter()
            .flat_map(|(key, value)| [key, value])
            .collect::<Vec<_>>();
        let values = Value::retain_many(rustia.vm_mut(), &raw_values)?;
        Ok(values
            .chunks_exact(2)
            .map(|pair| (pair[0].clone(), pair[1].clone()))
            .collect())
    }
}

fn raw_value_type(value: RawValue) -> Result<ValueType> {
    Ok(match value.kind() {
        ValueKind::Nil => ValueType::Nil,
        ValueKind::Boolean => ValueType::Boolean,
        ValueKind::Number => ValueType::Number,
        ValueKind::String => ValueType::String,
        ValueKind::Table => ValueType::Table,
        ValueKind::Function => ValueType::Function,
        ValueKind::Userdata => ValueType::Userdata,
        ValueKind::Buffer => ValueType::Buffer,
        ValueKind::Vector => ValueType::Vector,
        _ => {
            return Err(Error::embed(
                "unknown VM value kind cannot be represented as a live value".to_owned(),
            ));
        }
    })
}