1
//! The session instance heap: what survives a unit's settle.
2

            
3
use crate::ast::{Expr, Number, StructId};
4
use crate::runtime::{Found, StructInstance, Symbol, SymbolKind, SymbolTable};
5

            
6
7
fn number(n: i64) -> Expr {
7
7
    Expr::Number(Number::Index(n))
8
7
}
9

            
10
14
fn instance(fields: Vec<Expr>) -> Option<StructInstance> {
11
14
    Some(StructInstance {
12
14
        layout: StructId(0),
13
14
        fields,
14
14
    })
15
14
}
16

            
17
17
fn leaf(serial: u32) -> Expr {
18
17
    Expr::Instance(serial, StructId(0))
19
17
}
20

            
21
8
fn holding(globals: &[(&str, Expr)]) -> SymbolTable {
22
8
    let mut symbols = SymbolTable::new();
23
8
    for (name, value) in globals {
24
10
        symbols.define(Symbol::new(*name, SymbolKind::Variable).with_value(value.clone()));
25
    }
26
8
    symbols
27
8
}
28

            
29
2
fn held(symbols: &SymbolTable, name: &str) -> Option<Expr> {
30
2
    symbols.lookup(name).and_then(Symbol::value).cloned()
31
2
}
32

            
33
5
fn serials(symbols: &SymbolTable) -> Vec<u32> {
34
5
    symbols.instances().map(|(serial, _)| serial).collect()
35
5
}
36

            
37
#[test]
38
1
fn an_instance_a_global_reaches_is_kept_with_its_fields() {
39
1
    let mut symbols = holding(&[("P", leaf(0))]);
40
1
    let found = Found::from([(0, instance(vec![number(3)]))]);
41
1
    symbols.settle_instances(found, 1);
42
1
    let kept: Vec<_> = symbols
43
1
        .instances()
44
1
        .map(|(_, found)| found.clone())
45
        .collect();
46
1
    assert_eq!(kept, [instance(vec![number(3)]).unwrap()]);
47
1
    assert_eq!(held(&symbols, "P"), Some(leaf(0)));
48
1
    assert_eq!(symbols.next_serial(), 1);
49
1
}
50

            
51
#[test]
52
1
fn an_instance_no_global_or_instance_reaches_is_swept() {
53
1
    let mut symbols = holding(&[("P", leaf(0))]);
54
1
    let found = Found::from([
55
1
        (0, instance(vec![leaf(1)])),
56
1
        (1, instance(vec![number(1)])),
57
1
        (2, instance(vec![number(2)])),
58
1
    ]);
59
1
    symbols.settle_instances(found, 3);
60
1
    assert_eq!(serials(&symbols), [0, 1]);
61
1
}
62

            
63
#[test]
64
1
fn a_cycle_stays_while_a_global_reaches_it() {
65
1
    let mut symbols = holding(&[("P", leaf(0))]);
66
1
    let found = Found::from([(0, instance(vec![leaf(1)])), (1, instance(vec![leaf(0)]))]);
67
1
    symbols.settle_instances(found, 2);
68
1
    assert_eq!(serials(&symbols), [0, 1]);
69
1
    symbols.carry("P", crate::runtime::Carried::Constant(Expr::Nil));
70
1
    symbols.settle_instances(
71
1
        Found::from([(0, instance(vec![leaf(1)])), (1, instance(vec![leaf(0)]))]),
72
1
        2,
73
1
    );
74
1
    assert_eq!(serials(&symbols), Vec::<u32>::new());
75
1
}
76

            
77
#[test]
78
1
fn an_opaque_instance_is_dropped_with_every_global_reaching_it() {
79
1
    let list = Expr::quoted(Expr::List(vec![number(1), leaf(0)]));
80
1
    let mut symbols = holding(&[("P", leaf(0)), ("L", list)]);
81
1
    symbols.settle_instances(Found::from([(0, None)]), 1);
82
1
    assert_eq!(serials(&symbols), Vec::<u32>::new());
83
1
    assert_eq!(symbols.lost_type("P"), Some("struct"));
84
1
    assert_eq!(symbols.lost_type("L"), Some("struct"));
85
1
    assert_eq!(held(&symbols, "P"), None);
86
1
}
87

            
88
#[test]
89
1
fn an_instance_reaching_an_opaque_one_is_dropped_and_its_closure_holders_too() {
90
1
    let closure = Expr::Lambda(
91
1
        crate::ast::LambdaParams::simple(Vec::new()),
92
1
        Box::new(Expr::List(vec![Expr::symbol("LET"), leaf(1)])),
93
1
    );
94
1
    let mut symbols = holding(&[("A", leaf(0)), ("F", closure)]);
95
1
    let found = Found::from([(0, instance(vec![leaf(1)])), (1, None)]);
96
1
    symbols.settle_instances(found, 2);
97
1
    assert_eq!(serials(&symbols), Vec::<u32>::new());
98
1
    assert_eq!(symbols.lost_type("A"), Some("struct"));
99
1
    assert_eq!(symbols.lost_type("F"), Some("closure"));
100
1
}
101

            
102
#[test]
103
1
fn an_instance_a_unit_never_read_makes_its_holder_lost() {
104
1
    let mut symbols = holding(&[("P", leaf(4))]);
105
1
    symbols.settle_instances(Found::new(), 0);
106
1
    assert_eq!(symbols.lost_type("P"), Some("struct"));
107
1
}
108

            
109
#[test]
110
1
fn the_serial_counter_never_goes_back() {
111
1
    let mut symbols = holding(&[("P", leaf(0))]);
112
1
    symbols.settle_instances(Found::from([(0, instance(vec![]))]), 5);
113
1
    symbols.settle_instances(Found::from([(0, instance(vec![]))]), 2);
114
1
    assert_eq!(symbols.next_serial(), 5);
115
1
}
116

            
117
#[test]
118
1
fn a_restored_journal_restores_the_instances_and_the_counter() {
119
1
    let mut symbols = holding(&[("P", leaf(0))]);
120
1
    symbols.settle_instances(Found::from([(0, instance(vec![number(1)]))]), 1);
121
1
    let digest = symbols.state_digest();
122
1
    let journal = symbols.journal();
123
1
    symbols.settle_instances(Found::from([(0, instance(vec![number(9)]))]), 7);
124
1
    assert_ne!(symbols.state_digest(), digest);
125
1
    symbols.restore_journal(journal);
126
1
    assert_eq!(symbols.state_digest(), digest);
127
1
}