1
use crate::ast::{Expr, PairElement, WasmType};
2
use crate::compiler::context::CompileContext;
3
use crate::compiler::emit::FunctionEmitter;
4
use crate::compiler::expr::{compile_for_stack, eval_value};
5
use crate::error::Result;
6
use crate::reader::Reader;
7
use crate::runtime::SymbolTable;
8

            
9
use super::spill::lambda_runs_at_runtime;
10

            
11
5
fn form(source: &str) -> Expr {
12
5
    Reader::parse(source)
13
5
        .expect("source parses")
14
5
        .exprs
15
5
        .into_iter()
16
5
        .next()
17
5
        .expect("one form")
18
5
}
19

            
20
3
fn stack_type(source: &str) -> Result<WasmType> {
21
3
    let mut ctx = CompileContext::new_with_host_fns(&[])?;
22
3
    let mut emit = FunctionEmitter::new();
23
3
    let mut symbols = SymbolTable::with_builtins_for_wasm();
24
3
    compile_for_stack(&mut ctx, &mut emit, &mut symbols, &form(source))
25
3
}
26

            
27
#[test]
28
1
fn a_lambda_literal_is_found_among_resolved_members() {
29
1
    let lambda = eval_value(
30
1
        &mut SymbolTable::with_builtins_for_wasm(),
31
1
        &form("(lambda (x) x)"),
32
1
    );
33
1
    let symbols = SymbolTable::with_builtins_for_wasm();
34
1
    assert!(lambda_runs_at_runtime(
35
1
        &symbols,
36
1
        &[Expr::Nil, lambda.expect("lambda")]
37
1
    ));
38
1
    assert!(!lambda_runs_at_runtime(&symbols, &[Expr::Nil, Expr::T]));
39
1
}
40

            
41
#[test]
42
1
fn a_list_with_a_lambda_literal_is_untyped_on_the_eval_surface() {
43
1
    let value = eval_value(
44
1
        &mut SymbolTable::with_builtins_for_wasm(),
45
1
        &form("(list 1 (lambda (x) x))"),
46
1
    );
47
1
    assert_eq!(
48
1
        value.expect("evaluates"),
49
1
        Expr::WasmRuntime(WasmType::AnyRef)
50
    );
51
1
}
52

            
53
#[test]
54
1
fn a_list_of_one_lambda_rides_its_own_closure_cell() {
55
1
    assert!(matches!(
56
1
        stack_type("(list (lambda (x) x))"),
57
        Ok(WasmType::PairRef(PairElement::Closure(_)))
58
    ));
59
1
}
60

            
61
#[test]
62
1
fn a_lambda_beside_another_kind_widens_the_cell() {
63
1
    assert_eq!(
64
1
        stack_type("(list 1 (lambda (x) x) \"s\")").expect("compiles"),
65
1
        WasmType::PairRef(PairElement::AnyRef)
66
    );
67
1
}
68

            
69
#[test]
70
1
fn lambdas_of_different_signatures_widen_the_cell() {
71
1
    assert_eq!(
72
1
        stack_type("(list (lambda (x) x) (lambda (x y) x))").expect("compiles"),
73
1
        WasmType::PairRef(PairElement::AnyRef)
74
    );
75
1
}