1
use super::*;
2
use crate::ast::Number;
3
use crate::reader::Reader;
4

            
5
#[test]
6
1
fn call_place_names_a_local_closure() {
7
1
    let head = Expr::WasmLocal(4, WasmType::Closure(ClosureSigId(2)));
8
1
    assert_eq!(call_place(&head), Some((Place::Local(4), ClosureSigId(2))));
9
1
}
10

            
11
#[test]
12
1
fn call_place_names_a_global_closure() {
13
1
    let head = Expr::WasmGlobal(1, WasmType::Closure(ClosureSigId(0)));
14
1
    assert_eq!(call_place(&head), Some((Place::Global(1), ClosureSigId(0))));
15
1
}
16

            
17
#[test]
18
1
fn call_place_refuses_a_place_of_another_type() {
19
1
    assert_eq!(call_place(&Expr::WasmLocal(4, WasmType::I32)), None);
20
1
    assert_eq!(call_place(&Expr::Number(Number::Index(1.into()))), None);
21
1
}
22

            
23
#[test]
24
1
fn call_target_names_a_closure_or_an_any_place() {
25
1
    let closure = Expr::WasmLocal(4, WasmType::Closure(ClosureSigId(2)));
26
1
    assert!(matches!(
27
1
        call_target(&closure),
28
        Some(CallTarget::Closure(Place::Local(4), ClosureSigId(2)))
29
    ));
30
1
    let any = Expr::WasmGlobal(3, WasmType::AnyRef);
31
1
    assert!(matches!(
32
1
        call_target(&any),
33
        Some(CallTarget::Any(Place::Global(3)))
34
    ));
35
1
    assert!(call_target(&Expr::WasmLocal(4, WasmType::I32)).is_none());
36
1
}
37

            
38
#[test]
39
1
fn call_place_leaves_an_any_place_to_the_run_time_check() {
40
1
    assert_eq!(call_place(&Expr::WasmLocal(4, WasmType::AnyRef)), None);
41
1
}
42

            
43
2
fn lambda_value() -> Expr {
44
2
    Expr::Lambda(LambdaParams::simple(Vec::new()), Box::new(Expr::Nil))
45
2
}
46

            
47
5
fn depth_inside(head: &Expr) -> usize {
48
5
    let mut symbols = SymbolTable::with_builtins_for_wasm();
49
5
    applying_lambda(&mut symbols, head, |inner| Ok(levels_charged(inner))).expect("below ceiling")
50
5
}
51

            
52
5
fn levels_charged(symbols: &SymbolTable) -> usize {
53
5
    let mut probe = symbols.clone();
54
5
    let mut left = 0;
55
122
    while probe.enter_shared_recursion().is_ok() {
56
117
        left += 1;
57
    }
58
5
    crate::runtime::MAX_SHARED_RECURSION_DEPTH - left
59
5
}
60

            
61
#[test]
62
1
fn applying_a_lambda_value_is_charged_one_level() {
63
1
    assert_eq!(depth_inside(&lambda_value()), 1);
64
1
}
65

            
66
#[test]
67
1
fn applying_a_lambda_a_call_returns_is_charged_one_level() {
68
1
    let call = Reader::parse_expr("(mk 1)").expect("form parses");
69
1
    assert_eq!(depth_inside(&call), 1);
70
1
}
71

            
72
#[test]
73
1
fn applying_a_lambda_literal_is_not_charged() {
74
1
    let literal = Reader::parse_expr("(lambda (x) x)").expect("form parses");
75
1
    assert_eq!(depth_inside(&literal), 0);
76
1
}
77

            
78
#[test]
79
1
fn applying_a_function_wrapped_lambda_literal_is_not_charged() {
80
1
    let literal = Reader::parse_expr("(function (lambda (x) x))").expect("form parses");
81
1
    assert_eq!(depth_inside(&literal), 0);
82
1
}
83

            
84
#[test]
85
1
fn applying_a_function_named_by_designator_is_charged_one_level() {
86
1
    let named = Reader::parse_expr("(function mk)").expect("form parses");
87
1
    assert_eq!(depth_inside(&named), 1);
88
1
}
89

            
90
#[test]
91
1
fn applying_a_lambda_value_is_refused_at_the_ceiling() {
92
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
93
1
    for _ in 0..crate::runtime::MAX_SHARED_RECURSION_DEPTH {
94
24
        symbols.enter_shared_recursion().expect("below the ceiling");
95
    }
96
1
    let err = applying_lambda(&mut symbols, &lambda_value(), |_| Ok(())).expect_err("ceiling");
97
1
    assert!(matches!(err, Error::Resource(_)), "{err}");
98
1
}
99

            
100
#[test]
101
1
fn a_mismatch_shows_the_compile_prefix_once() {
102
1
    let err = mismatch(
103
1
        WasmType::I32,
104
1
        Error::undefined_symbol(&SymbolTable::new().hidden_name("MK", "A")),
105
1
        "",
106
1
    );
107
1
    assert_eq!(
108
1
        err.to_string(),
109
1
        "Compile error: closure argument type mismatch: expected I32: MK's lambda reads its parameter A here, where a closure factory cannot be used"
110
    );
111
1
}
112

            
113
#[test]
114
1
fn a_sourceless_mismatch_asks_for_a_pin() {
115
1
    let arg = Expr::WasmRuntime(WasmType::I32);
116
1
    let hint = pin_hint(
117
1
        &SymbolTable::new(),
118
1
        (Place::Local(0), 0),
119
1
        (&arg, WasmType::Ratio),
120
1
    );
121
1
    assert_eq!(hint, "; pin the parameter with THE unless its body does");
122
1
    let err = mismatch(WasmType::Ratio, Error::Compile("boom".to_string()), &hint);
123
1
    assert_eq!(
124
1
        err.to_string(),
125
1
        "Compile error: closure argument type mismatch: expected Ratio: boom; pin the parameter with THE unless its body does"
126
    );
127
1
}
128

            
129
#[test]
130
1
fn a_mismatch_of_an_unknown_or_equal_argument_type_asks_for_nothing() {
131
1
    let symbols = SymbolTable::new();
132
1
    for arg in [Expr::WasmRuntime(WasmType::Ratio), Expr::symbol("UNBOUND")] {
133
2
        let hint = pin_hint(&symbols, (Place::Local(0), 0), (&arg, WasmType::Ratio));
134
2
        assert_eq!(hint, "", "{arg:?}");
135
    }
136
1
}
137

            
138
#[test]
139
1
fn a_sourced_mismatch_names_the_unpinned_parameter() {
140
1
    let mut symbols = SymbolTable::with_builtins();
141
1
    let params = LambdaParams::simple(vec!["X".to_string(), "Y".to_string()]);
142
1
    let body = Expr::List(vec![
143
1
        Expr::symbol("+"),
144
1
        Expr::symbol("Y"),
145
1
        Expr::symbol("X"),
146
1
    ]);
147
1
    symbols.record_closure_body(Place::Local(0), params, body);
148
1
    let arg = Expr::WasmRuntime(WasmType::I32);
149
2
    let hint = |index| pin_hint(&symbols, (Place::Local(0), index), (&arg, WasmType::Ratio));
150
1
    assert_eq!(
151
1
        hint(0),
152
1
        "; cannot infer the type of parameter X; pin it with THE"
153
    );
154
1
    assert_eq!(hint(2), "");
155
1
}
156

            
157
#[test]
158
1
fn a_mismatch_names_no_parameter_a_pin_or_no_dimension_decides() {
159
1
    let mut symbols = SymbolTable::with_builtins();
160
1
    let params = LambdaParams::simple(vec!["M".to_string(), "S".to_string()]);
161
1
    let body = Reader::parse_expr("(+ m (the money s))").expect("form parses");
162
1
    symbols.record_closure_body(Place::Local(0), params, body);
163
1
    let arg = Expr::WasmRuntime(WasmType::I32);
164
1
    for index in [0, 1] {
165
2
        let hint = pin_hint(&symbols, (Place::Local(0), index), (&arg, WasmType::Ratio));
166
2
        assert_eq!(hint, "", "{index}");
167
    }
168
1
}
169

            
170
#[test]
171
1
fn a_mismatch_hints_nothing_for_a_parameter_no_dimension_fits() {
172
1
    let mut symbols = SymbolTable::with_builtins();
173
1
    let params = LambdaParams::simple(vec!["X".to_string()]);
174
    let body =
175
1
        Reader::parse_expr("(list (index->scalar x) (scalar->index x))").expect("form parses");
176
1
    symbols.record_closure_body(Place::Local(0), params, body);
177
1
    let arg = Expr::WasmRuntime(WasmType::I32);
178
1
    let hint = pin_hint(&symbols, (Place::Local(0), 0), (&arg, WasmType::Ratio));
179
1
    assert_eq!(hint, "");
180
1
}
181

            
182
5
fn closure_arg(source: &str, expected: WasmType) -> Result<()> {
183
5
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
184
5
    let mut emit = FunctionEmitter::new();
185
5
    let mut symbols = SymbolTable::with_builtins_for_wasm();
186
5
    let arg = Reader::parse_expr(source).expect("parses");
187
5
    compile_closure_arg(&mut ctx, &mut emit, &mut symbols, &arg, expected)
188
5
}
189

            
190
#[test]
191
1
fn an_integer_argument_is_an_index_for_a_closure_parameter() {
192
1
    assert!(closure_arg("1", WasmType::I32).is_ok());
193
1
    assert!(closure_arg("1/2", WasmType::Ratio).is_ok());
194
1
    for source in ["1", "(progn 2)"] {
195
2
        assert!(matches!(
196
2
            closure_arg(source, WasmType::Ratio),
197
            Err(Error::Dimension(_))
198
        ));
199
    }
200
1
    assert!(closure_arg("1/2", WasmType::I32).is_err());
201
1
}