Skip to main content

seq_core/
value.rs

1use crate::seqstring::SeqString;
2use may::sync::mpmc;
3use std::collections::HashMap;
4use std::hash::{Hash, Hasher};
5use std::sync::Arc;
6
7/// Channel data: holds sender and receiver for direct handle passing
8///
9/// Both sender and receiver are Clone (MPMC), so duplicating a Channel value
10/// just clones the Arc. Send/receive operations use the handles directly
11/// with zero mutex overhead.
12#[derive(Debug)]
13pub struct ChannelData {
14    pub sender: mpmc::Sender<Value>,
15    pub receiver: mpmc::Receiver<Value>,
16}
17
18impl Clone for ChannelData {
19    fn clone(&self) -> Self {
20        Self {
21            sender: self.sender.clone(),
22            receiver: self.receiver.clone(),
23        }
24    }
25}
26
27// PartialEq by identity (Arc pointer comparison)
28impl PartialEq for ChannelData {
29    fn eq(&self, other: &Self) -> bool {
30        std::ptr::eq(self, other)
31    }
32}
33
34/// Message type for weave channels.
35///
36/// Using an enum instead of sentinel values ensures no collision with user data.
37/// Any `Value` can be safely yielded/resumed, including `i64::MIN`.
38#[derive(Debug, Clone, PartialEq)]
39pub enum WeaveMessage {
40    /// Normal value being yielded or resumed
41    Value(Value),
42    /// Weave completed naturally (sent on yield_chan)
43    Done,
44    /// Cancellation requested (sent on resume_chan)
45    Cancel,
46}
47
48/// Channel data specifically for weave communication.
49///
50/// Uses `WeaveMessage` instead of raw `Value` to support typed control flow.
51#[derive(Debug)]
52pub struct WeaveChannelData {
53    pub sender: mpmc::Sender<WeaveMessage>,
54    pub receiver: mpmc::Receiver<WeaveMessage>,
55}
56
57impl Clone for WeaveChannelData {
58    fn clone(&self) -> Self {
59        Self {
60            sender: self.sender.clone(),
61            receiver: self.receiver.clone(),
62        }
63    }
64}
65
66// PartialEq by identity (Arc pointer comparison)
67impl PartialEq for WeaveChannelData {
68    fn eq(&self, other: &Self) -> bool {
69        std::ptr::eq(self, other)
70    }
71}
72
73// Note: Arc is used for both Closure.env and Variant to enable O(1) cloning.
74// This is essential for functional programming with recursive data structures.
75
76/// MapKey: Hashable subset of Value for use as map keys
77///
78/// Only types that can be meaningfully hashed are allowed as map keys:
79/// Int, String, Bool. Float is excluded due to NaN equality issues.
80#[derive(Debug, Clone, PartialEq, Eq)]
81pub enum MapKey {
82    Int(i64),
83    String(SeqString),
84    Bool(bool),
85}
86
87impl Hash for MapKey {
88    fn hash<H: Hasher>(&self, state: &mut H) {
89        // Discriminant for type safety
90        std::mem::discriminant(self).hash(state);
91        match self {
92            MapKey::Int(n) => n.hash(state),
93            MapKey::String(s) => s.as_str().hash(state),
94            MapKey::Bool(b) => b.hash(state),
95        }
96    }
97}
98
99impl MapKey {
100    /// Try to convert a Value to a MapKey
101    /// Returns None for non-hashable types (Float, Variant, Quotation, Closure, Map)
102    pub fn from_value(value: &Value) -> Option<MapKey> {
103        match value {
104            Value::Int(n) => Some(MapKey::Int(*n)),
105            Value::String(s) => Some(MapKey::String(s.clone())),
106            Value::Bool(b) => Some(MapKey::Bool(*b)),
107            _ => None,
108        }
109    }
110
111    /// Convert MapKey back to Value
112    pub fn to_value(&self) -> Value {
113        match self {
114            MapKey::Int(n) => Value::Int(*n),
115            MapKey::String(s) => Value::String(s.clone()),
116            MapKey::Bool(b) => Value::Bool(*b),
117        }
118    }
119}
120
121/// Value: What the language talks about
122///
123/// This is pure data with no pointers to other values.
124/// Values can be pushed on the stack, stored in variants, etc.
125/// The key insight: Value is independent of Stack structure.
126///
127/// # Memory Layout
128///
129/// Using `#[repr(C)]` ensures a predictable C-compatible layout:
130/// - Discriminant (tag) at offset 0
131/// - Payload data follows at a fixed offset
132///
133/// This allows compiled code to write Values directly without FFI calls,
134/// enabling inline integer/boolean operations for better performance.
135#[repr(C)]
136#[derive(Debug, Clone, PartialEq)]
137pub enum Value {
138    /// Integer value
139    Int(i64),
140
141    /// Floating-point value (IEEE 754 double precision)
142    Float(f64),
143
144    /// Boolean value
145    Bool(bool),
146
147    /// String (arena or globally allocated via SeqString)
148    String(SeqString),
149
150    /// Symbol (identifier for dynamic variant construction)
151    /// Like Ruby/Clojure symbols - lightweight identifiers used for tags.
152    /// Note: Currently NOT interned (each symbol allocates). Interning may be
153    /// added in the future for O(1) equality comparison.
154    Symbol(SeqString),
155
156    /// Variant (sum type with tagged fields)
157    /// Uses Arc for O(1) cloning - essential for recursive data structures
158    Variant(Arc<VariantData>),
159
160    /// Map (key-value dictionary with O(1) lookup)
161    /// Keys must be hashable types (Int, String, Bool)
162    Map(Box<HashMap<MapKey, Value>>),
163
164    /// Quotation (stateless function with two entry points for calling convention compatibility)
165    /// - wrapper: C-convention entry point for calls from the runtime
166    /// - impl_: tailcc entry point for tail calls from compiled code (enables TCO)
167    Quotation {
168        /// C-convention wrapper function pointer (for runtime calls via patch_seq_call)
169        wrapper: usize,
170        /// tailcc implementation function pointer (for musttail from compiled code)
171        impl_: usize,
172    },
173
174    /// Closure (quotation with captured environment)
175    /// Contains function pointer and Arc-shared array of captured values.
176    /// Arc enables TCO: no cleanup needed after tail call, ref-count handles it.
177    Closure {
178        /// Function pointer (transmuted to function taking Stack + environment)
179        fn_ptr: usize,
180        /// Captured values from creation site (Arc for TCO support)
181        /// Ordered top-down: `env[0]` is top of stack at creation
182        env: Arc<[Value]>,
183    },
184
185    /// Channel (MPMC sender/receiver pair for CSP-style concurrency)
186    /// Uses Arc for O(1) cloning - duplicating a channel shares the underlying handles.
187    /// Send/receive operations use the handles directly with zero mutex overhead.
188    Channel(Arc<ChannelData>),
189
190    /// Weave context (generator/coroutine communication channels)
191    /// Contains both yield and resume channels for bidirectional communication.
192    /// Travels on the stack - no global registry needed.
193    /// Uses WeaveChannelData with WeaveMessage for type-safe control flow.
194    WeaveCtx {
195        yield_chan: Arc<WeaveChannelData>,
196        resume_chan: Arc<WeaveChannelData>,
197    },
198}
199
200// Safety: Value can be sent and shared between strands (green threads)
201//
202// Send (safe to transfer ownership between threads):
203// - Int, Float, Bool are Copy types (trivially Send)
204// - String (SeqString) implements Send (clone to global on transfer)
205// - Variant contains Arc<VariantData> which is Send when VariantData is Send+Sync
206// - Quotation stores function pointer as usize (Send-safe, no owned data)
207// - Closure: fn_ptr is usize (Send), env is Arc<[Value]> (Send when Value is Send+Sync)
208// - Map contains Box<HashMap> which is Send because keys and values are Send
209// - Channel contains Arc<ChannelData> which is Send (May's Sender/Receiver are Send)
210//
211// Sync (safe to share references between threads):
212// - Value has no interior mutability (no Cell, RefCell, Mutex, etc.)
213// - All operations on Value are read-only or create new values (functional semantics)
214// - Arc requires T: Send + Sync for full thread-safety
215//
216// This is required for:
217// - Channel communication between strands
218// - Arc-based sharing of Variants, Closure environments, and Channels
219unsafe impl Send for Value {}
220unsafe impl Sync for Value {}
221
222/// VariantData: Composite values (sum types)
223///
224/// Fields are stored in a heap-allocated array, NOT linked via next pointers.
225/// This is the key difference from cem2, which used StackCell.next for field linking.
226///
227/// # Arc and Reference Cycles
228///
229/// Variants use `Arc<VariantData>` for O(1) cloning, which could theoretically
230/// create reference cycles. However, cycles are prevented by design:
231/// - VariantData.fields is immutable (no mutation after creation)
232/// - All variant operations create new variants rather than modifying existing ones
233/// - The Seq language has no mutation primitives for variant fields
234///
235/// This functional/immutable design ensures Arc reference counts always reach zero.
236#[derive(Debug, Clone, PartialEq)]
237pub struct VariantData {
238    /// Tag identifies which variant constructor was used (symbol name)
239    /// Stored as SeqString for dynamic variant construction via `wrap-N`
240    pub tag: SeqString,
241
242    /// Fields stored as a Vec for COW (copy-on-write) optimization.
243    /// When Arc refcount == 1, list.push can append in place (amortized O(1)).
244    /// When shared, a clone is made before mutation.
245    pub fields: Vec<Value>,
246}
247
248impl VariantData {
249    /// Create a new variant with the given tag and fields
250    pub fn new(tag: SeqString, fields: Vec<Value>) -> Self {
251        Self { tag, fields }
252    }
253}
254
255impl std::fmt::Display for Value {
256    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
257        match self {
258            Value::Int(n) => write!(f, "{}", n),
259            Value::Float(n) => write!(f, "{}", n),
260            Value::Bool(b) => write!(f, "{}", b),
261            Value::String(s) => write!(f, "{:?}", s.as_str()),
262            Value::Symbol(s) => write!(f, ":{}", s.as_str()),
263            Value::Variant(v) => {
264                write!(f, ":{}", v.tag.as_str())?;
265                if !v.fields.is_empty() {
266                    write!(f, "(")?;
267                }
268                for (i, field) in v.fields.iter().enumerate() {
269                    if i > 0 {
270                        write!(f, ", ")?;
271                    }
272                    write!(f, "{}", field)?;
273                }
274                if !v.fields.is_empty() {
275                    write!(f, ")")?;
276                }
277                Ok(())
278            }
279            Value::Map(m) => {
280                write!(f, "{{")?;
281                for (i, (k, v)) in m.iter().enumerate() {
282                    if i > 0 {
283                        write!(f, ", ")?;
284                    }
285                    write!(f, "{}: {}", k.to_value(), v)?;
286                }
287                write!(f, "}}")
288            }
289            Value::Quotation { .. } => write!(f, "<quotation>"),
290            Value::Closure { .. } => write!(f, "<closure>"),
291            Value::Channel(_) => write!(f, "<channel>"),
292            Value::WeaveCtx { .. } => write!(f, "<weave-ctx>"),
293        }
294    }
295}
296
297#[cfg(test)]
298mod tests {
299    use super::*;
300    use std::mem::{align_of, size_of};
301
302    #[test]
303    fn test_value_layout() {
304        println!("size_of::<Value>() = {}", size_of::<Value>());
305        println!("align_of::<Value>() = {}", align_of::<Value>());
306
307        // Value (Rust enum) is always 40 bytes with #[repr(C)]
308        assert_eq!(
309            size_of::<Value>(),
310            40,
311            "Value must be exactly 40 bytes, got {}",
312            size_of::<Value>()
313        );
314
315        // StackValue is 8 bytes (tagged pointer / u64)
316        use crate::tagged_stack::StackValue;
317        assert_eq!(
318            size_of::<StackValue>(),
319            8,
320            "StackValue must be 8 bytes, got {}",
321            size_of::<StackValue>()
322        );
323
324        assert_eq!(align_of::<Value>(), 8);
325    }
326
327    #[test]
328    fn test_value_int_layout() {
329        let val = Value::Int(42);
330        let ptr = &val as *const Value as *const u8;
331
332        unsafe {
333            // With #[repr(C)], the discriminant is at offset 0
334            // For 9 variants, discriminant fits in 1 byte but is padded
335            let discriminant_byte = *ptr;
336            assert_eq!(
337                discriminant_byte, 0,
338                "Int discriminant should be 0, got {}",
339                discriminant_byte
340            );
341
342            // The i64 value should be at a fixed offset after the discriminant
343            // With C repr, it's typically at offset 8 (discriminant + padding)
344            let value_ptr = ptr.add(8) as *const i64;
345            let stored_value = *value_ptr;
346            assert_eq!(
347                stored_value, 42,
348                "Int value should be 42 at offset 8, got {}",
349                stored_value
350            );
351        }
352    }
353
354    #[test]
355    fn test_value_bool_layout() {
356        let val_true = Value::Bool(true);
357        let val_false = Value::Bool(false);
358        let ptr_true = &val_true as *const Value as *const u8;
359        let ptr_false = &val_false as *const Value as *const u8;
360
361        unsafe {
362            // Bool is variant index 2 (after Int=0, Float=1)
363            let discriminant = *ptr_true;
364            assert_eq!(
365                discriminant, 2,
366                "Bool discriminant should be 2, got {}",
367                discriminant
368            );
369
370            // The bool value should be at offset 8
371            let value_ptr_true = ptr_true.add(8);
372            let value_ptr_false = ptr_false.add(8);
373            assert_eq!(*value_ptr_true, 1, "true should be 1");
374            assert_eq!(*value_ptr_false, 0, "false should be 0");
375        }
376    }
377
378    #[test]
379    fn test_value_display() {
380        // Test Display impl formats values correctly
381        assert_eq!(format!("{}", Value::Int(42)), "42");
382        assert_eq!(format!("{}", Value::Float(2.5)), "2.5");
383        assert_eq!(format!("{}", Value::Bool(true)), "true");
384        assert_eq!(format!("{}", Value::Bool(false)), "false");
385
386        // String shows with quotes (Debug-style)
387        let s = Value::String(SeqString::from("hello"));
388        assert_eq!(format!("{}", s), "\"hello\"");
389
390        // Symbol shows with : prefix
391        let sym = Value::Symbol(SeqString::from("my-symbol"));
392        assert_eq!(format!("{}", sym), ":my-symbol");
393    }
394}