1
use super::*;
2
use crate::ast::{ClosureSigId, Number};
3
use crate::compiler::expr::classify_stack_type;
4
use crate::compiler::{CompileMode, Compiler};
5
use crate::reader::Reader;
6

            
7
use super::super::fold::probe_value;
8

            
9
#[test]
10
1
fn an_argument_of_the_parameter_type_passes_as_it_is() {
11
1
    assert_eq!(fit(WasmType::Ratio, WasmType::Ratio), Some(Fit::Pass));
12
1
    assert_eq!(fit(WasmType::I32, WasmType::I32), Some(Fit::Pass));
13
1
}
14

            
15
#[test]
16
1
fn an_argument_boxes_into_an_any_parameter() {
17
1
    assert_eq!(
18
1
        fit(WasmType::I32, WasmType::AnyRef),
19
1
        Some(Fit::Box(WasmType::I32))
20
    );
21
1
    assert_eq!(
22
1
        fit(WasmType::I32, WasmType::AnyRef),
23
1
        Some(Fit::Box(WasmType::I32))
24
    );
25
1
}
26

            
27
#[test]
28
1
fn an_index_argument_fits_neither_a_scalar_nor_a_money_parameter() {
29
1
    assert_eq!(fit(WasmType::I32, WasmType::Ratio), None);
30
1
    assert_eq!(fit(WasmType::I32, WasmType::Commodity), None);
31
1
}
32

            
33
#[test]
34
1
fn an_any_argument_narrows_to_the_types_the_is_narrowing_throws_for() {
35
1
    assert_eq!(
36
1
        fit(WasmType::AnyRef, WasmType::I32),
37
1
        Some(Fit::Narrow(Target::Index))
38
    );
39
1
    assert_eq!(
40
1
        fit(WasmType::AnyRef, WasmType::StringRef),
41
1
        Some(Fit::Narrow(Target::String))
42
    );
43
1
    assert_eq!(
44
1
        fit(WasmType::AnyRef, WasmType::PairRef(PairElement::AnyRef)),
45
1
        Some(Fit::List)
46
    );
47
1
}
48

            
49
#[test]
50
1
fn nothing_else_converts() {
51
1
    let typed_list = WasmType::PairRef(PairElement::I32);
52
1
    assert_eq!(fit(WasmType::I32, WasmType::Ratio), None);
53
1
    assert_eq!(fit(WasmType::AnyRef, typed_list), None);
54
1
    assert_eq!(
55
1
        fit(typed_list, WasmType::PairRef(PairElement::AnyRef)),
56
1
        None
57
    );
58
1
    assert_eq!(
59
1
        fit(
60
1
            WasmType::Closure(ClosureSigId(0)),
61
1
            WasmType::Closure(ClosureSigId(1))
62
1
        ),
63
1
        None
64
    );
65
1
}
66

            
67
#[test]
68
1
fn a_signature_that_cannot_take_the_shape_has_no_arm() {
69
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
70
1
    let sig = ctx
71
1
        .intern_closure_signature(&[WasmType::I32], WasmType::I32)
72
1
        .expect("sig");
73
1
    let entry = ctx.closure_sig(sig).expect("entry").clone();
74
1
    assert_eq!(arm_fits(&[WasmType::I32], &entry), Some(vec![Fit::Pass]));
75
1
    assert_eq!(arm_fits(&[WasmType::Ratio], &entry), None);
76
1
}
77

            
78
2
fn local_any(ctx: &mut CompileContext) -> Place {
79
2
    Place::Local(ctx.alloc_local(WasmType::AnyRef).expect("local"))
80
2
}
81

            
82
#[test]
83
1
fn a_call_pushes_each_argument_at_its_own_type_and_returns_any() {
84
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
85
1
    let callee = local_any(&mut ctx);
86
1
    let mut emit = FunctionEmitter::new();
87
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
88
1
    let args = [
89
1
        Reader::parse_expr("2").expect("parses"),
90
1
        Reader::parse_expr("1/2").expect("parses"),
91
1
        Reader::parse_expr("\"s\"").expect("parses"),
92
1
    ];
93
1
    let ty = compile_any_call(&mut ctx, &mut emit, &mut symbols, callee, &args).expect("compiles");
94
1
    assert_eq!(ty, WasmType::AnyRef);
95
1
    let helpers = ctx.take_any_call_helpers();
96
1
    assert_eq!(helpers.len(), 1);
97
1
    assert_eq!(
98
1
        helpers[0].shape,
99
1
        [WasmType::I32, WasmType::Ratio, WasmType::StringRef]
100
    );
101
1
}
102

            
103
#[test]
104
1
fn an_integer_beyond_the_index_range_is_no_argument() {
105
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
106
1
    let callee = local_any(&mut ctx);
107
1
    let mut emit = FunctionEmitter::new();
108
1
    let mut symbols = SymbolTable::with_builtins_for_wasm();
109
1
    let big = Expr::Number(Number::Index(i64::from(i32::MAX) + 1));
110
1
    assert!(compile_any_call(&mut ctx, &mut emit, &mut symbols, callee, &[big]).is_err());
111
1
}
112

            
113
2
fn compiled(source: &str, mode: CompileMode) -> Vec<u8> {
114
2
    let program = Reader::parse(source).expect("parses");
115
2
    let mut symbols = SymbolTable::with_builtins_for_wasm();
116
2
    Compiler::new()
117
2
        .compile_with_mode(&program, &mut symbols, mode)
118
2
        .expect("compiles")
119
2
}
120

            
121
#[test]
122
1
fn both_drivers_fill_every_reserved_helper() {
123
1
    let source = "(funcall (car (list (lambda () 1) (lambda (x) x) nil)))";
124
1
    for mode in [CompileMode::Script, CompileMode::Eval] {
125
2
        assert!(!compiled(source, mode).is_empty());
126
    }
127
1
}
128

            
129
#[test]
130
1
fn a_unit_without_any_call_reserves_no_helper() {
131
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
132
1
    fill_any_call_helpers(&mut ctx).expect("fills");
133
1
    assert!(ctx.take_any_call_helpers().is_empty());
134
1
}
135

            
136
/// The mirror: eval, the stack classifier and codegen agree that a call of an
137
/// `any` value is `any`, whatever the candidates return.
138
#[test]
139
1
fn an_any_call_is_any_on_every_surface() {
140
1
    let forms = [
141
1
        "(funcall (car (list (lambda () 1) nil)))",
142
1
        "(let ((g (car (list (lambda () 1) nil)))) (funcall g))",
143
1
        "(funcall (if (< (entity-count) 1) (lambda () 1) (lambda (x) x)))",
144
1
    ];
145
1
    for source in forms {
146
3
        let form = Reader::parse_expr(source).expect("parses");
147
3
        let mut symbols = SymbolTable::with_builtins_for_wasm();
148
3
        let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
149
3
        let mut emit = FunctionEmitter::new();
150
3
        let evaluated = probe_value(&mut symbols.clone(), &form).expect("probes");
151
3
        let pushed = compile_for_stack(&mut ctx, &mut emit, &mut symbols, &form).expect("compiles");
152
3
        assert_eq!(
153
3
            classify_stack_type(&evaluated),
154
3
            Some(WasmType::AnyRef),
155
            "{source}"
156
        );
157
3
        assert_eq!(pushed, WasmType::AnyRef, "{source}");
158
    }
159
1
}
160

            
161
#[test]
162
1
fn signatures_of_one_wasm_shape_test_by_their_own_id() {
163
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
164
1
    let boolean = ctx
165
1
        .intern_closure_signature(&[], WasmType::Bool)
166
1
        .expect("sig");
167
1
    let index = ctx
168
1
        .intern_closure_signature(&[], WasmType::I32)
169
1
        .expect("sig");
170
1
    let sigs = ctx.closure_sigs_of_arity(0);
171
1
    assert_eq!(sigs.len(), 2);
172
1
    assert_ne!(sigs[0].id, sigs[1].id);
173
1
    assert_eq!((sigs[0].id, sigs[1].id), (boolean, index));
174
1
    let mut emit = FunctionEmitter::new();
175
1
    emit_sig_test(&ctx, &mut emit, &sigs[1]).expect("emits");
176
1
    emit.finish(&[]).expect("a balanced test");
177
1
}