Skip to main content

mirascript_vm/value/arena/
manageable.rs

1use std::rc::Rc;
2
3use crate::{MiraArray, MiraFunction, MiraModule, MiraRecord, MiraType, MiraValue};
4
5/// An owned value waiting to be inserted into a Runtime arena.
6pub enum MiraManageable {
7    /// A value whose payload is already inline or already belongs to this Runtime.
8    Value(MiraValue),
9    /// An owned string.
10    String(String),
11    /// An owned array implementation.
12    Array(Box<dyn MiraArray>),
13    /// An owned record implementation.
14    Record(Box<dyn MiraRecord>),
15    /// An owned callable implementation.
16    Function(Rc<dyn MiraFunction>),
17    /// An owned module implementation.
18    Module(Box<dyn MiraModule>),
19}
20
21impl MiraManageable {
22    /// Return the MiraScript type of this value.
23    pub fn value_type(&self) -> MiraType {
24        match self {
25            Self::Value(value) => value.value_type(),
26            Self::String(_) => MiraType::String,
27            Self::Array(_) => MiraType::Array,
28            Self::Record(_) => MiraType::Record,
29            Self::Function(_) => MiraType::Function,
30            Self::Module(_) => MiraType::Module,
31        }
32    }
33}
34
35impl<T> From<T> for MiraManageable
36where
37    T: Into<MiraValue>,
38{
39    fn from(value: T) -> Self {
40        Self::Value(value.into())
41    }
42}
43
44impl TryFrom<MiraManageable> for MiraValue {
45    type Error = ();
46
47    fn try_from(value: MiraManageable) -> Result<Self, Self::Error> {
48        match value {
49            MiraManageable::Value(value) => Ok(value),
50            _ => Err(()),
51        }
52    }
53}
54
55impl From<String> for MiraManageable {
56    fn from(value: String) -> Self {
57        Self::String(value)
58    }
59}
60
61impl From<&str> for MiraManageable {
62    fn from(value: &str) -> Self {
63        Self::String(value.to_owned())
64    }
65}
66
67impl MiraManageable {
68    /// Wrap an array implementation for insertion into a Runtime.
69    pub fn from_array(value: impl MiraArray) -> Self {
70        Self::Array(Box::new(value))
71    }
72
73    /// Wrap a record implementation for insertion into a Runtime.
74    pub fn from_record(value: impl MiraRecord) -> Self {
75        Self::Record(Box::new(value))
76    }
77
78    /// Wrap a function implementation for insertion into a Runtime.
79    pub fn from_function(value: impl MiraFunction) -> Self {
80        Self::Function(Rc::new(value))
81    }
82
83    /// Wrap a module implementation for insertion into a Runtime.
84    pub fn from_module(value: impl MiraModule) -> Self {
85        Self::Module(Box::new(value))
86    }
87
88    /// Build a simple named module from already materialized Runtime values.
89    pub fn map_module(
90        name: impl Into<String>,
91        values: indexmap::IndexMap<String, MiraValue>,
92    ) -> Self {
93        crate::value::types::map_module(name, values)
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use crate::{Runtime, value::map_module};
100
101    use super::*;
102
103    #[test]
104    fn test_value() {
105        let value: MiraManageable = MiraValue::number(42.0).into();
106        assert_eq!(value.value_type(), MiraType::Number);
107        let mut runtime = Runtime::new();
108        runtime.insert_global("test_value", value).unwrap();
109        let result = runtime.eval_unchecked("test_value + 1");
110        assert_eq!(result.as_number_unchecked(), 43.0);
111    }
112
113    #[test]
114    fn test_string() {
115        let value: MiraManageable = "Hello".into();
116        assert_eq!(value.value_type(), MiraType::String);
117        let mut runtime = Runtime::new();
118        runtime.insert_global("test_string", value).unwrap();
119        let result = runtime.eval_unchecked("'$test_string World!'");
120        assert_eq!(result.as_str(&runtime).unwrap(), Some("Hello World!"));
121    }
122
123    #[test]
124    fn test_array() {
125        let value: MiraManageable =
126            MiraManageable::from_array(vec![MiraValue::number(1.0), MiraValue::number(2.0)]);
127        assert_eq!(value.value_type(), MiraType::Array);
128        let mut runtime = Runtime::new();
129        runtime.insert_global("test_array", value).unwrap();
130        let result = runtime.eval_unchecked("test_array[0] + test_array[1]");
131        assert_eq!(result.as_number_unchecked(), 3.0);
132    }
133
134    #[test]
135    fn test_record() {
136        let value: MiraManageable = MiraManageable::from_record(std::collections::HashMap::<
137            String,
138            MiraValue,
139        >::from_iter([
140            ("a".to_string(), MiraValue::number(1.0)),
141            ("b".to_string(), MiraValue::number(2.0)),
142        ]));
143        assert_eq!(value.value_type(), MiraType::Record);
144        let mut runtime = Runtime::new();
145        runtime.insert_global("test_record", value).unwrap();
146        let result = runtime.eval_unchecked("test_record.a + test_record.b");
147        assert_eq!(result.as_number_unchecked(), 3.0);
148    }
149
150    #[test]
151    fn test_function() {
152        let value: MiraManageable = MiraManageable::from_function(
153            |_: &mut Runtime, args: &[MiraValue]| -> crate::Result<MiraManageable> {
154                Ok(MiraValue::number(args.len() as f64).into())
155            },
156        );
157        assert_eq!(value.value_type(), MiraType::Function);
158        let mut runtime = Runtime::new();
159        runtime.insert_global("test_function", value).unwrap();
160        let result = runtime.eval_unchecked("test_function(1, 2, 3)");
161        assert_eq!(result.as_number_unchecked(), 3.0);
162    }
163
164    #[test]
165    fn test_module() {
166        let value: MiraManageable = map_module("test", indexmap::IndexMap::new());
167        assert_eq!(value.value_type(), MiraType::Module);
168        let mut runtime = Runtime::new();
169        runtime.insert_global("test_module", value).unwrap();
170        let result = runtime.eval_unchecked("test_module");
171        assert!(result.is_module());
172    }
173}