1
//! Scratch summaries and splice re-export of `FunctionEmitter` (plan 0014 P4).
2

            
3
use wasm_encoder::{BlockType, Catch};
4

            
5
use super::super::FunctionEmitter;
6

            
7
const EMPTY: BlockType = BlockType::Empty;
8

            
9
20
fn exits(emit: &FunctionEmitter) -> Vec<usize> {
10
20
    emit.frame.exits.iter().copied().collect()
11
20
}
12

            
13
6
fn marked(emit: &FunctionEmitter) -> Vec<bool> {
14
17
    emit.frame.nested.iter().map(|f| f.end_targeted).collect()
15
6
}
16

            
17
15
fn closed(emit: &mut FunctionEmitter) -> bool {
18
15
    emit.block_end().expect("matching end");
19
15
    emit.frame.is_live()
20
15
}
21

            
22
10
fn scratch_with_br_ifs(open: usize, depths: &[u32]) -> FunctionEmitter {
23
10
    let mut scratch = FunctionEmitter::new_seeded(0);
24
10
    for _ in 0..open {
25
1
        scratch.block_start();
26
    }
27
10
    for &depth in depths {
28
46
        scratch.br_if(depth);
29
    }
30
10
    scratch
31
10
}
32

            
33
#[test]
34
1
fn branches_beyond_the_open_frames_are_recorded_parent_relative() {
35
1
    let scratch = scratch_with_br_ifs(1, &[0, 1, 1, 2, 4]);
36
1
    assert_eq!(exits(&scratch), [0, 1, 3]);
37
1
    assert_eq!(marked(&scratch), [true], "depth 0 marks the open block");
38
1
}
39

            
40
#[test]
41
1
fn equality_depth_with_no_open_frame_is_an_exit() {
42
1
    let scratch = scratch_with_br_ifs(0, &[0, 0, 1, 2]);
43
1
    assert_eq!(exits(&scratch), [0, 1, 2]);
44
1
}
45

            
46
#[test]
47
1
fn br_table_records_every_outside_target_and_the_default() {
48
1
    let mut scratch = FunctionEmitter::new_seeded(0);
49
1
    scratch.block_start();
50
1
    scratch.br_table(&[0, 1, 1, 2], 4);
51
1
    assert_eq!(exits(&scratch), [0, 1, 3]);
52
1
    assert_eq!(marked(&scratch), [true], "target 0 marks the open block");
53
1
}
54

            
55
#[test]
56
1
fn try_table_records_catch_labels_outside_the_scratch() {
57
1
    let mut scratch = FunctionEmitter::new_seeded(0);
58
1
    scratch.try_table(
59
1
        EMPTY,
60
1
        &[
61
1
            Catch::All { label: 0 },
62
1
            Catch::All { label: 2 },
63
1
            Catch::All { label: 2 },
64
1
            Catch::All { label: 3 },
65
1
        ],
66
1
    );
67
1
    assert_eq!(exits(&scratch), [0, 2, 3]);
68
1
}
69

            
70
#[test]
71
1
fn dead_branches_record_nothing() {
72
1
    let mut scratch = FunctionEmitter::new_seeded(0);
73
1
    scratch.unreachable();
74
1
    scratch.br_if(0);
75
1
    scratch.br_if(1);
76
1
    scratch.br_if(2);
77
1
    scratch.br_table(&[0, 1], 2);
78
1
    assert!(exits(&scratch).is_empty());
79
1
}
80

            
81
#[test]
82
1
fn return_from_in_an_if_arm_scratch_targets_the_block() {
83
1
    let mut parent = FunctionEmitter::new();
84
1
    parent.block_start();
85
1
    parent.if_block(EMPTY);
86
1
    let mut then_arm = FunctionEmitter::new_seeded(2);
87
1
    then_arm.br(1);
88
1
    assert_eq!(exits(&then_arm), [1]);
89
1
    parent.splice(&then_arm).expect("balanced");
90
1
    parent.else_block();
91
1
    parent.unreachable();
92
1
    assert!(!closed(&mut parent), "the if has no reachable end");
93
1
    assert!(closed(&mut parent), "the block end is the scratch's target");
94
1
}
95

            
96
/// `(block b (if … (return-from b 1) 2))` as compiled: the then-arm scratch
97
/// sits inside the block-body scratch, whose only open frame is the `if`.
98
#[test]
99
1
fn block_body_scratch_exports_a_return_from_at_the_equality_boundary() {
100
1
    let mut then_arm = FunctionEmitter::new_seeded(2);
101
1
    then_arm.br(1);
102
1
    let mut body = FunctionEmitter::new_seeded(1);
103
1
    body.if_block(EMPTY);
104
1
    body.splice(&then_arm).expect("balanced");
105
1
    assert_eq!(
106
1
        exits(&body),
107
1
        [0],
108
        "depth 1 equals the body's one open frame"
109
    );
110
1
    body.else_block();
111
1
    assert!(closed(&mut body), "the else arm falls through");
112

            
113
1
    let mut parent = FunctionEmitter::new();
114
1
    parent.block_start();
115
1
    parent.splice(&body).expect("balanced");
116
1
    parent.unreachable();
117
1
    assert!(closed(&mut parent), "b is end-targeted");
118
1
}
119

            
120
#[test]
121
1
fn splice_applies_every_entry_to_the_parent_frames() {
122
1
    let mut parent = FunctionEmitter::new();
123
1
    parent.block_start();
124
1
    parent.block_start();
125
1
    parent.block_start();
126
1
    let scratch = scratch_with_br_ifs(0, &[0, 2, 2, 1]);
127
1
    parent.splice(&scratch).expect("balanced");
128
1
    parent.unreachable();
129
1
    assert!(closed(&mut parent), "depth 0");
130
1
    parent.unreachable();
131
1
    assert!(closed(&mut parent), "depth 1");
132
1
    parent.unreachable();
133
1
    assert!(closed(&mut parent), "depth 2");
134
1
    assert!(exits(&parent).is_empty());
135
1
}
136

            
137
#[test]
138
1
fn splice_never_marks_a_loop_frame() {
139
1
    let mut parent = FunctionEmitter::new();
140
1
    parent.block_start();
141
1
    parent.loop_start();
142
1
    let scratch = scratch_with_br_ifs(0, &[0, 0, 1, 2]);
143
1
    parent.splice(&scratch).expect("balanced");
144
1
    assert_eq!(marked(&parent), [true, false], "only the block is marked");
145
1
    parent.unreachable();
146
1
    assert!(!closed(&mut parent), "a branch to a loop is not its end");
147
1
    parent.unreachable();
148
1
    assert!(closed(&mut parent));
149
1
    assert_eq!(exits(&parent), [0]);
150
1
}
151

            
152
#[test]
153
1
fn splice_reexports_entries_beyond_the_parent_frames() {
154
1
    let mut parent = FunctionEmitter::new_seeded(0);
155
1
    parent.block_start();
156
1
    let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 3, 3]);
157
1
    parent.splice(&scratch).expect("balanced");
158
1
    assert_eq!(exits(&parent), [0, 1, 2]);
159
1
}
160

            
161
#[test]
162
1
fn a_scratch_spliced_dead_contributes_nothing() {
163
1
    let mut parent = FunctionEmitter::new_seeded(0);
164
1
    parent.block_start();
165
1
    parent.block_start();
166
1
    parent.unreachable();
167
1
    let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 2, 3]);
168
1
    assert_eq!(exits(&scratch), [0, 1, 2, 3]);
169
1
    parent.splice(&scratch).expect("balanced");
170
1
    assert!(exits(&parent).is_empty());
171
1
    assert!(!closed(&mut parent));
172
1
    assert!(!closed(&mut parent));
173
1
}
174

            
175
#[test]
176
1
fn nested_scratches_mark_the_block_after_both_splices() {
177
1
    let mut parent = FunctionEmitter::new_seeded(0);
178
1
    parent.block_start();
179
1
    parent.if_block(EMPTY);
180

            
181
1
    let mut arm = FunctionEmitter::new_seeded(2);
182
1
    arm.block_start();
183
1
    let clause = scratch_with_br_ifs(0, &[0, 0, 2, 3]);
184
1
    assert_eq!(exits(&clause), [0, 2, 3]);
185
1
    arm.splice(&clause).expect("balanced");
186
1
    assert_eq!(exits(&arm), [1, 2], "entries 2 and 3 cross the arm's block");
187
1
    arm.unreachable();
188
1
    assert!(closed(&mut arm), "entry 0 marked the arm's own block");
189
1
    arm.unreachable();
190

            
191
1
    parent.splice(&arm).expect("balanced");
192
1
    assert_eq!(exits(&parent), [0], "entry 2 crosses the parent too");
193
1
    parent.else_block();
194
1
    parent.unreachable();
195
1
    assert!(!closed(&mut parent), "the if has no reachable end");
196
1
    assert!(closed(&mut parent), "entry 1 marked the outer block");
197
1
}
198

            
199
#[test]
200
1
fn reexport_crosses_two_scratch_boundaries() {
201
1
    let clause = scratch_with_br_ifs(0, &[0, 1, 2, 2]);
202
1
    let mut arm = FunctionEmitter::new_seeded(1);
203
1
    arm.splice(&clause).expect("balanced");
204
1
    assert_eq!(exits(&arm), [0, 1, 2]);
205

            
206
1
    let mut parent = FunctionEmitter::new_seeded(0);
207
1
    parent.block_start();
208
1
    parent.splice(&arm).expect("balanced");
209
1
    assert_eq!(exits(&parent), [0, 1]);
210
1
    parent.unreachable();
211
1
    assert!(closed(&mut parent), "entry 0 marked the parent's block");
212
1
}
213

            
214
/// A backward `(go tag)` from an `if`-arm scratch in a non-final TAGBODY
215
/// segment: the parent is the exit block, the dispatch loop and two still-open
216
/// tag blocks, so the scratch's exits reach the loop behind them.
217
#[test]
218
1
fn splice_from_a_tagbody_segment_skips_only_the_dispatch_loop() {
219
1
    let mut parent = FunctionEmitter::new();
220
1
    parent.block_start();
221
1
    parent.loop_start();
222
1
    parent.block_start();
223
1
    parent.block_start();
224
1
    let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 2, 3]);
225
1
    parent.splice(&scratch).expect("balanced");
226
1
    assert_eq!(marked(&parent), [true, false, true, true]);
227
1
    assert!(exits(&parent).is_empty());
228
1
}
229

            
230
#[test]
231
1
fn splice_marks_if_and_try_table_frames_but_not_a_loop() {
232
1
    let mut parent = FunctionEmitter::new();
233
1
    parent.if_block(EMPTY);
234
1
    parent.try_table(EMPTY, &[]);
235
1
    parent.loop_start();
236
1
    parent.try_table(EMPTY, &[]);
237
1
    parent.if_block(EMPTY);
238
1
    let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 2, 3, 4]);
239
1
    parent.splice(&scratch).expect("balanced");
240
1
    assert_eq!(marked(&parent), [true, true, false, true, true]);
241
1
    assert!(exits(&parent).is_empty());
242
1
}
243

            
244
#[test]
245
1
fn a_branch_marks_only_the_frames_it_names() {
246
1
    let mut scratch = FunctionEmitter::new_seeded(0);
247
1
    scratch.if_block(EMPTY);
248
1
    scratch.block_start();
249
1
    scratch.try_table(EMPTY, &[]);
250
1
    scratch.if_block(EMPTY);
251
1
    scratch.br_table(&[0, 2, 2], 4);
252
1
    assert_eq!(marked(&scratch), [false, true, false, true]);
253
1
    assert_eq!(exits(&scratch), [0]);
254
1
}