hiko-vm 0.5.1

hiko-vm: part of the hiko ML-family scripting language
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
//! SendableValue: the process boundary type.
//!
//! Only SendableValue may cross process boundaries.
//! It contains no GcRef, no closures, no continuations.

use std::sync::Arc;

use crate::heap::Heap;
use crate::value::{GcRef, HeapObject, Value};
use crate::vm::{TAG_CONS, TAG_NIL};

/// A value that can safely cross process boundaries via message passing.
/// Contains no process-local heap references.
#[derive(Debug, Clone)]
pub enum SendableValue {
    Int(i64),
    Float(f64),
    Bool(bool),
    Char(char),
    Unit,
    String(Arc<str>),
    Bytes(Arc<[u8]>),
    Tuple(Vec<SendableValue>),
    List(Vec<SendableValue>),
    Data {
        tag: u16,
        fields: Vec<SendableValue>,
    },
}

/// Serialize a VM Value into a SendableValue.
/// Returns Err if the value contains non-sendable types
/// (closures, continuations, Rng).
pub fn serialize(value: Value, heap: &Heap) -> Result<SendableValue, String> {
    match value {
        Value::Int(n) => Ok(SendableValue::Int(n)),
        Value::Float(f) => Ok(SendableValue::Float(f)),
        Value::Bool(b) => Ok(SendableValue::Bool(b)),
        Value::Char(c) => Ok(SendableValue::Char(c)),
        Value::Unit => Ok(SendableValue::Unit),
        Value::Builtin(_) => Err("cannot send builtin functions across processes".into()),
        Value::Heap(r) => serialize_heap(r, heap),
    }
}

fn serialize_heap(r: GcRef, heap: &Heap) -> Result<SendableValue, String> {
    match heap.get(r).map_err(|e| e.to_string())? {
        HeapObject::String(s) => Ok(SendableValue::String(Arc::from(s.as_str()))),
        HeapObject::Bytes(b) => Ok(SendableValue::Bytes(Arc::from(b.as_slice()))),
        HeapObject::Tuple(fields) => {
            let mut out = Vec::with_capacity(fields.len());
            for &v in fields.iter() {
                out.push(serialize(v, heap)?);
            }
            Ok(SendableValue::Tuple(out))
        }
        HeapObject::Data { tag, fields } => {
            // Special case: lists → serialize as SendableValue::List
            if *tag == TAG_NIL && fields.is_empty() {
                return Ok(SendableValue::List(Vec::new()));
            }
            if *tag == TAG_CONS && fields.len() == 2 {
                return serialize_list(fields[0], fields[1], heap);
            }
            let mut out = Vec::with_capacity(fields.len());
            for &v in fields.iter() {
                out.push(serialize(v, heap)?);
            }
            Ok(SendableValue::Data {
                tag: *tag,
                fields: out,
            })
        }
        HeapObject::Closure { .. } => Err("cannot send closures across processes".into()),
        HeapObject::Continuation { .. } => Err("cannot send continuations across processes".into()),
        HeapObject::Rng { .. } => Err("cannot send Rng state across processes".into()),
    }
}

/// Serialize a cons-list into a flat Vec for efficient transfer.
fn serialize_list(head: Value, tail: Value, heap: &Heap) -> Result<SendableValue, String> {
    let mut items = vec![serialize(head, heap)?];
    let mut cur = tail;
    loop {
        match cur {
            Value::Heap(r) => match heap.get(r).map_err(|e| e.to_string())? {
                HeapObject::Data { tag, fields } if *tag == TAG_NIL && fields.is_empty() => {
                    break;
                }
                HeapObject::Data { tag, fields } if *tag == TAG_CONS && fields.len() == 2 => {
                    items.push(serialize(fields[0], heap)?);
                    cur = fields[1];
                }
                _ => return Err("malformed list during serialization".into()),
            },
            _ => return Err("malformed list during serialization".into()),
        }
    }
    Ok(SendableValue::List(items))
}

/// Deserialize a SendableValue into a VM Value, allocating into the given heap.
pub fn deserialize(msg: SendableValue, heap: &mut Heap) -> Value {
    use smallvec::smallvec;

    match msg {
        SendableValue::Int(n) => Value::Int(n),
        SendableValue::Float(f) => Value::Float(f),
        SendableValue::Bool(b) => Value::Bool(b),
        SendableValue::Char(c) => Value::Char(c),
        SendableValue::Unit => Value::Unit,
        SendableValue::String(s) => Value::Heap(heap.alloc(HeapObject::String(s.to_string()))),
        SendableValue::Bytes(b) => Value::Heap(heap.alloc(HeapObject::Bytes(b.to_vec()))),
        SendableValue::Tuple(fields) => {
            let values: smallvec::SmallVec<[Value; 2]> =
                fields.into_iter().map(|v| deserialize(v, heap)).collect();
            Value::Heap(heap.alloc(HeapObject::Tuple(values)))
        }
        SendableValue::List(items) => {
            // Build cons-list in reverse
            let mut list = Value::Heap(heap.alloc(HeapObject::Data {
                tag: TAG_NIL,
                fields: smallvec![],
            }));
            for item in items.into_iter().rev() {
                let val = deserialize(item, heap);
                list = Value::Heap(heap.alloc(HeapObject::Data {
                    tag: TAG_CONS,
                    fields: smallvec![val, list],
                }));
            }
            list
        }
        SendableValue::Data { tag, fields } => {
            let values: smallvec::SmallVec<[Value; 2]> =
                fields.into_iter().map(|v| deserialize(v, heap)).collect();
            Value::Heap(heap.alloc(HeapObject::Data {
                tag,
                fields: values,
            }))
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::heap::Heap;
    use crate::value::HeapObject;
    use smallvec::smallvec;
    use std::sync::Arc;

    fn round_trip(value: Value, heap: &Heap) -> Value {
        let sendable = serialize(value, heap).expect("serialize failed");
        let mut new_heap = Heap::new();
        deserialize(sendable, &mut new_heap)
    }

    #[test]
    fn test_int() {
        let heap = Heap::new();
        match round_trip(Value::Int(42), &heap) {
            Value::Int(42) => {}
            other => panic!("expected Int(42), got {:?}", other),
        }
    }

    #[test]
    fn test_float() {
        let heap = Heap::new();
        match round_trip(Value::Float(3.14), &heap) {
            Value::Float(f) => assert!((f - 3.14).abs() < 1e-10),
            other => panic!("expected Float, got {:?}", other),
        }
    }

    #[test]
    fn test_bool() {
        let heap = Heap::new();
        match round_trip(Value::Bool(true), &heap) {
            Value::Bool(true) => {}
            other => panic!("expected Bool(true), got {:?}", other),
        }
    }

    #[test]
    fn test_char() {
        let heap = Heap::new();
        match round_trip(Value::Char('X'), &heap) {
            Value::Char('X') => {}
            other => panic!("expected Char('X'), got {:?}", other),
        }
    }

    #[test]
    fn test_unit() {
        let heap = Heap::new();
        match round_trip(Value::Unit, &heap) {
            Value::Unit => {}
            other => panic!("expected Unit, got {:?}", other),
        }
    }

    #[test]
    fn test_string() {
        let mut heap = Heap::new();
        let s = Value::Heap(heap.alloc(HeapObject::String("hello world".to_string())));
        let sendable = serialize(s, &heap).unwrap();
        let mut new_heap = Heap::new();
        let result = deserialize(sendable, &mut new_heap);
        match result {
            Value::Heap(r) => match new_heap.get(r) {
                Ok(HeapObject::String(s)) => assert_eq!(s, "hello world"),
                other => panic!("expected String, got {:?}", other),
            },
            other => panic!("expected Heap, got {:?}", other),
        }
    }

    #[test]
    fn test_bytes() {
        let mut heap = Heap::new();
        let b = Value::Heap(heap.alloc(HeapObject::Bytes(vec![1, 2, 3])));
        let sendable = serialize(b, &heap).unwrap();
        let mut new_heap = Heap::new();
        let result = deserialize(sendable, &mut new_heap);
        match result {
            Value::Heap(r) => match new_heap.get(r) {
                Ok(HeapObject::Bytes(b)) => assert_eq!(b, &[1, 2, 3]),
                other => panic!("expected Bytes, got {:?}", other),
            },
            other => panic!("expected Heap, got {:?}", other),
        }
    }

    #[test]
    fn test_tuple() {
        let mut heap = Heap::new();
        let t = Value::Heap(heap.alloc(HeapObject::Tuple(smallvec![
            Value::Int(1),
            Value::Bool(true)
        ])));
        let sendable = serialize(t, &heap).unwrap();
        let mut new_heap = Heap::new();
        let result = deserialize(sendable, &mut new_heap);
        match result {
            Value::Heap(r) => match new_heap.get(r) {
                Ok(HeapObject::Tuple(fields)) => {
                    assert_eq!(fields.len(), 2);
                    assert!(matches!(fields[0], Value::Int(1)));
                    assert!(matches!(fields[1], Value::Bool(true)));
                }
                other => panic!("expected Tuple, got {:?}", other),
            },
            other => panic!("expected Heap, got {:?}", other),
        }
    }

    #[test]
    fn test_list() {
        let mut heap = Heap::new();
        // Build [1, 2, 3]
        let nil = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_NIL,
            fields: smallvec![],
        }));
        let c3 = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_CONS,
            fields: smallvec![Value::Int(3), nil],
        }));
        let c2 = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_CONS,
            fields: smallvec![Value::Int(2), c3],
        }));
        let c1 = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_CONS,
            fields: smallvec![Value::Int(1), c2],
        }));

        let sendable = serialize(c1, &heap).unwrap();

        // Verify it serialized as a flat list
        match &sendable {
            SendableValue::List(items) => {
                assert_eq!(items.len(), 3);
            }
            other => panic!("expected List, got {:?}", other),
        }

        // Deserialize into new heap and verify
        let mut new_heap = Heap::new();
        let result = deserialize(sendable, &mut new_heap);
        // Walk the cons-list
        let mut cur = result;
        let mut values = vec![];
        loop {
            match cur {
                Value::Heap(r) => match new_heap.get(r).unwrap() {
                    HeapObject::Data { tag, .. } if *tag == TAG_NIL => break,
                    HeapObject::Data { tag, fields } if *tag == TAG_CONS => {
                        if let Value::Int(n) = fields[0] {
                            values.push(n);
                        }
                        cur = fields[1];
                    }
                    _ => panic!("bad list"),
                },
                _ => panic!("bad list"),
            }
        }
        assert_eq!(values, vec![1, 2, 3]);
    }

    #[test]
    fn test_data() {
        let mut heap = Heap::new();
        // Some(42) — tag 1, one field
        let data = Value::Heap(heap.alloc(HeapObject::Data {
            tag: 1,
            fields: smallvec![Value::Int(42)],
        }));
        let sendable = serialize(data, &heap).unwrap();
        let mut new_heap = Heap::new();
        let result = deserialize(sendable, &mut new_heap);
        match result {
            Value::Heap(r) => match new_heap.get(r) {
                Ok(HeapObject::Data { tag, fields }) => {
                    assert_eq!(*tag, 1);
                    assert_eq!(fields.len(), 1);
                    assert!(matches!(fields[0], Value::Int(42)));
                }
                other => panic!("expected Data, got {:?}", other),
            },
            other => panic!("expected Heap, got {:?}", other),
        }
    }

    #[test]
    fn test_nested_tuple_with_string() {
        let mut heap = Heap::new();
        let s = Value::Heap(heap.alloc(HeapObject::String("hello".to_string())));
        let t = Value::Heap(heap.alloc(HeapObject::Tuple(smallvec![Value::Int(1), s])));
        let sendable = serialize(t, &heap).unwrap();

        // Verify Arc<str> in serialized form
        match &sendable {
            SendableValue::Tuple(fields) => {
                assert!(matches!(&fields[1], SendableValue::String(s) if &**s == "hello"));
            }
            _ => panic!("expected Tuple"),
        }
    }

    #[test]
    fn test_closure_rejected() {
        let mut heap = Heap::new();
        let closure = Value::Heap(heap.alloc(HeapObject::Closure {
            proto_idx: 0,
            captures: Arc::from(vec![].into_boxed_slice()),
        }));
        let result = serialize(closure, &heap);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("closure"));
    }

    #[test]
    fn test_continuation_rejected() {
        let mut heap = Heap::new();
        let cont = Value::Heap(heap.alloc(HeapObject::Continuation {
            saved_frames: vec![],
            saved_stack: vec![],
            saved_handler: None,
        }));
        let result = serialize(cont, &heap);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("continuation"));
    }

    #[test]
    fn test_rng_rejected() {
        let mut heap = Heap::new();
        let rng = Value::Heap(heap.alloc(HeapObject::Rng { state: 0, inc: 1 }));
        let result = serialize(rng, &heap);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("Rng"));
    }

    #[test]
    fn test_builtin_rejected() {
        let heap = Heap::new();
        let result = serialize(Value::Builtin(0), &heap);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("builtin"));
    }

    #[test]
    fn test_empty_list() {
        let mut heap = Heap::new();
        let nil = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_NIL,
            fields: smallvec![],
        }));
        let sendable = serialize(nil, &heap).unwrap();
        match &sendable {
            SendableValue::List(items) => assert!(items.is_empty()),
            other => panic!("expected empty List, got {:?}", other),
        }
    }

    #[test]
    fn test_list_with_strings() {
        let mut heap = Heap::new();
        let s1 = Value::Heap(heap.alloc(HeapObject::String("a".to_string())));
        let s2 = Value::Heap(heap.alloc(HeapObject::String("b".to_string())));
        let nil = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_NIL,
            fields: smallvec![],
        }));
        let c2 = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_CONS,
            fields: smallvec![s2, nil],
        }));
        let c1 = Value::Heap(heap.alloc(HeapObject::Data {
            tag: TAG_CONS,
            fields: smallvec![s1, c2],
        }));

        let sendable = serialize(c1, &heap).unwrap();
        match &sendable {
            SendableValue::List(items) => {
                assert_eq!(items.len(), 2);
                assert!(matches!(&items[0], SendableValue::String(s) if &**s == "a"));
                assert!(matches!(&items[1], SendableValue::String(s) if &**s == "b"));
            }
            other => panic!("expected List, got {:?}", other),
        }
    }

    #[test]
    fn test_tuple_with_closure_rejected() {
        let mut heap = Heap::new();
        let closure = Value::Heap(heap.alloc(HeapObject::Closure {
            proto_idx: 0,
            captures: Arc::from(vec![].into_boxed_slice()),
        }));
        let t = Value::Heap(heap.alloc(HeapObject::Tuple(smallvec![Value::Int(1), closure])));
        let result = serialize(t, &heap);
        assert!(result.is_err());
        assert!(result.unwrap_err().contains("closure"));
    }
}