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
mod value;
mod value_handle;

pub use self::value::ArrayValue;
pub use self::value::BoolValue;
pub use self::value::FixnumValue;
pub use self::value::HashValue;
pub use self::value::NilValue;
pub use self::value::ObjectValue;
pub use self::value::StringValue;
pub use self::value::SymbolValue;
pub use self::value::UserDefinedValue;
pub use self::value::Value;
pub use self::value::ValueKind;
pub use self::value_handle::TypedValueHandle;
pub use self::value_handle::ValueHandle;
use slotmap::SlotMap;
use std::collections::HashMap;

/// An arena of Ruby values.
#[derive(Debug)]
pub struct ValueArena {
    arena: SlotMap<slotmap::DefaultKey, Value>,
    symbols: HashMap<Vec<u8>, TypedValueHandle<SymbolValue>>,
    root: ValueHandle,
}

impl ValueArena {
    /// Make a new empty [`ValueArena`].
    ///
    /// The root node is nil.
    pub fn new() -> Self {
        let mut arena = SlotMap::new();
        let symbols = HashMap::new();
        let root = ValueHandle::new(arena.insert(Value::Nil(NilValue)));

        Self {
            arena,
            symbols,
            root,
        }
    }

    /// Get the root [`ValueHandle`].
    pub fn root(&self) -> ValueHandle {
        self.root
    }

    /// Replace the current root, returning the old root.
    pub fn replace_root<H>(&mut self, new_root: H) -> ValueHandle
    where
        H: Into<ValueHandle>,
    {
        let mut new_root = new_root.into();

        std::mem::swap(&mut self.root, &mut new_root);
        new_root
    }

    /// Get a reference to the [`Value`] denoted by the given [`ValueHandle`].
    pub fn get<H>(&self, handle: H) -> Option<&Value>
    where
        H: Into<ValueHandle>,
    {
        self.arena.get(handle.into().index)
    }

    /// Get a mutable reference to the [`Value`] denoted by the given [`ValueHandle`].
    pub(crate) fn get_mut<H>(&mut self, handle: H) -> Option<&mut Value>
    where
        H: Into<ValueHandle>,
    {
        self.arena.get_mut(handle.into().index)
    }

    /// Get a reference to the [`SymbolValue`] denoted by the given [`TypedValueHandle`].
    ///
    /// # Panics
    /// Panics if the value is not a SymbolValue.
    pub fn get_symbol(&self, handle: TypedValueHandle<SymbolValue>) -> Option<&SymbolValue> {
        Some(self.get(handle)?.as_symbol().expect("not a symbol"))
    }

    /// Create an orphan `Nil` value and return the handle.
    pub fn create_nil(&mut self) -> TypedValueHandle<NilValue> {
        let index = self.arena.insert(Value::Nil(NilValue));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `Bool` value and return the handle.
    pub fn create_bool(&mut self, value: bool) -> TypedValueHandle<BoolValue> {
        let index = self.arena.insert(Value::Bool(BoolValue::new(value)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `Fixnum` value and return the handle.
    pub fn create_fixnum(&mut self, value: i32) -> TypedValueHandle<FixnumValue> {
        let index = self.arena.insert(Value::Fixnum(FixnumValue::new(value)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `Symbol` value and return the handle.
    ///
    /// If a symbol with this name already exists in this arena, it is returned instead of creating a new symbol.
    pub fn create_symbol(&mut self, value: Vec<u8>) -> TypedValueHandle<SymbolValue> {
        if let Some(handle) = self.symbols.get(&value) {
            return *handle;
        }

        self.create_new_symbol(value)
    }

    /// Create a new orphan `Symbol` value and return the handle.
    pub fn create_new_symbol(&mut self, value: Vec<u8>) -> TypedValueHandle<SymbolValue> {
        let index = self
            .arena
            .insert(Value::Symbol(SymbolValue::new(value.clone())));
        let handle = ValueHandle::new(index);
        let handle = TypedValueHandle::new_unchecked(handle);

        self.symbols.entry(value).or_insert(handle);

        handle
    }

    /// Create an orphan `Array` value and return the handle.
    pub fn create_array(&mut self, value: Vec<ValueHandle>) -> TypedValueHandle<ArrayValue> {
        let index = self.arena.insert(Value::Array(ArrayValue::new(value)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `Hash` value and return the handle.
    pub fn create_hash(
        &mut self,
        value: Vec<(ValueHandle, ValueHandle)>,
        default_value: Option<ValueHandle>,
    ) -> TypedValueHandle<HashValue> {
        let index = self
            .arena
            .insert(Value::Hash(HashValue::new(value, default_value)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `Object` value and return the handle.
    pub fn create_object(
        &mut self,
        name: TypedValueHandle<SymbolValue>,
        instance_variables: Vec<(TypedValueHandle<SymbolValue>, ValueHandle)>,
    ) -> TypedValueHandle<ObjectValue> {
        let index = self
            .arena
            .insert(Value::Object(ObjectValue::new(name, instance_variables)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `String` value and return the handle.
    pub fn create_string(&mut self, value: Vec<u8>) -> TypedValueHandle<StringValue> {
        let index = self.arena.insert(Value::String(StringValue::new(value)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }

    /// Create an orphan `UserDefined` value and return the handle.
    pub fn create_user_defined(
        &mut self,
        name: TypedValueHandle<SymbolValue>,
        value: Vec<u8>,
    ) -> TypedValueHandle<UserDefinedValue> {
        let index = self
            .arena
            .insert(Value::UserDefined(UserDefinedValue::new(name, value)));
        let handle = ValueHandle::new(index);

        TypedValueHandle::new_unchecked(handle)
    }
}

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

impl std::ops::Index<ValueHandle> for ValueArena {
    type Output = Value;

    fn index(&self, index: ValueHandle) -> &Self::Output {
        self.get(index).expect("missing value")
    }
}