Lines
100 %
Functions
Branches
//! Scratch summaries and splice re-export of `FunctionEmitter` (plan 0014 P4).
use wasm_encoder::{BlockType, Catch};
use super::super::FunctionEmitter;
const EMPTY: BlockType = BlockType::Empty;
fn exits(emit: &FunctionEmitter) -> Vec<usize> {
emit.frame.exits.iter().copied().collect()
}
fn marked(emit: &FunctionEmitter) -> Vec<bool> {
emit.frame.nested.iter().map(|f| f.end_targeted).collect()
fn closed(emit: &mut FunctionEmitter) -> bool {
emit.block_end().expect("matching end");
emit.frame.is_live()
fn scratch_with_br_ifs(open: usize, depths: &[u32]) -> FunctionEmitter {
let mut scratch = FunctionEmitter::new_seeded(0);
for _ in 0..open {
scratch.block_start();
for &depth in depths {
scratch.br_if(depth);
scratch
#[test]
fn branches_beyond_the_open_frames_are_recorded_parent_relative() {
let scratch = scratch_with_br_ifs(1, &[0, 1, 1, 2, 4]);
assert_eq!(exits(&scratch), [0, 1, 3]);
assert_eq!(marked(&scratch), [true], "depth 0 marks the open block");
fn equality_depth_with_no_open_frame_is_an_exit() {
let scratch = scratch_with_br_ifs(0, &[0, 0, 1, 2]);
assert_eq!(exits(&scratch), [0, 1, 2]);
fn br_table_records_every_outside_target_and_the_default() {
scratch.br_table(&[0, 1, 1, 2], 4);
assert_eq!(marked(&scratch), [true], "target 0 marks the open block");
fn try_table_records_catch_labels_outside_the_scratch() {
scratch.try_table(
EMPTY,
&[
Catch::All { label: 0 },
Catch::All { label: 2 },
Catch::All { label: 3 },
],
);
assert_eq!(exits(&scratch), [0, 2, 3]);
fn dead_branches_record_nothing() {
scratch.unreachable();
scratch.br_if(0);
scratch.br_if(1);
scratch.br_if(2);
scratch.br_table(&[0, 1], 2);
assert!(exits(&scratch).is_empty());
fn return_from_in_an_if_arm_scratch_targets_the_block() {
let mut parent = FunctionEmitter::new();
parent.block_start();
parent.if_block(EMPTY);
let mut then_arm = FunctionEmitter::new_seeded(2);
then_arm.br(1);
assert_eq!(exits(&then_arm), [1]);
parent.splice(&then_arm).expect("balanced");
parent.else_block();
parent.unreachable();
assert!(!closed(&mut parent), "the if has no reachable end");
assert!(closed(&mut parent), "the block end is the scratch's target");
/// `(block b (if … (return-from b 1) 2))` as compiled: the then-arm scratch
/// sits inside the block-body scratch, whose only open frame is the `if`.
fn block_body_scratch_exports_a_return_from_at_the_equality_boundary() {
let mut body = FunctionEmitter::new_seeded(1);
body.if_block(EMPTY);
body.splice(&then_arm).expect("balanced");
assert_eq!(
exits(&body),
[0],
"depth 1 equals the body's one open frame"
body.else_block();
assert!(closed(&mut body), "the else arm falls through");
parent.splice(&body).expect("balanced");
assert!(closed(&mut parent), "b is end-targeted");
fn splice_applies_every_entry_to_the_parent_frames() {
let scratch = scratch_with_br_ifs(0, &[0, 2, 2, 1]);
parent.splice(&scratch).expect("balanced");
assert!(closed(&mut parent), "depth 0");
assert!(closed(&mut parent), "depth 1");
assert!(closed(&mut parent), "depth 2");
assert!(exits(&parent).is_empty());
fn splice_never_marks_a_loop_frame() {
parent.loop_start();
assert_eq!(marked(&parent), [true, false], "only the block is marked");
assert!(!closed(&mut parent), "a branch to a loop is not its end");
assert!(closed(&mut parent));
assert_eq!(exits(&parent), [0]);
fn splice_reexports_entries_beyond_the_parent_frames() {
let mut parent = FunctionEmitter::new_seeded(0);
let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 3, 3]);
assert_eq!(exits(&parent), [0, 1, 2]);
fn a_scratch_spliced_dead_contributes_nothing() {
let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 2, 3]);
assert_eq!(exits(&scratch), [0, 1, 2, 3]);
assert!(!closed(&mut parent));
fn nested_scratches_mark_the_block_after_both_splices() {
let mut arm = FunctionEmitter::new_seeded(2);
arm.block_start();
let clause = scratch_with_br_ifs(0, &[0, 0, 2, 3]);
assert_eq!(exits(&clause), [0, 2, 3]);
arm.splice(&clause).expect("balanced");
assert_eq!(exits(&arm), [1, 2], "entries 2 and 3 cross the arm's block");
arm.unreachable();
assert!(closed(&mut arm), "entry 0 marked the arm's own block");
parent.splice(&arm).expect("balanced");
assert_eq!(exits(&parent), [0], "entry 2 crosses the parent too");
assert!(closed(&mut parent), "entry 1 marked the outer block");
fn reexport_crosses_two_scratch_boundaries() {
let clause = scratch_with_br_ifs(0, &[0, 1, 2, 2]);
let mut arm = FunctionEmitter::new_seeded(1);
assert_eq!(exits(&arm), [0, 1, 2]);
assert_eq!(exits(&parent), [0, 1]);
assert!(closed(&mut parent), "entry 0 marked the parent's block");
/// A backward `(go tag)` from an `if`-arm scratch in a non-final TAGBODY
/// segment: the parent is the exit block, the dispatch loop and two still-open
/// tag blocks, so the scratch's exits reach the loop behind them.
fn splice_from_a_tagbody_segment_skips_only_the_dispatch_loop() {
assert_eq!(marked(&parent), [true, false, true, true]);
fn splice_marks_if_and_try_table_frames_but_not_a_loop() {
parent.try_table(EMPTY, &[]);
let scratch = scratch_with_br_ifs(0, &[0, 1, 2, 2, 3, 4]);
assert_eq!(marked(&parent), [true, true, false, true, true]);
fn a_branch_marks_only_the_frames_it_names() {
scratch.if_block(EMPTY);
scratch.try_table(EMPTY, &[]);
scratch.br_table(&[0, 2, 2], 4);
assert_eq!(marked(&scratch), [false, true, false, true]);
assert_eq!(exits(&scratch), [0]);