Lines
100 %
Functions
Branches
//! A journalled scope either commits everything it did or rolls all of it
//! back; its work stays charged either way.
//!
//! The journal is the `SymbolTable` state a trial can change plus the
//! `CompileContext` state `ContextSnapshot` covers — one rollback path, not a
//! second journal beside it. A trial lowers into a scratch emitter and may
//! add data segments, locals and pending helpers to the context; the snapshot
//! covers all of them, and the scratch is only spliced into the real function
//! on commit. Nested trials are LIFO by construction; an inner commit stays
//! covered by the outer journal.
//! An error keeps the stores recorded into the unit's global homes: the
//! unit settles them before it compiles again (plan 0014 stage GJ).
//! The probe scope (ADR-0034): a pass compiled only to classify. Discarding
//! it restores everything but the budget — its work stands — and hands the
//! next pass only its
//! [`Observations`](crate::compiler::observations::Observations).
use crate::ast::{Expr, WasmType};
use crate::compiler::context::{CompileContext, ContextSnapshot};
use crate::compiler::emit::FunctionEmitter;
use crate::compiler::expr::compile_for_stack;
use crate::error::{Error, Result};
use crate::runtime::{SymbolJournal, SymbolTable};
/// How a trial ends when it does not fail.
#[derive(Debug)]
pub(crate) enum Trial<C, D> {
Commit(C),
Discard(D),
}
struct Journal {
context: ContextSnapshot,
symbols: SymbolJournal,
impl Journal {
fn open(ctx: &CompileContext, symbols: &SymbolTable) -> Self {
Self {
context: ctx.snapshot(),
symbols: symbols.journal(),
fn roll_back(self, ctx: &mut CompileContext, symbols: &mut SymbolTable) {
ctx.restore(self.context);
symbols.restore_journal(self.symbols);
/// Runs one pass in a scope; see the module docs. A discard or an error
/// restores the journalled state, but not the budget.
pub(crate) fn recompile<C, D>(
ctx: &mut CompileContext,
symbols: &mut SymbolTable,
pass: impl FnOnce(&mut CompileContext, &mut SymbolTable) -> Result<Trial<C, D>>,
) -> Result<Trial<C, D>> {
let journal = Journal::open(ctx, symbols);
match pass(ctx, symbols) {
Ok(Trial::Commit(committed)) => Ok(Trial::Commit(committed)),
Ok(Trial::Discard(summary)) => {
journal.roll_back(ctx, symbols);
Ok(Trial::Discard(summary))
Err(err) => {
let homes = symbols.home_book();
symbols.restore_home_book(homes);
Err(err)
/// What `form` pushes compiled on a scratch emitter seeded from `emit`,
/// `None` for one that never falls through, handed to `read` before the trial
/// rolls back; `None` too when the form does not compile, but a resource
/// ceiling is an error, not a form that does not compile.
pub(crate) fn compiled_in_trial<T>(
emit: &FunctionEmitter,
form: &Expr,
read: impl FnOnce(&CompileContext, Option<WasmType>) -> Result<T>,
) -> Result<Option<T>> {
let trial = recompile(ctx, symbols, |ctx, symbols| {
let mut scratch = FunctionEmitter::new_seeded(emit.block_depth());
let ty = compile_for_stack(ctx, &mut scratch, symbols, form)?;
read(ctx, scratch.is_reachable().then_some(ty)).map(Trial::<(), _>::Discard)
});
match trial {
Ok(Trial::Discard(value)) => Ok(Some(value)),
Err(err @ Error::Resource(_)) => Err(err),
_ => Ok(None),
#[cfg(test)]
#[path = "recompile_tests.rs"]
mod tests;
#[path = "rollback_tests.rs"]
mod rollback_tests;