Lines
100 %
Functions
93.33 %
Branches
//! The probe scope against the whole `SymbolTable` and `CompileContext`
//! state. The budget is compared on its own, since a discarded pass keeps it.
use super::{compiled_in_trial, recompile};
use crate::ast::{Expr, WasmType};
use crate::compiler::budget::MAX_ELAB_WORK;
use crate::compiler::context::CompileContext;
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::join::{Arm, Exit};
use crate::compiler::observations::{Leaf, Observations, ObservedBlock, StructuralType};
use crate::compiler::recompile::Trial;
use crate::error::{Error, Result};
use crate::reader::Reader;
use crate::runtime::{HomeKey, Symbol, SymbolKind, SymbolTable};
fn setup() -> (CompileContext, SymbolTable) {
let ctx = CompileContext::new_with_host_fns(&[]).expect("context");
let symbols = SymbolTable::with_builtins_for_wasm();
(ctx, symbols)
}
/// Everything but the budget.
fn state(ctx: &CompileContext, symbols: &SymbolTable) -> (String, String) {
let digest = symbols.state_digest();
let symbols: Vec<&str> = digest
.lines()
.filter(|line| !line.starts_with("charged:"))
.collect();
(ctx.state_digest(), symbols.join("\n"))
fn charged(symbols: &SymbolTable) -> u64 {
symbols.budget().charged()
fn pass_work(ctx: &mut CompileContext, symbols: &mut SymbolTable, name: &str) -> Result<()> {
ctx.alloc_local(WasmType::Ratio)?;
ctx.intern_closure_signature(&[WasmType::Ratio], WasmType::I32)?;
ctx.record_block_exit(
"OUTER",
Exit {
arm: Arm::Value(WasmType::Ratio),
emitted: WasmType::Ratio,
},
);
ctx.record_block_join(ctx.block_join_start(), ratio_block(name));
symbols.define(Symbol::new(name, SymbolKind::Variable).with_value(Expr::T));
symbols.charge_work(100)
fn ratio_block(name: &str) -> ObservedBlock {
ObservedBlock {
name: name.to_string(),
body_len: 1,
join: StructuralType {
arrows: vec![],
result: Leaf::Ratio,
fn observed() -> Observations {
Observations {
result: StructuralType {
blocks: Vec::new(),
#[test]
fn a_discarded_pass_restores_the_state_and_keeps_its_charge() {
let (mut ctx, mut symbols) = setup();
ctx.push_block_label("OUTER", 0, None);
let before = state(&ctx, &symbols);
let base = charged(&symbols);
let pass = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
pass_work(ctx, symbols, "PASS")?;
Ok(Trial::<(), _>::Discard(observed()))
})
.expect("pass");
assert!(matches!(pass, Trial::Discard(obs) if obs == observed()));
assert_eq!(state(&ctx, &symbols), before);
assert_eq!(charged(&symbols), base + 100);
fn an_error_inside_an_inner_pass_restores_the_outer_state_and_keeps_the_charge() {
recompile(&mut ctx, &mut symbols, |ctx, symbols| {
pass_work(ctx, symbols, "OUTER-PASS")?;
let outer = state(ctx, symbols);
let err = recompile(ctx, symbols, |ctx, symbols| {
pass_work(ctx, symbols, "INNER-PASS")?;
Err::<Trial<(), Observations>, _>(Error::Compile("inner".to_string()))
.expect_err("inner error");
assert!(err.to_string().contains("inner"));
assert_eq!(state(ctx, symbols), outer);
assert_eq!(charged(symbols), base + 200);
.expect("outer");
assert_eq!(charged(&symbols), base + 200);
/// A disagreeing LET inside a disagreeing LET: the outer binding's first
/// pass runs the inner one twice, the outer's second pass agrees.
fn a_nested_discard_restores_each_pre_pass_state_and_charges_every_pass() {
let mut marks = vec![charged(&symbols)];
pass_work(ctx, symbols, "OUTER-1")?;
marks.push(charged(symbols));
recompile(ctx, symbols, |ctx, symbols| {
pass_work(ctx, symbols, "INNER-1")?;
})?;
pass_work(ctx, symbols, "INNER-2")?;
Ok(Trial::<(), Observations>::Commit(()))
.expect("outer pass 1");
let second = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
pass_work(ctx, symbols, "OUTER-2")?;
.expect("outer pass 2");
marks.push(charged(&symbols));
assert!(matches!(second, Trial::Commit(())));
assert!(marks.windows(2).all(|pair| pair[0] < pair[1]), "{marks:?}");
assert!(symbols.lookup("OUTER-2").is_some());
assert!(symbols.lookup("INNER-2").is_none());
fn a_discarded_pass_puts_back_the_block_joins_it_consumed() {
let join = ratio_block("B").join;
ctx.expecting_block_joins(vec![ratio_block("A"), ratio_block("B")], |ctx| {
assert!(ctx.expected_block_join("A", 1).is_some());
recompile(ctx, &mut symbols, |ctx, _| {
assert!(ctx.expected_block_join("B", 1).is_some());
assert_eq!(ctx.expected_block_join("B", 1), Some(join));
assert_eq!(ctx.expected_block_join("B", 1), None);
});
fn symbols_with_home() -> SymbolTable {
let mut symbols = SymbolTable::with_builtins_for_wasm();
symbols.begin_home_attempt(std::collections::HashMap::new());
symbols.note_home(
"G",
vec![Arm::NumberLiteral(WasmType::I32)],
None,
WasmType::I32,
symbols
fn a_discarded_pass_forgets_the_stores_it_recorded() {
let mut ctx = CompileContext::new_with_host_fns(&[]).expect("context");
let mut symbols = symbols_with_home();
recompile(&mut ctx, &mut symbols, |_, symbols| {
symbols.note_store(&HomeKey::global("G"), Arm::Value(WasmType::Ratio));
assert_eq!(symbols.widened_homes(), None);
fn a_failed_pass_keeps_the_stores_it_recorded() {
Err::<Trial<(), Observations>, _>(Error::Compile("refused".to_string()))
.expect_err("refused");
assert!(symbols.widened_homes().is_some());
fn a_trial_out_of_budget_is_a_resource_error_not_a_form_that_does_not_compile() {
let emit = FunctionEmitter::new();
symbols.charge_work(MAX_ELAB_WORK).expect("within");
let form = Reader::parse_expr("(+ 1 2)").expect("parses");
let trial = compiled_in_trial(&mut ctx, &mut symbols, &emit, &form, |_, ty| Ok(ty));
assert!(matches!(trial, Err(Error::Resource(_))), "{trial:?}");
fn a_trial_that_does_not_compile_is_none() {
let form = Reader::parse_expr("(no-such-function 1)").expect("parses");
assert_eq!(trial.expect("no resource error"), None);