1
use std::collections::HashMap;
2

            
3
use super::*;
4
use crate::ast::{ClosureSigId, StructId};
5

            
6
15
fn field_id() -> StructId {
7
15
    StructId(3)
8
15
}
9

            
10
#[test]
11
1
fn a_field_read_before_any_store_is_provisional_and_the_store_widens_it() {
12
1
    let mut symbols = SymbolTable::new();
13
1
    symbols.begin_home_attempt(HashMap::new());
14
1
    assert_eq!(symbols.field_type(field_id(), 0), WasmType::AnyRef);
15
1
    symbols.note_field_store(field_id(), 0, Arm::Value(WasmType::I32));
16
1
    assert_eq!(symbols.field_type(field_id(), 0), WasmType::AnyRef);
17
1
    assert_eq!(
18
1
        symbols.widened_homes(),
19
1
        Some(HashMap::from([(
20
1
            HomeKey::Field(field_id(), 0),
21
1
            WasmType::I32
22
1
        )]))
23
    );
24
1
}
25

            
26
#[test]
27
1
fn the_first_field_store_types_the_home_and_a_second_kind_widens_it() {
28
1
    let mut symbols = SymbolTable::new();
29
1
    symbols.begin_home_attempt(HashMap::new());
30
1
    symbols.note_field_store(field_id(), 1, Arm::Value(WasmType::I32));
31
1
    assert_eq!(symbols.field_type(field_id(), 1), WasmType::I32);
32
1
    assert_eq!(symbols.widened_homes(), None);
33
1
    symbols.note_field_store(field_id(), 1, Arm::Value(WasmType::StringRef));
34
1
    assert_eq!(
35
1
        symbols.widened_homes(),
36
1
        Some(HashMap::from([(
37
1
            HomeKey::Field(field_id(), 1),
38
1
            WasmType::AnyRef
39
1
        )]))
40
    );
41
1
}
42

            
43
#[test]
44
1
fn a_field_hint_types_the_next_attempt_and_a_closure_store_rides_as_any() {
45
1
    let mut symbols = SymbolTable::new();
46
1
    let hint = HomeKey::Field(field_id(), 0);
47
1
    symbols.begin_home_attempt(HashMap::from([(hint, WasmType::Ratio)]));
48
1
    assert_eq!(symbols.field_type(field_id(), 0), WasmType::Ratio);
49
1
    symbols.note_field_store(
50
1
        field_id(),
51
1
        0,
52
1
        Arm::Value(WasmType::Closure(ClosureSigId(0))),
53
1
    );
54
1
    assert_eq!(symbols.field_type(field_id(), 0), WasmType::Ratio);
55
1
    assert_eq!(
56
1
        symbols.widened_homes(),
57
1
        Some(HashMap::from([(
58
1
            HomeKey::Field(field_id(), 0),
59
1
            WasmType::AnyRef
60
1
        )]))
61
    );
62
1
}
63

            
64
#[test]
65
1
fn field_homes_are_counted_apart_from_globals_and_never_collide_with_them() {
66
1
    let mut symbols = SymbolTable::new();
67
1
    symbols.begin_home_attempt(HashMap::new());
68
1
    symbols.note_home(
69
1
        "G",
70
1
        vec![Arm::Value(WasmType::Ratio)],
71
1
        None,
72
1
        WasmType::Ratio,
73
1
    );
74
1
    symbols.note_field_store(field_id(), 0, Arm::Value(WasmType::I32));
75
1
    symbols.note_field_store(StructId(4), 0, Arm::Value(WasmType::I32));
76
1
    assert_eq!(symbols.site_home_count(), 2);
77
1
    assert_eq!(symbols.field_type(field_id(), 0), WasmType::I32);
78
1
    assert_eq!(symbols.hint(&HomeKey::global("G")), None);
79
1
    assert_eq!(symbols.widened_homes(), None);
80
1
}