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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
use crate::{Error, Result};
use rustia_vm::{Error as VmError, ErrorKind as VmErrorKind, Value as RawValue, ValueKind, Vm};
use std::any::type_name;
use std::collections::BTreeMap;

const SNAPSHOT_DEPTH_LIMIT: usize = 64;

/// A Rust-owned copy of a script value.
///
/// Use this for ordinary data that should outlive the VM or a
/// [`reset_decision`](crate::Rustia::reset_decision). Tables are copied
/// recursively; changes to the original script table do not affect the copy.
///
/// This representation supports UTF-8 strings and acyclic tables with boolean,
/// number, or string keys. Use a live [`crate::Value`] for binary strings,
/// functions, buffers, vectors, userdata, or shared and cyclic table identity.
#[derive(Clone, Debug, PartialEq)]
pub enum OwnedValue {
    Nil,
    Boolean(bool),
    Number(f64),
    String(String),
    Table(OwnedTable),
}

/// A Rust-owned table, split into sequence values and keyed entries.
///
/// The `array` field uses zero-based Rust indices, corresponding to Luau
/// keys `1`, `2`, and so on. When a script table is copied, a contiguous sequence
/// becomes `array`; a sparse or mixed-key table becomes `entries`.
#[derive(Clone, Debug, Default, PartialEq)]
pub struct OwnedTable {
    /// Sequence values, starting at Luau index `1`.
    pub array: Vec<OwnedValue>,
    /// Keyed values in their snapshot order.
    pub entries: Vec<(OwnedKey, OwnedValue)>,
}

/// A key supported by an [`OwnedTable`].
#[derive(Clone, Debug, PartialEq)]
pub enum OwnedKey {
    Boolean(bool),
    Number(f64),
    String(String),
}

impl OwnedTable {
    /// Creates an empty table.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a sequence from values that are already converted.
    pub fn array(values: impl IntoIterator<Item = OwnedValue>) -> Self {
        Self {
            array: values.into_iter().collect(),
            entries: Vec::new(),
        }
    }

    /// Creates a table from keys and values that are already converted.
    pub fn entries(entries: impl IntoIterator<Item = (OwnedKey, OwnedValue)>) -> Self {
        Self {
            array: Vec::new(),
            entries: entries.into_iter().collect(),
        }
    }

    /// Looks up a string key without converting or cloning its value.
    pub fn get_string(&self, key: &str) -> Option<&OwnedValue> {
        self.entries.iter().find_map(|(entry_key, value)| {
            matches!(entry_key, OwnedKey::String(found) if found == key).then_some(value)
        })
    }

    /// Returns a borrowed table for a string key, or an error if the key holds a non-table value.
    pub fn get_string_table(&self, key: &str) -> Result<Option<&OwnedTable>> {
        self.get_string(key).map(expect_table_ref).transpose()
    }

    /// Returns an owned-array slot by zero-based Rust index. Luau index 1 is slot 0.
    pub fn get_array(&self, index: usize) -> Option<&OwnedValue> {
        self.array.get(index)
    }

    /// Returns a borrowed table for an array slot, or an error if the slot holds a non-table value.
    pub fn get_array_table(&self, index: usize) -> Result<Option<&OwnedTable>> {
        self.get_array(index).map(expect_table_ref).transpose()
    }
}

impl OwnedValue {
    /// Returns the corresponding Luau type name.
    pub fn type_name(&self) -> &'static str {
        match self {
            Self::Nil => "nil",
            Self::Boolean(_) => "boolean",
            Self::Number(_) => "number",
            Self::String(_) => "string",
            Self::Table(_) => "table",
        }
    }

    /// Borrows the table contents, or returns `None` for another value kind.
    pub fn table(&self) -> Option<&OwnedTable> {
        match self {
            Self::Table(table) => Some(table),
            _ => None,
        }
    }
}

/// Converts a Rust value to an owned script value.
///
/// Implementations include numbers, strings, `Option<T>`, `Vec<T>`, and
/// `BTreeMap<String, T>`. `None` becomes `nil`; vectors become one-based Luau
/// sequences. Integer conversions reject values that cannot be represented
/// exactly as a Luau number.
pub trait IntoRustia {
    /// Converts this value, returning an error if it cannot be represented.
    fn into_rustia(self) -> Result<OwnedValue>;
}

/// Converts an owned script value to a Rust value.
///
/// Conversions check the requested type rather than applying Luau coercions.
/// `Option<T>` accepts `nil` as `None`; integer types require a finite, whole
/// number in range. `Vec<T>` requires a sequence without keyed entries, while
/// `BTreeMap<String, T>` requires string keys without sequence entries.
pub trait FromRustia: Sized {
    /// Converts a value, returning an error for an incompatible type or range.
    fn from_rustia(value: OwnedValue) -> Result<Self>;
}

impl OwnedTable {
    /// Converts Rust values into a one-based Luau sequence.
    pub fn array_values<T>(values: impl IntoIterator<Item = T>) -> Result<Self>
    where
        T: IntoRustia,
    {
        Ok(Self {
            array: values
                .into_iter()
                .map(IntoRustia::into_rustia)
                .collect::<Result<Vec<_>>>()?,
            entries: Vec::new(),
        })
    }

    /// Converts Rust values into a table with string keys.
    pub fn string_entries<K, V>(entries: impl IntoIterator<Item = (K, V)>) -> Result<Self>
    where
        K: Into<String>,
        V: IntoRustia,
    {
        Ok(Self {
            array: Vec::new(),
            entries: entries
                .into_iter()
                .map(|(key, value)| {
                    value
                        .into_rustia()
                        .map(|value| (OwnedKey::String(key.into()), value))
                })
                .collect::<Result<Vec<_>>>()?,
        })
    }

    /// Reads and converts a string-keyed value. Missing keys return `None`.
    pub fn get_string_as<T>(&self, key: &str) -> Result<Option<T>>
    where
        T: FromRustia,
    {
        self.get_string(key)
            .cloned()
            .map(T::from_rustia)
            .transpose()
    }

    /// Converts an owned-array slot by zero-based Rust index. Luau index 1 is slot 0.
    pub fn get_array_as<T>(&self, index: usize) -> Result<Option<T>>
    where
        T: FromRustia,
    {
        self.get_array(index)
            .cloned()
            .map(T::from_rustia)
            .transpose()
    }
}

/// Converts function arguments or results to a list of script values.
///
/// A single value becomes one argument, `()` becomes no arguments, and tuples
/// through eight elements become separate arguments. A `Vec<T>` is one Luau
/// sequence table, not a list of arguments.
pub trait IntoRustiaMulti {
    /// Converts the complete argument or result list.
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>>;

    /// Returns Rust type names used by host-function signature metadata.
    fn rustia_type_names() -> Vec<&'static str>
    where
        Self: Sized,
    {
        vec![type_name::<Self>()]
    }
}

/// Converts a script's return values to a Rust value or tuple.
///
/// A single type reads the first result; tuples through eight elements read
/// successive results. Missing results are treated as `nil`, extra results are
/// ignored, and `()` discards all results. Each selected result still has to
/// satisfy its [`FromRustia`] conversion.
///
/// Owned execution methods copy every result into [`OwnedValue`] before calling
/// this trait, including results that will be discarded. Use live-value methods
/// when any result cannot be represented by [`OwnedValue`].
pub trait FromRustiaMulti: Sized {
    /// Converts the selected return values.
    fn from_rustia_multi(values: Vec<OwnedValue>) -> Result<Self>;
}

impl IntoRustia for OwnedValue {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(self)
    }
}

impl FromRustia for OwnedValue {
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        Ok(value)
    }
}

impl IntoRustia for OwnedTable {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::Table(self))
    }
}

impl FromRustia for OwnedTable {
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        match value {
            OwnedValue::Table(table) => Ok(table),
            other => type_error("table", &other),
        }
    }
}

impl IntoRustia for () {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::Nil)
    }
}

impl FromRustia for () {
    fn from_rustia(_value: OwnedValue) -> Result<Self> {
        Ok(())
    }
}

impl IntoRustia for bool {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::Boolean(self))
    }
}

impl FromRustia for bool {
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        match value {
            OwnedValue::Boolean(value) => Ok(value),
            other => type_error("boolean", &other),
        }
    }
}

impl IntoRustia for f64 {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::Number(self))
    }
}

impl FromRustia for f64 {
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        match value {
            OwnedValue::Number(value) => Ok(value),
            other => type_error("number", &other),
        }
    }
}

impl IntoRustia for f32 {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::Number(f64::from(self)))
    }
}

impl FromRustia for f32 {
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        f64::from_rustia(value).map(|value| value as f32)
    }
}

macro_rules! impl_signed_integer_value {
    ($($ty:ty),* $(,)?) => {
        $(
            impl IntoRustia for $ty {
                fn into_rustia(self) -> Result<OwnedValue> {
                    let number = self as f64;
                    if number as i128 != self as i128 {
                        return Err(Error::embed(format!(
                            "{} value {self} cannot be represented exactly as a Luau number",
                            stringify!($ty)
                        )));
                    }
                    Ok(OwnedValue::Number(number))
                }
            }

            impl FromRustia for $ty {
                fn from_rustia(value: OwnedValue) -> Result<Self> {
                    let number = f64::from_rustia(value)?;
                    if !number.is_finite() || number.fract() != 0.0 {
                        return Err(Error::embed(format!(
                            "expected integer-compatible number, got {number}"
                        )));
                    }
                    let integer = number as i128;
                    if integer < <$ty>::MIN as i128 || integer > <$ty>::MAX as i128 {
                        return Err(Error::embed(format!(
                            "number {number} is outside {} range",
                            stringify!($ty)
                        )));
                    }
                    Ok(integer as $ty)
                }
            }
        )*
    };
}

macro_rules! impl_unsigned_integer_value {
    ($($ty:ty),* $(,)?) => {
        $(
            impl IntoRustia for $ty {
                fn into_rustia(self) -> Result<OwnedValue> {
                    let number = self as f64;
                    if number as u128 != self as u128 {
                        return Err(Error::embed(format!(
                            "{} value {self} cannot be represented exactly as a Luau number",
                            stringify!($ty)
                        )));
                    }
                    Ok(OwnedValue::Number(number))
                }
            }

            impl FromRustia for $ty {
                fn from_rustia(value: OwnedValue) -> Result<Self> {
                    let number = f64::from_rustia(value)?;
                    if !number.is_finite() || number.fract() != 0.0 {
                        return Err(Error::embed(format!(
                            "expected integer-compatible number, got {number}"
                        )));
                    }
                    if number < 0.0 {
                        return Err(Error::embed(format!(
                            "number {number} is outside {} range",
                            stringify!($ty)
                        )));
                    }
                    let integer = number as u128;
                    if integer > <$ty>::MAX as u128 {
                        return Err(Error::embed(format!(
                            "number {number} is outside {} range",
                            stringify!($ty)
                        )));
                    }
                    Ok(integer as $ty)
                }
            }
        )*
    };
}

impl_signed_integer_value!(i8, i16, i32, i64, isize);
impl_unsigned_integer_value!(u8, u16, u32, u64, usize);

impl IntoRustia for String {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::String(self))
    }
}

impl IntoRustia for &str {
    fn into_rustia(self) -> Result<OwnedValue> {
        Ok(OwnedValue::String(self.to_owned()))
    }
}

impl FromRustia for String {
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        match value {
            OwnedValue::String(value) => Ok(value),
            other => type_error("string", &other),
        }
    }
}

impl<T> IntoRustia for Option<T>
where
    T: IntoRustia,
{
    fn into_rustia(self) -> Result<OwnedValue> {
        self.map_or(Ok(OwnedValue::Nil), IntoRustia::into_rustia)
    }
}

impl<T> FromRustia for Option<T>
where
    T: FromRustia,
{
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        match value {
            OwnedValue::Nil => Ok(None),
            value => T::from_rustia(value).map(Some),
        }
    }
}

impl<T> IntoRustia for Vec<T>
where
    T: IntoRustia,
{
    fn into_rustia(self) -> Result<OwnedValue> {
        OwnedTable::array_values(self).map(OwnedValue::Table)
    }
}

impl<T> FromRustia for Vec<T>
where
    T: FromRustia,
{
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        match value {
            OwnedValue::Table(table) if table.entries.is_empty() => {
                table.array.into_iter().map(T::from_rustia).collect()
            }
            OwnedValue::Table(_) => Err(Error::embed(
                "expected array table, got table with keyed entries".to_owned(),
            )),
            other => type_error("array table", &other),
        }
    }
}

impl<T> IntoRustia for BTreeMap<String, T>
where
    T: IntoRustia,
{
    fn into_rustia(self) -> Result<OwnedValue> {
        OwnedTable::string_entries(self).map(OwnedValue::Table)
    }
}

impl<T> FromRustia for BTreeMap<String, T>
where
    T: FromRustia,
{
    fn from_rustia(value: OwnedValue) -> Result<Self> {
        let table = OwnedTable::from_rustia(value)?;
        if !table.array.is_empty() {
            return Err(Error::embed(
                "expected string-keyed table, got table with array entries".to_owned(),
            ));
        }
        table
            .entries
            .into_iter()
            .map(|(key, value)| match key {
                OwnedKey::String(key) => T::from_rustia(value).map(|value| (key, value)),
                _ => Err(Error::embed(
                    "expected string-keyed table, got non-string key".to_owned(),
                )),
            })
            .collect()
    }
}

macro_rules! impl_single_multi {
    ($($ty:ty),* $(,)?) => {
        $(
            impl IntoRustiaMulti for $ty {
                fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
                    self.into_rustia().map(|value| vec![value])
                }
            }

            impl FromRustiaMulti for $ty {
                fn from_rustia_multi(mut values: Vec<OwnedValue>) -> Result<Self> {
                    let value = if values.is_empty() {
                        OwnedValue::Nil
                    } else {
                        values.remove(0)
                    };
                    Self::from_rustia(value)
                }
            }
        )*
    };
}

impl_single_multi!(
    OwnedValue, OwnedTable, bool, f64, f32, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize,
    String
);

impl IntoRustiaMulti for &str {
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
        self.into_rustia().map(|value| vec![value])
    }
}

impl<T> IntoRustiaMulti for Option<T>
where
    T: IntoRustia,
{
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
        self.into_rustia().map(|value| vec![value])
    }
}

impl<T> FromRustiaMulti for Option<T>
where
    T: FromRustia,
{
    fn from_rustia_multi(mut values: Vec<OwnedValue>) -> Result<Self> {
        let value = if values.is_empty() {
            OwnedValue::Nil
        } else {
            values.remove(0)
        };
        Self::from_rustia(value)
    }
}

impl<T> IntoRustiaMulti for Vec<T>
where
    T: IntoRustia,
{
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
        self.into_rustia().map(|value| vec![value])
    }
}

impl<T> FromRustiaMulti for Vec<T>
where
    T: FromRustia,
{
    fn from_rustia_multi(mut values: Vec<OwnedValue>) -> Result<Self> {
        let value = if values.is_empty() {
            OwnedValue::Nil
        } else {
            values.remove(0)
        };
        Self::from_rustia(value)
    }
}

impl<T> IntoRustiaMulti for BTreeMap<String, T>
where
    T: IntoRustia,
{
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
        self.into_rustia().map(|value| vec![value])
    }
}

impl<T> FromRustiaMulti for BTreeMap<String, T>
where
    T: FromRustia,
{
    fn from_rustia_multi(mut values: Vec<OwnedValue>) -> Result<Self> {
        let value = if values.is_empty() {
            OwnedValue::Nil
        } else {
            values.remove(0)
        };
        Self::from_rustia(value)
    }
}

impl IntoRustiaMulti for () {
    fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
        Ok(Vec::new())
    }

    fn rustia_type_names() -> Vec<&'static str> {
        Vec::new()
    }
}

impl FromRustiaMulti for () {
    fn from_rustia_multi(_values: Vec<OwnedValue>) -> Result<Self> {
        Ok(())
    }
}

macro_rules! impl_tuple_multi {
    ($($name:ident),+ $(,)?) => {
        impl<$($name),+> IntoRustiaMulti for ($($name,)+)
        where
            $($name: IntoRustia,)+
        {
            #[allow(non_snake_case)]
            fn into_rustia_multi(self) -> Result<Vec<OwnedValue>> {
                let ($($name,)+) = self;
                Ok(vec![$($name.into_rustia()?,)+])
            }

            fn rustia_type_names() -> Vec<&'static str> {
                vec![$(type_name::<$name>()),+]
            }
        }

        impl<$($name),+> FromRustiaMulti for ($($name,)+)
        where
            $($name: FromRustia,)+
        {
            #[allow(non_snake_case)]
            fn from_rustia_multi(values: Vec<OwnedValue>) -> Result<Self> {
                let mut values = values.into_iter();
                Ok(($({
                    let value = values.next().unwrap_or(OwnedValue::Nil);
                    $name::from_rustia(value)?
                },)+))
            }
        }
    };
}

impl_tuple_multi!(A);
impl_tuple_multi!(A, B);
impl_tuple_multi!(A, B, C);
impl_tuple_multi!(A, B, C, D);
impl_tuple_multi!(A, B, C, D, E);
impl_tuple_multi!(A, B, C, D, E, F);
impl_tuple_multi!(A, B, C, D, E, F, G);
impl_tuple_multi!(A, B, C, D, E, F, G, H);

pub(crate) fn snapshot_value(vm: &mut Vm, value: RawValue) -> Result<OwnedValue> {
    snapshot_value_inner(vm, value, 0)
}

fn snapshot_value_inner(vm: &mut Vm, value: RawValue, depth: usize) -> Result<OwnedValue> {
    if depth > SNAPSHOT_DEPTH_LIMIT {
        return Err(Error::embed(
            "value snapshot exceeded recursive table depth limit".to_owned(),
        ));
    }

    match value.kind() {
        ValueKind::Nil => Ok(OwnedValue::Nil),
        ValueKind::Boolean => Ok(OwnedValue::Boolean(value.as_bool().unwrap_or(false))),
        ValueKind::Number => Ok(OwnedValue::Number(value.as_number().unwrap_or(f64::NAN))),
        ValueKind::String => vm
            .string_value(value)
            .map_err(Error::from_engine)?
            .map(OwnedValue::String)
            .ok_or_else(|| Error::embed("string value is missing".to_owned())),
        ValueKind::Table => snapshot_table(vm, value, depth + 1),
        ValueKind::Function => Err(Error::embed(
            "function values must be retained as Function handles".to_owned(),
        )),
        ValueKind::Userdata | ValueKind::Buffer | ValueKind::Vector => Err(Error::embed(format!(
            "{} values cannot be represented as OwnedValue",
            raw_kind_name(value.kind())
        ))),
        _ => Err(Error::embed(
            "unknown VM value kind cannot be represented as OwnedValue".to_owned(),
        )),
    }
}

fn snapshot_table(vm: &mut Vm, value: RawValue, depth: usize) -> Result<OwnedValue> {
    let mut ordered = Vec::new();
    let mut all_array_keys = true;

    for (key, value) in vm
        .raw_table_entries_value(value)
        .map_err(Error::from_engine)?
    {
        let key = snapshot_key(vm, key)?;
        let array_index = match key {
            OwnedKey::Number(number) => number_to_array_index(number),
            _ => None,
        };
        if array_index.is_none() {
            all_array_keys = false;
        }
        ordered.push((
            key,
            snapshot_value_inner(vm, value, depth + 1)?,
            array_index,
        ));
    }

    if all_array_keys {
        let mut array_entries = ordered
            .iter()
            .filter_map(|(_, value, index)| index.map(|index| (index, value.clone())))
            .collect::<Vec<_>>();
        array_entries.sort_by_key(|(index, _)| *index);
        let contiguous = array_entries
            .iter()
            .enumerate()
            .all(|(position, (index, _))| *index == position + 1);
        if contiguous {
            let array = array_entries
                .into_iter()
                .map(|(_, value)| value)
                .collect::<Vec<_>>();
            return Ok(OwnedValue::Table(OwnedTable {
                array,
                entries: Vec::new(),
            }));
        }
    }

    Ok(OwnedValue::Table(OwnedTable {
        array: Vec::new(),
        entries: ordered
            .into_iter()
            .map(|(key, value, _)| (key, value))
            .collect(),
    }))
}

fn snapshot_key(vm: &mut Vm, key: RawValue) -> Result<OwnedKey> {
    match key.kind() {
        ValueKind::Boolean => Ok(OwnedKey::Boolean(key.as_bool().unwrap_or(false))),
        ValueKind::Number => Ok(OwnedKey::Number(key.as_number().unwrap_or(f64::NAN))),
        ValueKind::String => vm
            .string_value(key)
            .map_err(Error::from_engine)?
            .map(OwnedKey::String)
            .ok_or_else(|| Error::embed("string key is missing".to_owned())),
        _ => Err(Error::embed(format!(
            "{} table keys cannot be represented as OwnedKey",
            raw_kind_name(key.kind())
        ))),
    }
}

pub(crate) fn load_owned_value(vm: &mut Vm, value: &OwnedValue) -> Result<RawValue> {
    match value {
        OwnedValue::Nil => Ok(RawValue::nil()),
        OwnedValue::Boolean(value) => Ok(RawValue::boolean(*value)),
        OwnedValue::Number(value) => Ok(RawValue::number(*value)),
        OwnedValue::String(value) => Ok(vm.string(value)),
        OwnedValue::Table(table) => {
            let raw_table = vm.empty_table();
            // Nested values can force quota collection before they are attached
            // to this table. Keep the partial parent reachable throughout.
            let _table_root = vm.retain_value(raw_table).map_err(Error::from_engine)?;
            for (index, value) in table.array.iter().enumerate() {
                let key = RawValue::number((index + 1) as f64);
                let value = load_owned_value(vm, value)?;
                vm.raw_table_set_value(raw_table, key, value)
                    .map_err(Error::from_engine)?;
            }
            for (key, value) in &table.entries {
                let key = load_owned_key(vm, key);
                let _key_root = if key.kind() == ValueKind::String {
                    Some(vm.retain_value(key).map_err(Error::from_engine)?)
                } else {
                    None
                };
                let value = load_owned_value(vm, value)?;
                vm.raw_table_set_value(raw_table, key, value)
                    .map_err(Error::from_engine)?;
            }
            Ok(raw_table)
        }
    }
}

/// Keeps earlier arguments alive while later owned graphs allocate or collect.
pub(crate) fn load_owned_arguments(
    vm: &mut Vm,
    values: &[OwnedValue],
) -> Result<(Vec<RawValue>, Vec<rustia_vm::RetainedValue>)> {
    let mut arguments = Vec::with_capacity(values.len());
    let mut roots = Vec::new();
    for value in values {
        let value = load_owned_value(vm, value)?;
        if !matches!(
            value.kind(),
            ValueKind::Nil | ValueKind::Boolean | ValueKind::Number
        ) {
            roots.push(vm.retain_value(value).map_err(Error::from_engine)?);
        }
        arguments.push(value);
    }
    Ok((arguments, roots))
}

fn load_owned_key(vm: &mut Vm, key: &OwnedKey) -> RawValue {
    match key {
        OwnedKey::Boolean(value) => RawValue::boolean(*value),
        OwnedKey::Number(value) => RawValue::number(*value),
        OwnedKey::String(value) => vm.string(value),
    }
}

fn number_to_array_index(number: f64) -> Option<usize> {
    if !number.is_finite() || number.fract() != 0.0 || number < 1.0 {
        return None;
    }
    if number > usize::MAX as f64 {
        return None;
    }
    Some(number as usize)
}

fn expect_table_ref(value: &OwnedValue) -> Result<&OwnedTable> {
    match value {
        OwnedValue::Table(table) => Ok(table),
        other => type_error("table", other),
    }
}

fn type_error<T>(expected: &'static str, value: &OwnedValue) -> Result<T> {
    Err(Error::embed(format!(
        "expected {expected}, got {}",
        value.type_name()
    )))
}

pub(crate) fn embed_to_vm_error(error: Error) -> VmError {
    match error {
        Error {
            kind: crate::ErrorKind::Vm(error),
            ..
        } => (*error).into_core(),
        error => {
            let mut vm_error = VmError::new(VmErrorKind::RuntimeError {
                message: error.to_string(),
            });
            vm_error.diagnostic = Some(std::sync::Arc::new(rustia_vm::diagnostic::Diagnostic {
                host_cause: Some(std::sync::Arc::new(error)),
                ..rustia_vm::diagnostic::Diagnostic::default()
            }));
            vm_error
        }
    }
}

fn raw_kind_name(kind: ValueKind) -> &'static str {
    match kind {
        ValueKind::Nil => "nil",
        ValueKind::Boolean => "boolean",
        ValueKind::Number => "number",
        ValueKind::String => "string",
        ValueKind::Table => "table",
        ValueKind::Function => "function",
        ValueKind::Userdata => "userdata",
        ValueKind::Buffer => "buffer",
        ValueKind::Vector => "vector",
        _ => "unknown",
    }
}