1
use super::*;
2
use crate::ast::Number;
3
use crate::ast::{ClosureSigId, EntityKind, Fraction, PairElement};
4
use crate::compiler::CompileContext;
5
use crate::compiler::emit::FunctionEmitter;
6
use crate::compiler::expr::call::compile_closure_arg;
7
use crate::runtime::{Symbol, SymbolKind};
8

            
9
const TYPES: [WasmType; 10] = [
10
    WasmType::I32,
11
    WasmType::Bool,
12
    WasmType::Ratio,
13
    WasmType::Commodity,
14
    WasmType::StringRef,
15
    WasmType::SymbolRef,
16
    WasmType::PairRef(PairElement::I32),
17
    WasmType::EntityRef(EntityKind::Account),
18
    WasmType::Closure(ClosureSigId(0)),
19
    WasmType::AnyRef,
20
];
21

            
22
1
fn arguments() -> Vec<Expr> {
23
1
    let mut args = vec![
24
1
        Expr::Nil,
25
1
        Expr::T,
26
1
        Expr::Number(Number::Index(3)),
27
1
        Expr::Number(Number::Scalar(Fraction::new(1, 2))),
28
1
        Expr::String("s".to_string()),
29
1
        Expr::List(vec![
30
1
            Expr::Symbol("QUOTE".to_string()),
31
1
            Expr::Symbol("A".to_string()),
32
1
        ]),
33
    ];
34
10
    args.extend(TYPES.iter().map(|ty| Expr::WasmLocal(0, *ty)));
35
1
    args
36
1
}
37

            
38
#[test]
39
1
fn call_fits_matches_the_closure_argument() {
40
1
    for arg in arguments() {
41
16
        for param in TYPES {
42
160
            let mut ctx = CompileContext::new_with_host_fns(&[]).expect("ctx");
43
160
            let mut emit = FunctionEmitter::new();
44
160
            let mut symbols = SymbolTable::with_builtins_for_wasm();
45
160
            let accepted = compile_closure_arg(&mut ctx, &mut emit, &mut symbols, &arg, param);
46
160
            let mut probe = SymbolTable::with_builtins_for_wasm();
47
160
            let value = probe_value(&mut probe, &arg).expect("probe");
48
160
            let fits = call_fits(&mut probe, &arg, &value, param);
49
160
            assert_eq!(
50
160
                fits.expect("probe"),
51
160
                accepted.is_ok(),
52
                "call_fits disagrees with compile_closure_arg for {arg:?} into {param:?}"
53
            );
54
        }
55
    }
56
1
}
57

            
58
#[test]
59
1
fn a_call_without_a_recorded_source_is_never_respecialised() {
60
1
    let symbols = SymbolTable::with_builtins_for_wasm();
61
1
    let found = respecialised(&symbols, Place::Local(9), &[], &[]);
62
1
    assert_eq!(found.expect("no probe runs"), None);
63
1
}
64

            
65
9
fn lambda(params: &[&str], body: &str) -> (LambdaParams, Expr) {
66
10
    let params = LambdaParams::simple(params.iter().map(|p| p.to_uppercase()).collect());
67
9
    let body = crate::reader::Reader::parse_expr(body).expect("parses");
68
9
    (params, body)
69
9
}
70

            
71
#[test]
72
1
fn nil_into_a_never_nil_parameter_the_body_typed_keeps_the_call() {
73
1
    let mut probe = SymbolTable::with_builtins_for_wasm();
74
1
    for param in [WasmType::I32, WasmType::Ratio, WasmType::Commodity] {
75
3
        assert!(!call_fits(&mut probe, &Expr::Nil, &Expr::Nil, param).expect("fits"));
76
3
        assert!(keeps_closure_call(&mut probe, &Expr::Nil, param, || true).expect("probe"));
77
3
        assert!(!keeps_closure_call(&mut probe, &Expr::Nil, param, || false).expect("probe"));
78
    }
79
1
}
80

            
81
#[test]
82
1
fn only_arithmetic_comparison_and_pins_are_evidence() {
83
1
    let symbols = SymbolTable::with_builtins_for_wasm();
84
7
    let evidence = |params: &[&str], body: &str, index| {
85
7
        let (params, body) = lambda(params, body);
86
7
        has_type_evidence(&symbols, &params, &body, index)
87
7
    };
88
1
    assert!(evidence(&["n"], "(+ n 1)", 0));
89
1
    assert!(evidence(&["n"], "(if t (< 1 n) 2)", 0));
90
1
    assert!(evidence(&["n"], "(cons 1 n)", 0));
91
1
    assert!(!evidence(&["n"], "(if n 1 2)", 0));
92
1
    assert!(!evidence(&["n"], "(let ((n 2)) (+ n 1))", 0));
93
1
    assert!(!evidence(&["n"], "'(+ n 1)", 0));
94
1
    assert!(!evidence(&["a", "b"], "(+ a 1)", 1));
95
1
}
96

            
97
2
fn closure_calling_h(local: bool) -> SymbolTable {
98
2
    let mut symbols = SymbolTable::with_builtins_for_wasm();
99
2
    let (params, body) = lambda(&["z"], "(h z)");
100
2
    symbols.record_closure_body(Place::Local(0), params, body);
101
2
    let mut function = Symbol::new("H", SymbolKind::Function).with_function(Expr::Nil);
102
2
    if local {
103
1
        function = function.local_function();
104
1
    }
105
2
    symbols.define(function);
106
2
    symbols
107
2
}
108

            
109
#[test]
110
1
fn a_source_calling_a_global_defun_is_respecialised_for_a_scalar() {
111
1
    let symbols = closure_calling_h(false);
112
1
    let scalar = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
113
1
    let found = respecialised(&symbols, Place::Local(0), &[WasmType::I32], &[scalar]);
114
1
    assert!(found.expect("probe").is_some());
115
1
}
116

            
117
#[test]
118
1
fn a_source_calling_a_labels_function_is_never_respecialised() {
119
1
    let symbols = closure_calling_h(true);
120
1
    let scalar = Expr::Number(Number::Scalar(Fraction::new(1, 2)));
121
1
    let found = respecialised(&symbols, Place::Local(0), &[WasmType::I32], &[scalar]);
122
1
    assert_eq!(found.expect("probe"), None);
123
1
}