1
//! A whole-state digest of a `CompileContext`, for rollback tests. Every field
2
//! is destructured, so a new field fails to compile until it is covered.
3

            
4
use std::collections::{HashMap, HashSet};
5
use std::fmt::Debug;
6
use std::fmt::Write as _;
7

            
8
use wasm_encoder::Encode;
9

            
10
use super::CompileContext;
11

            
12
2084
fn section(out: &mut String, name: &str, value: &impl Encode) {
13
2084
    let mut bytes = Vec::new();
14
2084
    value.encode(&mut bytes);
15
2084
    let _ = writeln!(out, "{name}: {bytes:?}");
16
2084
}
17

            
18
104
fn map<K: Debug, V: Debug>(out: &mut String, name: &str, value: &HashMap<K, V>) {
19
2215
    let mut entries: Vec<String> = value.iter().map(|entry| format!("{entry:?}")).collect();
20
104
    entries.sort();
21
104
    let _ = writeln!(out, "{name}: {entries:?}");
22
104
}
23

            
24
26
fn set<K: Debug>(out: &mut String, name: &str, value: &HashSet<K>) {
25
26
    let mut entries: Vec<String> = value.iter().map(|entry| format!("{entry:?}")).collect();
26
26
    entries.sort();
27
26
    let _ = writeln!(out, "{name}: {entries:?}");
28
26
}
29

            
30
546
fn debug(out: &mut String, name: &str, value: &impl Debug) {
31
546
    let _ = writeln!(out, "{name}: {value:?}");
32
546
}
33

            
34
impl CompileContext {
35
26
    pub(crate) fn state_digest(&self) -> String {
36
        let CompileContext {
37
            types,
38
            imports,
39
            functions,
40
            memories,
41
            tags,
42
            globals,
43
            global_count,
44
            exports,
45
            data,
46
            codes,
47
            data_count,
48
            type_count,
49
            tag_count,
50
            import_func_count,
51
            local_func_count,
52
            type_cache,
53
            func_names,
54
            host_fns,
55
            pending_helpers,
56
            next_local,
57
            local_types,
58
            serializer,
59
            closures,
60
            declared_funcs,
61
            monomorphs,
62
            nomi_error_tag,
63
            ids,
64
            inlining_stack,
65
            inlining_set,
66
            block_labels,
67
            hidden_labels,
68
            outer_labels,
69
            tag_frames,
70
            unwind_frames,
71
            block_joins,
72
            stats,
73
            read_checks,
74
            struct_layouts,
75
            struct_serial,
76
            struct_serial_base,
77
            instance_globals,
78
            instances_init,
79
26
        } = self;
80
26
        let mut out = String::new();
81
26
        section(&mut out, "types", types);
82
26
        section(&mut out, "imports", imports);
83
26
        section(&mut out, "functions", functions);
84
26
        section(&mut out, "memories", memories);
85
26
        section(&mut out, "tags", tags);
86
26
        section(&mut out, "globals", globals);
87
26
        section(&mut out, "exports", exports);
88
26
        section(&mut out, "data", data);
89
26
        section(&mut out, "codes", codes);
90
26
        for (index, helper) in pending_helpers.iter().enumerate() {
91
1850
            section(&mut out, &format!("pending_helper[{index}]"), helper);
92
        }
93
26
        let counters = (
94
26
            global_count,
95
26
            data_count,
96
26
            type_count,
97
26
            tag_count,
98
26
            import_func_count,
99
26
            local_func_count,
100
26
            next_local,
101
26
        );
102
26
        debug(&mut out, "counters", &counters);
103
26
        let mut caches: Vec<String> = type_cache
104
26
            .iter()
105
862
            .map(|(params, results)| {
106
                let mut inner: Vec<String> =
107
1252
                    results.iter().map(|entry| format!("{entry:?}")).collect();
108
862
                inner.sort();
109
862
                format!("{params:?} -> {inner:?}")
110
862
            })
111
            .collect();
112
26
        caches.sort();
113
26
        debug(&mut out, "type_cache", &caches);
114
26
        map(&mut out, "func_names", func_names);
115
26
        map(&mut out, "host_fns", host_fns);
116
26
        map(&mut out, "read_checks", read_checks);
117
26
        map(&mut out, "struct_layouts", struct_layouts);
118
26
        debug(&mut out, "struct_serial", struct_serial);
119
26
        debug(&mut out, "struct_serial_base", struct_serial_base);
120
26
        debug(&mut out, "instance_globals", instance_globals);
121
26
        debug(&mut out, "instances_init", instances_init);
122
26
        set(&mut out, "inlining_set", inlining_set);
123
26
        debug(&mut out, "local_types", local_types);
124
26
        debug(&mut out, "serializer", serializer);
125
26
        debug(&mut out, "closures", closures);
126
26
        debug(&mut out, "declared_funcs", declared_funcs);
127
26
        debug(&mut out, "monomorphs", monomorphs);
128
26
        debug(&mut out, "nomi_error_tag", nomi_error_tag);
129
26
        debug(&mut out, "ids", ids);
130
26
        debug(&mut out, "inlining_stack", inlining_stack);
131
26
        debug(&mut out, "block_labels", block_labels);
132
26
        debug(&mut out, "hidden_labels", hidden_labels);
133
26
        debug(&mut out, "outer_labels", outer_labels);
134
26
        debug(&mut out, "tag_frames", tag_frames);
135
26
        debug(&mut out, "unwind_frames", unwind_frames);
136
26
        debug(&mut out, "block_joins", block_joins);
137
26
        debug(&mut out, "stats", stats);
138
26
        out
139
26
    }
140
}