1
//! The eval surface joins a BLOCK's evaluated exits with its tail as the
2
//! compiled block does, recording no exit from dead code.
3

            
4
use crate::ast::{Expr, WasmType};
5
use crate::compiler::CompileMode;
6
use crate::compiler::behaviour_oracle::{Outcome, observe};
7
use crate::compiler::expr::eval_value;
8
use crate::host_fn::HostFnSpec;
9
use crate::reader::Reader;
10
use crate::runtime::SymbolTable;
11

            
12
10
fn hosts() -> [HostFnSpec; 1] {
13
10
    [
14
10
        HostFnSpec::new("rpc-protocol-version", "nomi", "rpc_protocol_version")
15
10
            .returns(WasmType::I32),
16
10
    ]
17
10
}
18

            
19
9
fn predicted(source: &str) -> Option<WasmType> {
20
9
    let mut symbols = SymbolTable::with_builtins_for_wasm();
21
9
    symbols.register_host_fns(&hosts());
22
9
    let forms = Reader::parse(source).ok()?;
23
9
    match eval_value(&mut symbols, forms.exprs.first()?).ok()? {
24
9
        Expr::WasmRuntime(ty) => Some(ty),
25
        _ => None,
26
    }
27
9
}
28

            
29
#[test]
30
1
fn a_nil_exit_beside_an_index_tail_is_predicted_anyref() {
31
1
    assert_eq!(
32
1
        predicted("(block b (if (> (rpc-protocol-version) 0) (return-from b nil)) 5)"),
33
1
        Some(WasmType::AnyRef)
34
    );
35
1
}
36

            
37
#[test]
38
1
fn an_exit_to_an_outer_block_joins_the_outer_block() {
39
1
    assert_eq!(
40
1
        predicted(r#"(block a (block b (if (> (rpc-protocol-version) 0) (return-from a "s")) 3))"#),
41
1
        Some(WasmType::AnyRef)
42
    );
43
1
}
44

            
45
#[test]
46
1
fn an_exit_after_a_diverging_form_does_not_join() {
47
1
    assert_eq!(
48
1
        predicted(r#"(block b (return-from b 1) (return-from b "s"))"#),
49
1
        Some(WasmType::I32)
50
    );
51
1
    assert_eq!(
52
1
        predicted(r#"(block b (progn (return-from b 1) (return-from b "s")))"#),
53
1
        Some(WasmType::I32)
54
    );
55
1
}
56

            
57
#[test]
58
1
fn a_step_after_a_body_that_always_exits_does_not_join() {
59
1
    for head in ["do", "do*"] {
60
2
        let source = format!(
61
2
            r#"(block b ({head} ((i 0 (progn (return-from b "dead") (+ i 1)))) ((> i 0) 0) (return-from b 20)))"#
62
        );
63
2
        assert_eq!(predicted(&source), Some(WasmType::I32), "{head}");
64
    }
65
1
}
66

            
67
#[test]
68
1
fn a_live_step_and_a_result_form_exit_join() {
69
1
    assert_eq!(
70
1
        predicted(
71
1
            r#"(block b (do ((i 0 (progn (if (> (rpc-protocol-version) 0) (return-from b "s")) (+ i 1)))) ((> i 2) 0) (setf i i)))"#
72
1
        ),
73
1
        Some(WasmType::AnyRef)
74
    );
75
1
    assert_eq!(
76
1
        predicted(
77
1
            r#"(block b (do ((i 0 (+ i 1))) ((> i (rpc-protocol-version)) (return-from b "s")) (setf i i)) 5)"#
78
1
        ),
79
1
        Some(WasmType::AnyRef)
80
    );
81
1
}
82

            
83
#[test]
84
1
fn a_block_that_may_exit_nil_is_a_runtime_test() {
85
1
    let (behaviours, _) = observe(
86
1
        "(if (block b (if (> (rpc-protocol-version) 0) (return-from b nil)) 5) 1 2)",
87
1
        CompileMode::Eval,
88
1
        &hosts(),
89
1
    );
90
2
    let outcomes: Vec<Outcome> = behaviours.into_iter().map(|b| b.outcome).collect();
91
1
    assert!(
92
1
        matches!(
93
1
            outcomes.as_slice(),
94
            [Outcome::Value(empty), Outcome::Value(ledger)]
95
1
                if empty.contains("I64(2)") && ledger.contains("I64(1)")
96
        ),
97
        "{outcomes:?}"
98
    );
99
1
}
100

            
101
#[test]
102
1
fn an_exit_whose_value_raises_is_predicted_not_to_join() {
103
1
    assert_eq!(
104
1
        predicted(
105
1
            r#"(block b (if (> (rpc-protocol-version) 0) (return-from b (error 'boom "x"))) "s")"#
106
1
        ),
107
1
        Some(WasmType::StringRef)
108
    );
109
1
}