1
use super::dynamic::{Saved, dynamic_home, reject_missed_specials, restore_forms};
2
use crate::ast::{Expr, Number, WasmType};
3
use crate::runtime::{Symbol, SymbolKind, SymbolTable};
4
use std::collections::HashSet;
5

            
6
4
fn table_with_global() -> SymbolTable {
7
4
    let mut symbols = SymbolTable::with_builtins_for_wasm();
8
4
    symbols.define_special(
9
4
        Symbol::new("X", SymbolKind::Variable).with_value(Expr::WasmGlobal(4, WasmType::Ratio)),
10
4
    );
11
4
    symbols
12
4
}
13

            
14
7
fn home_of(symbols: &SymbolTable, name: &str) -> Option<(u32, WasmType)> {
15
7
    dynamic_home(symbols, name).map(|home| (home.global, home.ty))
16
7
}
17

            
18
#[test]
19
1
fn a_global_valued_special_has_a_dynamic_home() {
20
1
    let symbols = table_with_global();
21
1
    assert_eq!(home_of(&symbols, "X"), Some((4, WasmType::Ratio)));
22
1
    assert_eq!(home_of(&symbols, "Y"), None);
23
1
}
24

            
25
#[test]
26
1
fn a_global_that_is_not_special_yet_binds_lexically() {
27
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
28
1
    symbols.define(
29
1
        Symbol::new("X", SymbolKind::Variable).with_value(Expr::WasmGlobal(4, WasmType::Ratio)),
30
1
    );
31
1
    assert_eq!(home_of(&symbols, "X"), None);
32
1
    symbols.mark_special("X");
33
1
    assert_eq!(home_of(&symbols, "X"), Some((4, WasmType::Ratio)));
34
1
}
35

            
36
#[test]
37
1
fn a_macro_body_binds_lexically() {
38
1
    let mut symbols = table_with_global();
39
1
    symbols.mark_macro_expansion();
40
1
    assert_eq!(home_of(&symbols, "X"), None);
41
1
}
42

            
43
#[test]
44
1
fn a_binder_of_a_unit_special_the_scan_did_not_see_is_refused() {
45
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
46
1
    symbols.set_unit_specials(HashSet::from(["X".to_string()]), HashSet::new());
47
1
    let refused = reject_missed_specials(&symbols, ["X"]).expect_err("scan miss");
48
1
    assert!(
49
1
        refused
50
1
            .to_string()
51
1
            .contains("a macro expansion rebinds the special X")
52
    );
53
1
    assert!(reject_missed_specials(&symbols, ["Y"]).is_ok());
54
1
    symbols.set_unit_specials(
55
1
        HashSet::from(["X".to_string()]),
56
1
        HashSet::from(["X".to_string()]),
57
1
    );
58
1
    assert!(reject_missed_specials(&symbols, ["X"]).is_ok());
59
1
}
60

            
61
#[test]
62
1
fn restores_run_last_bound_first() {
63
1
    let saved = [
64
1
        Saved {
65
1
            global: 1,
66
1
            ty: WasmType::I32,
67
1
            local: 10,
68
1
            flag: None,
69
1
            depth_local: 13,
70
1
            depth: 20,
71
1
        },
72
1
        Saved {
73
1
            global: 2,
74
1
            ty: WasmType::Ratio,
75
1
            local: 11,
76
1
            flag: Some((3, 12)),
77
1
            depth_local: 14,
78
1
            depth: 30,
79
1
        },
80
1
    ];
81
1
    let forms = restore_forms(&saved);
82
1
    assert_eq!(
83
1
        forms,
84
1
        vec![
85
1
            Expr::List(vec![
86
1
                Expr::symbol("SETF"),
87
1
                Expr::WasmGlobal(2, WasmType::Ratio),
88
1
                Expr::WasmLocal(11, WasmType::Ratio),
89
1
            ]),
90
1
            Expr::List(vec![
91
1
                Expr::symbol("SETF"),
92
1
                Expr::WasmGlobal(3, WasmType::I32),
93
1
                Expr::WasmLocal(12, WasmType::I32),
94
1
            ]),
95
1
            Expr::List(vec![
96
1
                Expr::symbol("SETF"),
97
1
                Expr::WasmGlobal(30, WasmType::I32),
98
1
                Expr::WasmLocal(14, WasmType::I32),
99
1
            ]),
100
1
            Expr::List(vec![
101
1
                Expr::symbol("SETF"),
102
1
                Expr::WasmGlobal(1, WasmType::I32),
103
1
                Expr::WasmLocal(10, WasmType::I32),
104
1
            ]),
105
1
            Expr::List(vec![
106
1
                Expr::symbol("SETF"),
107
1
                Expr::WasmGlobal(20, WasmType::I32),
108
1
                Expr::WasmLocal(13, WasmType::I32),
109
1
            ]),
110
        ]
111
    );
112
1
}
113

            
114
2
fn table_with_constant() -> SymbolTable {
115
2
    let mut symbols = SymbolTable::with_builtins_for_wasm();
116
2
    symbols.define_special(
117
2
        Symbol::new("C", SymbolKind::Variable).with_value(Expr::Number(Number::Index(7.into()))),
118
2
    );
119
2
    symbols
120
2
}
121

            
122
#[test]
123
1
fn a_special_of_an_earlier_unit_holds_a_constant_to_bind() {
124
1
    let symbols = table_with_constant();
125
1
    assert_eq!(
126
1
        symbols.constant_global("C"),
127
1
        Some(Expr::Number(Number::Index(7.into())))
128
    );
129
1
    assert_eq!(table_with_global().constant_global("X"), None);
130
1
    assert_eq!(symbols.constant_global("Y"), None);
131
1
}
132

            
133
#[test]
134
1
fn a_macro_body_binds_a_constant_global_lexically() {
135
1
    let mut symbols = table_with_constant();
136
1
    symbols.mark_macro_expansion();
137
1
    assert_eq!(symbols.constant_global("C"), None);
138
1
}
139

            
140
#[test]
141
1
fn a_created_global_has_no_dynamic_home_until_declared_special() {
142
1
    let mut symbols = table_with_global();
143
1
    symbols.mark_created_global("X");
144
1
    assert_eq!(home_of(&symbols, "X"), None);
145
1
    symbols.mark_special("X");
146
1
    assert_eq!(home_of(&symbols, "X"), Some((4, WasmType::Ratio)));
147
1
}