1
//! The source a closure keeps for a host that carries it to the next unit.
2

            
3
use crate::ast::WasmType;
4
use crate::compiler::expr::format_form;
5
use crate::compiler::{CompileMode, Compiler};
6
use crate::host_fn::HostFnSpec;
7
use crate::reader::Reader;
8
use crate::runtime::SymbolTable;
9

            
10
use crate::ast::Expr;
11

            
12
8
fn after(source: &str) -> SymbolTable {
13
8
    let specs = [HostFnSpec::new("idx", "test", "idx").returns(WasmType::I32)];
14
8
    let mut symbols = SymbolTable::with_builtins();
15
8
    symbols.register_host_fns(&specs);
16
8
    let program = Reader::parse(source).expect("parses");
17
8
    Compiler::with_host_fns(specs.to_vec())
18
8
        .compile_with_mode(&program, &mut symbols, CompileMode::Eval)
19
8
        .expect("compiles");
20
8
    symbols
21
8
}
22

            
23
6
fn carried_text(symbols: &SymbolTable, site: i32) -> Option<String> {
24
6
    let source = symbols.closure_source(site)?;
25
5
    let values = source.fields().iter().map(|_| Expr::Nil).collect();
26
5
    match source.lambda(values) {
27
5
        Expr::Lambda(params, body) => Some(format!("{:?} {}", params.required, format_form(&body))),
28
        _ => None,
29
    }
30
6
}
31

            
32
#[test]
33
1
fn a_capture_free_lambda_keeps_its_source() {
34
1
    let symbols = after("(lambda (x) (* x 2))");
35
1
    assert_eq!(
36
1
        carried_text(&symbols, 0).as_deref(),
37
1
        Some(r#"["X"] (* X 2)"#)
38
    );
39
1
}
40

            
41
#[test]
42
1
fn each_lambda_of_a_unit_has_its_own_site() {
43
1
    let symbols = after("(list (lambda (x) 1) (lambda (x) 2))");
44
1
    assert_eq!(carried_text(&symbols, 0).as_deref(), Some(r#"["X"] 1"#));
45
1
    assert_eq!(carried_text(&symbols, 1).as_deref(), Some(r#"["X"] 2"#));
46
1
    assert_eq!(carried_text(&symbols, 2), None);
47
1
}
48

            
49
#[test]
50
1
fn a_runtime_capture_is_a_field_of_the_environment() {
51
1
    let symbols = after("(let ((n (idx))) (lambda () n))");
52
1
    let source = symbols.closure_source(0).expect("kept");
53
1
    assert_eq!(source.fields(), [("N".to_string(), WasmType::I32)]);
54
1
}
55

            
56
#[test]
57
1
fn a_lexical_constant_is_bound_around_the_body() {
58
1
    let symbols = after("(let ((r 1/10)) (lambda (x) (* x r)))");
59
1
    assert_eq!(
60
1
        carried_text(&symbols, 0).as_deref(),
61
1
        Some(r#"["X"] (LET ((R 1/10)) (* X R))"#)
62
    );
63
1
}
64

            
65
#[test]
66
1
fn a_shared_cell_in_the_environment_keeps_no_source() {
67
1
    let symbols = after("(let ((n 0)) (lambda () (setf n (+ n 1))))");
68
1
    assert_eq!(symbols.closure_source(0), None);
69
1
}
70

            
71
#[test]
72
1
fn a_labels_function_in_the_body_keeps_no_source() {
73
1
    let symbols = after("(labels ((h (y) (* y 10))) (lambda (x) (h x)))");
74
1
    assert_eq!(symbols.closure_source(0), None);
75
1
}
76

            
77
#[test]
78
1
fn a_user_macro_in_the_body_keeps_its_expansion() {
79
1
    let symbols = after("(defmacro dbl (x) `(* ,x 2)) (lambda (y) (dbl y))");
80
1
    assert_eq!(
81
1
        carried_text(&symbols, 0).as_deref(),
82
1
        Some(r#"["Y"] (* Y 2)"#)
83
    );
84
1
}
85

            
86
#[test]
87
1
fn a_built_in_macro_in_the_body_keeps_its_source() {
88
1
    let symbols = after("(lambda (x) (when (> x 0) 1))");
89
1
    assert!(symbols.closure_source(0).is_some());
90
1
}