Lines
100 %
Functions
Branches
use std::collections::{HashMap, HashSet};
use super::*;
use crate::ast::{ClosureSigId, Number, PairElement};
use crate::error::Error;
#[path = "homes_binder_tests.rs"]
mod binder_tests;
#[path = "homes_field_tests.rs"]
mod field_tests;
fn table_with_home(name: &str, base: &[Arm], datum: Option<Expr>, ty: WasmType) -> SymbolTable {
let mut symbols = SymbolTable::new();
symbols.begin_home_attempt(HashMap::new());
symbols.note_home(name, base.to_vec(), datum, ty);
symbols
}
fn int(n: i64) -> Expr {
Expr::Number(Number::Index(n))
#[test]
fn a_home_that_is_the_join_of_its_stores_is_not_widened() {
let symbols = table_with_home(
"G",
&[Arm::NumberLiteral(WasmType::I32)],
None,
WasmType::I32,
);
symbols.note_store(&HomeKey::global("G"), Arm::NumberLiteral(WasmType::I32));
assert_eq!(symbols.widened_homes(), None);
fn a_store_wider_than_the_home_widens_it() {
symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
assert_eq!(
symbols.widened_homes(),
Some(HashMap::from([(
HomeKey::Global("G".to_string()),
WasmType::Ratio
)]))
fn a_nil_init_joins_a_list_store_to_the_list() {
let symbols = table_with_home("L", &[Arm::Nil], Some(Expr::Nil), WasmType::Bool);
symbols.note_store(
&HomeKey::global("L"),
Arm::Value(WasmType::PairRef(PairElement::I32)),
HomeKey::Global("L".to_string()),
WasmType::PairRef(PairElement::I32)
fn a_store_to_a_home_of_another_unit_records_nothing() {
symbols.note_store(&HomeKey::global("OTHER"), Arm::Value(WasmType::Ratio));
fn a_closure_store_widens_its_home_to_any_so_no_signature_is_carried() {
let sig = ClosureSigId(0);
let cases = [
(WasmType::Closure(sig), WasmType::AnyRef),
(
WasmType::PairRef(PairElement::Closure(sig)),
WasmType::PairRef(PairElement::AnyRef),
),
];
for (stored, widened) in cases {
let symbols = table_with_home("F", &[Arm::Nil], None, WasmType::Bool);
symbols.note_store(&HomeKey::global("F"), Arm::Value(stored));
Some(HashMap::from([(HomeKey::Global("F".to_string()), widened)]))
fn a_closure_home_is_not_recorded() {
"F",
&[Arm::Value(WasmType::Closure(ClosureSigId(0)))],
WasmType::Closure(ClosureSigId(0)),
symbols.note_store(&HomeKey::global("F"), Arm::Value(WasmType::Ratio));
fn the_hint_is_the_one_the_attempt_began_with() {
symbols.begin_home_attempt(HashMap::from([(
WasmType::Ratio,
)]));
assert_eq!(symbols.hint(&HomeKey::global("G")), Some(WasmType::Ratio));
symbols.end_home_attempt();
assert_eq!(symbols.hint(&HomeKey::global("G")), None);
fn a_clone_shares_the_book_of_its_table() {
.clone()
.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
assert!(symbols.widened_homes().is_some());
fn a_rolled_back_trial_forgets_its_stores() {
let mut symbols = table_with_home(
let journal = symbols.journal();
symbols.restore_journal(journal);
fn a_probe_at_init_runs_no_trial_for_any_other_stored_cell() {
let mut symbols = table_with_home("G", &[Arm::Nil], Some(int(1)), WasmType::I32);
symbols.note_home("H", vec![Arm::Nil], Some(int(2)), WasmType::I32);
for name in ["G", "H"] {
symbols.define(crate::runtime::Symbol::new(
name,
crate::runtime::SymbolKind::Variable,
));
symbols.set_stored_globals(HashSet::from(["G".to_string(), "H".to_string()]));
let probe = symbols
.probe_at_init(&HomeKey::global("G"), "G")
.expect("probe");
assert!(probe.probe_at_init(&HomeKey::global("H"), "H").is_none());
fn a_probe_at_init_reads_the_constant_the_home_started_with() {
Some(int(1)),
assert_eq!(probe.lookup("G").and_then(|s| s.value()), Some(&int(1)));
assert!(probe.probe_at_init(&HomeKey::global("G"), "G").is_none());
assert!(
table_with_home("H", &[Arm::Nil], None, WasmType::Bool)
.probe_at_init(&HomeKey::global("H"), "H")
.is_none()
fn table_with_global_g() -> SymbolTable {
symbols.define(
crate::runtime::Symbol::new("G", crate::runtime::SymbolKind::Variable)
.with_value(Expr::WasmGlobal(0, WasmType::I32)),
crate::runtime::Symbol::new("K", crate::runtime::SymbolKind::Variable).with_value(int(1)),
fn expiring_the_homes_drops_only_the_wasm_globals() {
let mut symbols = table_with_global_g();
symbols.expire_homes();
assert!(symbols.lookup("G").is_some_and(|s| s.value().is_none()));
assert_eq!(symbols.lookup("K").and_then(|s| s.value()), Some(&int(1)));
fn a_read_of_an_expired_global_says_its_value_did_not_survive() {
for err in [
Error::UndefinedSymbol("G".to_string()),
Error::Compile("symbol 'G' has no value".to_string()),
] {
symbols.explain_expired(err).to_string(),
"Compile error: G: its runtime value does not survive the form"
for tail in ["", " for stack value"] {
symbols.not_callable("G", tail).to_string(),
symbols.not_callable("K", " for stack value").to_string(),
"Compile error: symbol 'K' is not callable for stack value"
let other = Error::Compile("symbol 'K' has no value".to_string());
symbols.explain_expired(other).to_string(),
"Compile error: symbol 'K' has no value"
fn a_call_of_a_closure_variable_says_undefined_function() {
crate::runtime::Symbol::new("F", crate::runtime::SymbolKind::Variable).with_value(
Expr::WasmLocal(0, WasmType::Closure(crate::ast::ClosureSigId(0))),
symbols.not_callable("F", "").to_string(),
"Compile error: undefined function F; use (funcall f …)"
fn table_with_provisional_g() -> SymbolTable {
symbols.note_provisional_home("G");
fn a_provisional_home_no_store_typed_stays_as_it_is() {
let symbols = table_with_provisional_g();
symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::AnyRef));
fn a_provisional_home_widens_to_the_join_of_its_stores() {
fn a_hint_the_attempt_began_with_outlives_a_home_without_stores() {
HomeKey::Global("H".to_string()),
symbols.note_home(
vec![Arm::NumberLiteral(WasmType::I32)],
Some(HashMap::from([
(HomeKey::Global("G".to_string()), WasmType::Ratio),
(HomeKey::Global("H".to_string()), WasmType::Ratio)
]))
fn a_home_that_holds_its_stores_leaves_no_hint() {
let symbols = table_with_home("G", &[Arm::Nil], Some(Expr::Nil), WasmType::Bool);
symbols.note_store(&HomeKey::global("G"), Arm::Nil);
fn a_closure_in_a_provisional_home_asks_for_a_cell_of_its_shape() {
let shape = ClosureShape::of(&[WasmType::Ratio], WasmType::I32).expect("flat");
symbols.note_closure_shape("G", shape.clone());
symbols.note_closure_shape("G", ClosureShape::of(&[], WasmType::Ratio).expect("flat"));
assert_eq!(symbols.widened_homes(), Some(HashMap::new()));
symbols.closure_shapes(),
HashMap::from([("G".to_string(), shape.clone())])
let mut next = SymbolTable::new();
next.begin_home_attempt(HashMap::new());
next.hint_closures(symbols.closure_shapes());
assert_eq!(next.closure_hint("G"), Some(shape));
assert_eq!(next.closure_hint("H"), None);
fn a_closure_shape_naming_another_closure_is_not_carried() {
let nested = [WasmType::Closure(ClosureSigId(0))];
assert_eq!(ClosureShape::of(&nested, WasmType::I32), None);
assert_eq!(ClosureShape::of(&[], nested[0]), None);
assert!(ClosureShape::of(&[WasmType::I32], WasmType::I32).is_some());
fn a_closure_is_noted_only_in_a_provisional_home() {
let symbols = table_with_home("G", &[Arm::Nil], None, WasmType::Bool);
symbols.note_closure_shape("G", ClosureShape::of(&[], WasmType::I32).expect("flat"));
assert_eq!(symbols.closure_shapes(), HashMap::new());
fn the_first_constant_stored_is_the_init_a_store_is_typed_against() {
symbols.note_home_datum("G", &int(1));
symbols.note_home_datum("G", &int(2));
let mut table = symbols.clone();
table.define(crate::runtime::Symbol::new(
let probe = table
fn a_cell_is_registered_with_its_flag_and_whether_it_started_bound() {
symbols.note_cell("NEW", 3, Seed::Unbound);
symbols.note_cell("OLD", 5, Seed::Constant);
symbols.note_cell("LOST", 6, Seed::Lost);
assert!(symbols.is_cell("NEW") && symbols.is_cell("OLD") && !symbols.is_cell("X"));
assert_eq!(symbols.cell_flag("NEW"), Some(3));
assert_eq!(symbols.cell_flag("X"), None);
assert_eq!(symbols.cell_binding("NEW"), CellBinding::Unbound);
assert_eq!(symbols.cell_binding("OLD"), CellBinding::Bound);
assert_eq!(symbols.cell_binding("LOST"), CellBinding::Bound);
assert!(symbols.is_lost_cell("LOST") && !symbols.is_lost_cell("OLD"));
fn a_cell_global_is_a_registered_cell_whose_value_is_a_global() {
let symbols = table_with_global_g();
assert_eq!(symbols.cell_global("G"), None);
symbols.note_cell("G", 1, Seed::Unbound);
symbols.note_cell("K", 2, Seed::Unbound);
assert_eq!(symbols.cell_global("G"), Some((0, WasmType::I32)));
assert_eq!(symbols.cell_global("K"), None);
fn a_sure_definition_of_a_cell_outranks_a_skippable_one() {
symbols.note_cell("G", 0, Seed::Unbound);
assert_eq!(symbols.cell_binding("G"), CellBinding::Unbound);
symbols.note_cell_definition("G", false);
assert_eq!(symbols.cell_binding("G"), CellBinding::Maybe);
symbols.note_cell_definition("G", true);
assert_eq!(symbols.cell_binding("G"), CellBinding::Bound);
fn a_seeded_cell_is_bound() {
symbols.note_cell("G", 0, Seed::Constant);
assert_eq!(symbols.cell_binding("H"), CellBinding::Unbound);
fn a_cell_asked_for_widens_the_attempt_with_its_type() {
symbols.ask_cell("Z", Asked::Typed(WasmType::I32));
symbols.ask_cell("W", Asked::Untyped);
let shape = ClosureShape::of(&[WasmType::I32], WasmType::I32).expect("flat");
symbols.ask_cell("F", Asked::Closure(shape.clone()));
HashMap::from([("F".to_string(), shape)])
HomeKey::Global("Z".to_string()),
WasmType::I32
let forced = symbols.forced_cells();
forced,
BTreeSet::from(["F".to_string(), "W".to_string(), "Z".to_string()])
symbols.force_cells(forced.clone());
assert_eq!(symbols.forced_cells(), forced);
fn a_refused_closure_seed_widens_the_attempt_and_the_next_one_starts_without_it() {
symbols.note_cell("K", 0, Seed::Closure);
symbols.note_cell("N", 1, Seed::Constant);
symbols.refuse_seed("N");
symbols.refuse_seed("K");
let refused = symbols.refused_seeds();
assert_eq!(refused, BTreeSet::from(["K".to_string()]));
symbols.unseed_cells(refused);
assert!(symbols.is_seed_refused("K"));
assert!(!symbols.is_seed_refused("N"));