1
use super::*;
2

            
3
const NO_EXPORTS: &str = "";
4

            
5
/// Runs `alloc_value` for `value` inside a host fn of a module whose extra items are `wat`
6
/// and returns the refusal message.
7
8
async fn refusal(wat: &str, value: EvalValue) -> String {
8
8
    let engine = build_engine(EngineOpts::baseline()).unwrap();
9
8
    let wat = format!(
10
        r#"(module (import "test" "encode" (func $encode))
11
8
            {wat} (func (export "go") (call $encode)))"#
12
    );
13
8
    let module = compile_wat(&engine, &wat).unwrap();
14
8
    let mut linker: Linker<Option<String>> = Linker::new(&engine);
15
8
    linker
16
        .func_wrap_async(
17
8
            "test",
18
8
            "encode",
19
8
            move |mut caller: Caller<'_, Option<String>>, ()| {
20
8
                let value = value.clone();
21
8
                Box::new(async move {
22
8
                    let outcome = alloc_value(&mut caller, &value).await;
23
8
                    *caller.data_mut() = outcome.err().map(|err| err.to_string());
24
8
                    Ok(())
25
8
                })
26
8
            },
27
        )
28
        .unwrap();
29
8
    let mut store = Store::new(&engine, None);
30
8
    store.set_epoch_deadline(1_000);
31
8
    let instance = linker.instantiate_async(&mut store, &module).await.unwrap();
32
8
    let go = instance.get_func(&mut store, "go").unwrap();
33
8
    go.call_async(&mut store, &[], &mut []).await.unwrap();
34
8
    store.into_data().expect("alloc_value must refuse")
35
8
}
36

            
37
#[tokio::test(flavor = "current_thread")]
38
1
async fn bytes_have_no_host_built_form() {
39
1
    let message = refusal(NO_EXPORTS, EvalValue::Bytes(vec![1])).await;
40
1
    assert!(message.contains("bytes or a commodity"), "{message}");
41
1
}
42

            
43
#[tokio::test(flavor = "current_thread")]
44
1
async fn a_commodity_has_no_host_built_form() {
45
1
    let commodity = EvalValue::Commodity {
46
1
        numer: 1,
47
1
        denom: 1,
48
1
        commodity_hi: 0,
49
1
        commodity_lo: 0,
50
1
    };
51
1
    let message = refusal(NO_EXPORTS, commodity).await;
52
1
    assert!(message.contains("bytes or a commodity"), "{message}");
53
1
}
54

            
55
#[tokio::test(flavor = "current_thread")]
56
1
async fn an_opaque_value_has_no_host_built_form() {
57
1
    let message = refusal(NO_EXPORTS, EvalValue::Opaque).await;
58
1
    assert!(message.contains("opaque value"), "{message}");
59
1
}
60

            
61
#[tokio::test(flavor = "current_thread")]
62
1
async fn an_index_beyond_31_bits_is_refused() {
63
1
    let message = refusal(NO_EXPORTS, EvalValue::I32(i32::MAX)).await;
64
1
    assert!(message.contains("exceeds 31 bits"), "{message}");
65
1
}
66

            
67
#[tokio::test(flavor = "current_thread")]
68
1
async fn t_needs_the_t_global_export() {
69
1
    let message = refusal(NO_EXPORTS, EvalValue::T).await;
70
1
    assert!(
71
1
        message.contains("does not export the `t` global"),
72
1
        "{message}"
73
1
    );
74
1
}
75

            
76
#[tokio::test(flavor = "current_thread")]
77
1
async fn t_must_be_a_non_null_reference() {
78
1
    let wat = r#"(global (export "t") anyref (ref.null any))"#;
79
1
    let message = refusal(wat, EvalValue::T).await;
80
1
    assert!(
81
1
        message.contains("not a non-null internal reference"),
82
1
        "{message}"
83
1
    );
84
1
}
85

            
86
#[tokio::test(flavor = "current_thread")]
87
1
async fn a_symbol_needs_the_symbol_new_export() {
88
1
    let message = refusal(NO_EXPORTS, EvalValue::Symbol("A".into())).await;
89
1
    assert!(
90
1
        message.contains("does not export `symbol_new`"),
91
1
        "{message}"
92
1
    );
93
1
}
94

            
95
#[tokio::test(flavor = "current_thread")]
96
1
async fn symbol_new_must_return_a_symbol() {
97
1
    let wat = r#"(func (export "symbol_new") (param anyref) (result anyref)
98
1
                    (ref.null any))"#;
99
1
    let message = refusal(wat, EvalValue::Symbol("A".into())).await;
100
1
    assert!(
101
1
        message.contains("symbol_new returned no symbol"),
102
1
        "{message}"
103
1
    );
104
1
}