1
use wasm_encoder::{Function, Instruction};
2
use wasmparser::{CompositeInnerType, Parser, Payload, SubType};
3

            
4
use super::CompileContext;
5
use crate::ast::StructId;
6
use crate::runtime::StructLayout;
7

            
8
/// Every type of `wasm` with the size of the recursion group it sits in.
9
1
fn types_of(wasm: &[u8]) -> Vec<(usize, SubType)> {
10
1
    let mut found = Vec::new();
11
1
    for payload in Parser::new(0).parse_all(wasm) {
12
85
        if let Payload::TypeSection(reader) = payload.expect("payload") {
13
1
            for group in reader {
14
60
                let types: Vec<SubType> = group.expect("group").into_types().collect();
15
60
                let size = types.len();
16
66
                found.extend(types.into_iter().map(|ty| (size, ty)));
17
            }
18
84
        }
19
    }
20
1
    found
21
1
}
22

            
23
4
fn finished(mut ctx: CompileContext) -> Vec<u8> {
24
4
    ctx.add_should_apply(CompileContext::default_should_apply(), usize::MAX);
25
4
    let mut process = Function::new([]);
26
4
    process.instruction(&Instruction::End);
27
4
    ctx.add_process(process);
28
4
    ctx.finish()
29
4
}
30

            
31
#[test]
32
1
fn the_struct_shapes_are_distinct_from_every_other_registered_type() {
33
1
    let ctx = CompileContext::new_with_host_fns(&[]).unwrap();
34
1
    let (slots, instance) = (ctx.ids.ty_slots as usize, ctx.ids.ty_struct as usize);
35
1
    let types = types_of(&finished(ctx));
36
1
    for index in [slots, instance] {
37
2
        let (group, ty) = &types[index];
38
2
        assert_eq!(*group, 1, "type {index} shares a recursion group");
39
2
        let twins = types
40
2
            .iter()
41
132
            .filter(|(size, other)| *size == 1 && other.composite_type == ty.composite_type)
42
            .count();
43
2
        assert_eq!(twins, 1, "type {index} is structurally equal to another");
44
    }
45
1
    assert!(matches!(
46
1
        types[slots].1.composite_type.inner,
47
        CompositeInnerType::Array(_)
48
    ));
49
1
    assert!(matches!(
50
1
        types[instance].1.composite_type.inner,
51
        CompositeInnerType::Struct(_)
52
    ));
53
1
}
54

            
55
#[test]
56
1
fn a_layout_global_is_built_once_and_the_module_validates() {
57
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
58
1
    let layout = StructLayout {
59
1
        name: "PT".to_string(),
60
1
        fields: vec!["X".to_string(), "Y".to_string()],
61
1
    };
62
1
    let first = ctx.struct_layout_global(StructId(3), &layout).unwrap();
63
1
    let again = ctx.struct_layout_global(StructId(3), &layout).unwrap();
64
1
    let other = ctx.struct_layout_global(StructId(4), &layout).unwrap();
65
1
    assert_eq!(first, again);
66
1
    assert_ne!(first, other);
67
1
    assert!(wasmparser::validate(&finished(ctx)).is_ok());
68
1
}
69

            
70
#[test]
71
1
fn a_reused_id_with_another_layout_gets_a_global_of_its_own() {
72
1
    let mut ctx = CompileContext::new_with_host_fns(&[]).unwrap();
73
    let layout = |name: &str, fields: &[&str]| StructLayout {
74
2
        name: name.to_string(),
75
2
        fields: fields.iter().map(ToString::to_string).collect(),
76
2
    };
77
1
    let rolled_back = ctx
78
1
        .struct_layout_global(StructId(0), &layout("PT", &["X"]))
79
1
        .unwrap();
80
1
    let reused = layout("BOX", &["P", "Q"]);
81
1
    let global = ctx.struct_layout_global(StructId(0), &reused).unwrap();
82
1
    assert_ne!(rolled_back, global);
83
1
    assert_eq!(
84
1
        ctx.struct_layout_global(StructId(0), &reused).unwrap(),
85
1
        global
86
    );
87
1
    assert!(wasmparser::validate(&finished(ctx)).is_ok());
88
1
}
89

            
90
#[test]
91
1
fn the_struct_helpers_are_registered_in_both_module_modes() {
92
1
    let script = CompileContext::new_with_host_fns(&[]).unwrap();
93
1
    let mut eval = CompileContext::new_eval_with_host_fns(&[]).unwrap();
94
1
    let mut body = Function::new([]);
95
1
    body.instruction(&Instruction::RefNull(wasm_encoder::HeapType::ANY));
96
1
    body.instruction(&Instruction::End);
97
1
    eval.add_nomi_eval(body);
98
1
    assert_ne!(script.ids.struct_new, u32::MAX);
99
1
    assert_ne!(eval.ids.struct_copy, u32::MAX);
100
1
    assert!(wasmparser::validate(&finished(script)).is_ok());
101
1
    assert!(wasmparser::validate(&eval.finish()).is_ok());
102
1
}