Lines
100 %
Functions
Branches
//! Rollback is asserted on the whole `SymbolTable` and `CompileContext` state
//! but the budget, so a mutation to a field nobody journalled still fails
//! these tests.
use super::{Trial, recompile};
use crate::ast::{Expr, WasmType};
use crate::compiler::context::{CompileContext, TagFrame};
use crate::compiler::join::{Arm, Exit};
use crate::error::{Error, Result};
use crate::runtime::{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)
}
/// Touches every journalled piece of state, plus the budget.
fn mutate(ctx: &mut CompileContext, symbols: &mut SymbolTable) -> Result<()> {
ctx.alloc_local(WasmType::I32)?;
ctx.add_data(b"trial")?;
symbols.define(Symbol::new("TRIAL-ONLY", SymbolKind::Variable).with_value(Expr::T));
symbols.define(Symbol::new("+", SymbolKind::Variable).with_value(Expr::Nil));
symbols
.define_struct("TRIAL-STRUCT", vec!["A".to_string()])
.expect("TRIAL-STRUCT defines");
symbols.register_test("TRIAL-TEST", Expr::Nil);
symbols.mark_native_referenced("TRIAL-NATIVE");
symbols.charge_work(1000)
fn digests(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"))
#[test]
fn a_discarded_trial_restores_the_whole_state() {
let (mut ctx, mut symbols) = setup();
let before = digests(&ctx, &symbols);
let trial = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
mutate(ctx, symbols)?;
assert_ne!(digests(ctx, symbols), before);
Ok(Trial::<(), ()>::Discard(()))
})
.expect("trial");
assert!(matches!(trial, Trial::Discard(())));
assert_eq!(digests(&ctx, &symbols), before);
fn a_failed_trial_restores_the_whole_state_and_reports_its_error() {
let err = recompile(&mut ctx, &mut symbols, |ctx, symbols| {
Err::<Trial<(), ()>, _>(Error::Compile("trial failed".to_string()))
.expect_err("error");
assert!(err.to_string().contains("trial failed"));
fn a_committed_trial_keeps_its_changes() {
Ok(Trial::<u32, ()>::Commit(7))
assert!(matches!(trial, Trial::Commit(7)));
assert_ne!(digests(&ctx, &symbols), before);
assert!(symbols.lookup("TRIAL-ONLY").is_some());
fn an_inner_commit_is_undone_by_an_outer_discard() {
recompile(&mut ctx, &mut symbols, |ctx, symbols| {
recompile(ctx, symbols, |ctx, symbols| {
Ok(Trial::<(), ()>::Commit(()))
})?;
.expect("nested");
fn an_inner_discard_leaves_the_outer_trials_changes() {
symbols.define(Symbol::new("OUTER", SymbolKind::Variable).with_value(Expr::T));
let after_outer = digests(ctx, symbols);
assert_eq!(digests(ctx, symbols), after_outer);
assert!(symbols.lookup("OUTER").is_some());
assert!(symbols.lookup("TRIAL-ONLY").is_none());
/// Records an exit into the enclosing `OUTER` block and leaves one frame of
/// each kind pushed, as a trial abandoned mid-body would.
fn mutate_frames(ctx: &mut CompileContext, exit: WasmType, depth: u32) {
ctx.record_block_exit(
"OUTER",
Exit {
arm: Arm::Value(exit),
emitted: exit,
},
);
ctx.push_block_label("TRIAL-BLOCK", depth, None);
ctx.push_unwind_frame(depth, Vec::new());
ctx.push_tag_frame(TagFrame {
tags: Vec::new(),
pc_local: depth,
loop_depth: depth,
});
fn outer_exits(ctx: &CompileContext) -> Vec<Arm> {
ctx.lookup_block_label("OUTER")
.map(|label| label.recorded_exits.iter().map(|exit| exit.arm).collect())
.unwrap_or_default()
fn a_nested_discard_restores_the_frames() {
ctx.push_block_label("OUTER", 0, None);
mutate_frames(ctx, WasmType::I32, 1);
recompile(ctx, symbols, |ctx, _| {
mutate_frames(ctx, WasmType::Ratio, 2);
assert_eq!(outer_exits(ctx), vec![Arm::Value(WasmType::I32)]);
assert!(outer_exits(&ctx).is_empty());