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
//! Types used for lists/tuples.

use parking_lot::Mutex;
use refuse::Trace;

use crate::runtime::symbol::Symbol;
use crate::runtime::value::{
    CustomType, Dynamic, Rooted, RustFunctionTable, RustType, TypeRef, Value,
};
use crate::vm::{Fault, Register, VmContext};

/// The type definition that the [`List`] type uses.
///
/// In general, developers will not need this. However, if you are building your
/// own `core` module, this type can be used to populate `$.core.List`.
pub static LIST_TYPE: RustType<List> = RustType::new("List", |t| {
    t.with_construct(|_| {
        |vm, arity| {
            let list = List::new();
            for reg_index in 0..arity.0 {
                let value = vm[Register(reg_index)].take();
                list.push(value)?;
            }
            Ok(Dynamic::new(list, vm))
        }
    })
    .with_function_table(
        RustFunctionTable::new()
            .with_fn(
                Symbol::len_symbol(),
                0,
                |_vm: &mut VmContext<'_, '_>, this: &Rooted<List>| {
                    Value::try_from(this.0.lock().len())
                },
            )
            .with_fn(Symbol::set_symbol(), 2, |vm, this| {
                let index = vm[Register(0)].take();
                let value = vm[Register(1)].take();
                this.set(&index, value)?;
                Ok(value)
            })
            .with_fn(
                [Symbol::nth_symbol(), Symbol::get_symbol()],
                1,
                |vm, this| {
                    let key = vm[Register(0)].take();
                    this.get_by_value(&key)
                },
            ),
    )
});

/// A list of [`Value`]s.
#[derive(Debug)]
pub struct List(Mutex<Vec<Value>>);

impl List {
    /// Returns an empty list.
    #[must_use]
    pub const fn new() -> Self {
        Self(Mutex::new(Vec::new()))
    }

    /// Returns the value at `index`.
    ///
    /// # Errors
    ///
    /// Returns [`Fault::OutOfBounds`] if `index` cannot be converted to a
    /// `usize` or is out of bounds of this list.
    pub fn get_by_value(&self, index: &Value) -> Result<Value, Fault> {
        let Some(index) = index.as_usize() else {
            return Err(Fault::OutOfBounds);
        };
        self.get(index).ok_or(Fault::OutOfBounds)
    }

    pub fn get(&self, index: usize) -> Option<Value> {
        let contents = self.0.lock();

        contents.get(index).copied()
    }

    /// Inserts `value` at `index`.
    ///
    /// # Errors
    ///
    /// Returns [`Fault::OutOfBounds`] if `index` cannot be converted to a
    /// `usize` or is greater than the length of this list.
    pub fn insert(&self, index: &Value, value: Value) -> Result<(), Fault> {
        let mut contents = self.0.lock();
        contents.try_reserve(1).map_err(|_| Fault::OutOfMemory)?;
        match index.as_usize() {
            Some(index) if index <= contents.len() => {
                contents.insert(index, value);

                Ok(())
            }
            _ => Err(Fault::OutOfBounds),
        }
    }

    /// Pushes `value` to the end of the list.
    pub fn push(&self, value: Value) -> Result<(), Fault> {
        let mut contents = self.0.lock();
        contents.try_reserve(1).map_err(|_| Fault::OutOfMemory)?;
        contents.push(value);
        Ok(())
    }

    /// Replaces the value at `index` with `value`. Returns the previous value.
    ///
    /// # Errors
    ///
    /// Returns [`Fault::OutOfBounds`] if `index` cannot be converted to a
    /// `usize` or is out of bounds of this list.
    pub fn set(&self, index: &Value, value: Value) -> Result<Value, Fault> {
        let Some(index) = index.as_usize() else {
            return Err(Fault::OutOfBounds);
        };
        let mut contents = self.0.lock();

        if let Some(entry) = contents.get_mut(index) {
            Ok(std::mem::replace(entry, value))
        } else {
            Err(Fault::OutOfBounds)
        }
    }

    /// Converts this list into a Vec.
    pub fn to_vec(&self) -> Vec<Value> {
        self.0.lock().clone()
    }
}

impl Default for List {
    fn default() -> Self {
        Self::new()
    }
}

impl From<Vec<Value>> for List {
    fn from(value: Vec<Value>) -> Self {
        Self(Mutex::new(value))
    }
}

impl FromIterator<Value> for List {
    fn from_iter<T: IntoIterator<Item = Value>>(iter: T) -> Self {
        Self::from(Vec::from_iter(iter))
    }
}

impl CustomType for List {
    fn muse_type(&self) -> &TypeRef {
        &LIST_TYPE
    }
}

impl Trace for List {
    const MAY_CONTAIN_REFERENCES: bool = true;

    fn trace(&self, tracer: &mut refuse::Tracer) {
        self.0.lock().trace(tracer);
    }
}