Lines
100 %
Functions
Branches
use crate::compiler::CompileMode;
use std::collections::HashSet;
use super::*;
use crate::ast::WasmType;
use crate::compiler::Compiler;
use crate::compiler::join::Arm;
use crate::reader::Reader;
use crate::runtime::HomeKey;
fn eval_type(source: &str) -> Result<Option<WasmType>> {
let program = Reader::parse(source).expect("parses");
let mut symbols = SymbolTable::with_builtins();
Compiler::new()
.compile_eval_with_type(&program, &mut symbols)
.map(|(_, ty)| ty)
}
#[test]
fn a_unit_whose_homes_are_right_compiles_once() {
let mut symbols = SymbolTable::new();
let mut attempts = 0;
let result = settle_homes(&mut symbols, |symbols| {
attempts += 1;
symbols.note_home(
"G",
vec![Arm::NumberLiteral(WasmType::I32)],
None,
WasmType::I32,
);
symbols.note_store(&HomeKey::global("G"), Arm::NumberLiteral(WasmType::I32));
Ok(attempts)
});
assert_eq!(result.expect("settles"), 1);
fn a_unit_that_never_settles_stops_at_the_cap() {
let result: Result<()> = settle_homes(&mut symbols, |symbols| {
symbols.set_unit_specials(HashSet::from(["G".to_string()]), HashSet::new());
symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
Ok(())
assert!(
matches!(result, Err(Error::Resource(msg)) if msg == "the type of global G did not settle")
assert_eq!(attempts, 4);
fn a_widening_is_seeded_into_the_next_attempt() {
let mut hints = Vec::new();
let hint = symbols.hint(&HomeKey::global("G"));
hints.push(hint);
let ty = hint.unwrap_or(WasmType::I32);
symbols.note_home("G", vec![Arm::NumberLiteral(WasmType::I32)], None, ty);
result.expect("settles");
assert_eq!(hints, [None, Some(WasmType::Ratio)]);
fn the_hints_do_not_outlive_the_unit() {
settle_homes(&mut symbols, |symbols| {
})
.expect_err("never settles");
assert_eq!(symbols.hint(&HomeKey::global("G")), None);
fn a_defun_accumulator_seeded_with_an_index_is_refused_beside_a_scalar() {
let err =
eval_type("(defvar total 0) (defun add (a) (setf total (+ total a))) (add 1/2) total")
.expect_err("a mix, CL 1/2");
err.to_string()
.contains("+ cannot mix an index (count) with a scalar"),
"{err}"
fn a_defun_accumulator_seeded_with_a_scalar_is_a_scalar_home() {
let ty =
eval_type("(defvar total 0/1) (defun add (a) (setf total (+ total a))) (add 1/2) total");
assert_eq!(ty.expect("compiles"), Some(WasmType::Ratio));
fn a_defun_cons_onto_nil_settles_to_a_list_home() {
let ty = eval_type("(defvar l nil) (defun add (v) (setf l (cons v l))) (add 1) l");
assert!(matches!(ty.expect("compiles"), Some(WasmType::PairRef(_))));
fn a_nil_after_a_number_settles_to_an_anyref_home() {
let ty = eval_type("(defvar g 0) (setf g nil) g");
assert_eq!(ty.expect("compiles"), Some(WasmType::AnyRef));
fn a_unit_with_an_error_of_its_own_reports_it() {
let err = eval_type("(defvar g 0) (setf g 1/2) (undefined-fn g)").expect_err("rejected");
assert!(!matches!(err, Error::Resource(_)));
fn the_measured_stats_come_from_the_final_attempt_and_the_work_adds_up() {
let stats = |source: &str| {
let mut compiler = Compiler::new();
compiler
.compile_eval_with_type(&program, &mut SymbolTable::with_builtins())
.expect("compiles");
compiler.stats()
};
let source = "(defun put (a) (setf total a)) (put 1/2) total";
let widened = stats(&format!("(defvar total 0) {source}"));
let direct = stats(&format!("(defvar total 1/2) {source}"));
assert_eq!((widened.unit_attempts, direct.unit_attempts), (2, 1));
assert_eq!(
(widened.folds, widened.probe_walks, widened.stored_globals),
(direct.folds, direct.probe_walks, direct.stored_globals)
assert!(widened.stack_compiles > direct.stack_compiles);
fn a_settled_unit_leaves_no_hints_behind() {
let ty = symbols.hint(&HomeKey::global("G")).unwrap_or(WasmType::I32);
.expect("settles");
assert_eq!(attempts, 2);
fn a_constant_list_init_is_built_at_the_element_of_its_widened_home() {
let ty = eval_type("(defvar acc (list 0)) (setf acc (cons 1/2 acc)) acc");
ty.expect("compiles"),
Some(WasmType::PairRef(crate::ast::PairElement::AnyRef))
fn the_cap_counts_the_stored_globals_a_setf_created() {
symbols.set_stored_globals(HashSet::from(["G".to_string()]));
assert_eq!(symbols.unit_home_count(), 1);
fn a_definition_with_a_runtime_init_compiles_again_as_a_cell() {
let program = Reader::parse("(defvar z (entity-count)) z").expect("parses");
let mut cells = Vec::new();
crate::compiler::expr::prepare_unit(symbols, &program.exprs, CompileMode::Script)?;
cells.push(symbols.forced_cells());
match cells.len() {
1 => symbols.ask_cell("Z", crate::runtime::Asked::Typed(WasmType::I32)),
_ => assert_eq!(symbols.hint(&HomeKey::global("Z")), Some(WasmType::I32)),
assert_eq!(cells.len(), 2);
assert!(cells[1].contains("Z"));
fn attempts_for(source: &str) -> u64 {
compiler.stats().unit_attempts
fn every_definition_with_a_runtime_init_asks_in_one_attempt() {
let risky = |i: usize| format!("(defvar v{i} (handler-case (error 'boom \"m\") (t (e) 2)))");
let independent: Vec<String> = (0..40).map(risky).collect();
let chained: Vec<String> = (1..40)
.map(|i| format!("(defvar v{i} (+ v{} 1))", i - 1))
.collect();
assert_eq!(attempts_for(&format!("{} 0", independent.join(" "))), 2);
attempts_for(&format!("{} {} 0", risky(0), chained.join(" "))),
2
fn a_runtime_defvar_is_typed_by_its_init() {
let ty = eval_type("(defvar z (handler-case 1 (t (e) 2))) z");
assert_eq!(ty.expect("compiles"), Some(WasmType::I32));
fn the_unsettled_home_is_the_one_that_fails_its_check_in_the_last_attempt() {
symbols.set_unit_specials(
HashSet::from(["A".to_string(), "Z".to_string()]),
HashSet::new(),
for name in ["A", "Z"] {
let ty = symbols
.hint(&HomeKey::global(name))
.unwrap_or(WasmType::I32);
symbols.note_home(name, vec![Arm::NumberLiteral(WasmType::I32)], None, ty);
symbols.note_store(&HomeKey::global("A"), Arm::NumberLiteral(WasmType::Ratio));
let z = symbols.hint(&HomeKey::global("Z")).unwrap_or(WasmType::I32);
let flip = if z == WasmType::I32 {
WasmType::Ratio
} else {
WasmType::I32
symbols.note_store(&HomeKey::global("Z"), Arm::Value(flip));
matches!(result, Err(Error::Resource(msg)) if msg == "the type of global Z did not settle")