Lines
99 %
Functions
94.74 %
Branches
100 %
//! What a unit wrote and whether each write ran (plan 0014 PERSIST): every
//! definition form of a slot has an exported i32 flag that is 1 once it ran.
//! A host keeps the last definition whose flag is 1, or puts the slot back
//! from the snapshot taken before the unit.
use crate::ast::{Expr, StructId, WasmType};
use super::closures::ClosureSource;
use super::entry::Symbol;
use super::rebind::Rebind;
use super::table::SymbolTable;
/// Which slot of a name a definition fills: a variable's value, the function
/// (or macro) it names, or the field layout of the structure it names.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub enum Slot {
Value,
Function,
Layout,
}
/// Everything a definer writes under one name: its symbol, the field layout
/// of the structure of that name, and whether it was declared special or
/// created by a `SETF`.
#[derive(Clone, Debug, Default)]
pub(crate) struct Written {
symbol: Option<Symbol>,
layout: Option<StructId>,
pub(super) special: bool,
pub(super) created: bool,
impl Written {
pub(super) fn holds_value(&self) -> bool {
self.symbol
.as_ref()
.is_some_and(|symbol| symbol.value().is_some())
/// One definition form of a slot in the unit being compiled: the flag it sets
/// once it ran, and what it wrote.
#[derive(Clone, Debug)]
pub(super) struct Version {
flag: u32,
written: Written,
/// A slot the finished unit wrote: `flags` are the exported i32 globals, one
/// per definition form in text order, each 1 once that form ran, `cell` the
/// global a value slot holds its value in at run time, `rebind` the globals
/// that say which value it holds outside a binding when a binder rebinds it.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnitSlot {
pub name: String,
pub slot: Slot,
pub flags: Vec<u32>,
pub cell: Option<(u32, WasmType)>,
pub rebind: Option<Rebind>,
impl SymbolTable {
/// Records the symbols the unit is about to write as they stand before
/// it, so a slot the unit never wrote can be put back.
pub(crate) fn snapshot_pre_unit(&self, names: impl IntoIterator<Item = String>) {
let mut book = super::homes::locked(&self.homes);
for name in names {
let written = self.written(&name);
book.pre_unit.entry(name).or_insert(written);
fn written(&self, name: &str) -> Written {
let mut symbol = self.symbols.get(name).cloned();
if let (Some(live), Some(restored)) = (symbol.as_mut(), self.restored_function(name))
&& live.is_local_function()
{
live.function.clone_from(restored);
Written {
symbol,
layout: self.structs.id(name),
special: self.special_names.contains(name),
created: self.created_globals.contains(name),
/// Records the flag global that tells whether the definition form of
/// `name` in `slot` that just compiled has run, with what it wrote.
pub(crate) fn note_definition(&self, name: &str, slot: Slot, flag: u32) {
let written = self.written(name);
super::homes::locked(&self.homes)
.definitions
.entry((name.to_string(), slot))
.or_default()
.push(Version { flag, written });
/// Writes the manifest of the unit that just compiled, and keeps the
/// pre-unit snapshot its slots are put back from. Called once, after the
/// unit's module validates, with the source of the closures it created.
pub(crate) fn finish_unit(&mut self, closures: Vec<ClosureSource>) {
let book = super::homes::locked(&self.homes);
let slots = book
.iter()
.map(|((name, slot), versions)| {
let cell = self.cell_of(name, *slot);
UnitSlot {
name: name.clone(),
slot: *slot,
flags: versions.iter().map(|version| version.flag).collect(),
cell,
rebind: cell.and_then(|(global, _)| book.rebinds.get(&global).copied()),
})
.collect();
let pre_unit = book.pre_unit.clone();
let versions = book.definitions.clone();
drop(book);
self.unit_slots = slots;
self.unit_closures = closures;
self.forget_runtime_lexicals();
self.unit_pre = pre_unit;
self.unit_versions = versions;
fn cell_of(&self, name: &str, slot: Slot) -> Option<(u32, WasmType)> {
match (slot, self.symbols.get(name)?.value()?) {
(Slot::Value, Expr::WasmGlobal(idx, ty)) => Some((*idx, *ty)),
_ => None,
/// Takes the manifest of the unit that just compiled; a host that never
/// takes it leaves it to expire when the next unit starts.
pub fn take_unit_slots(&mut self) -> Vec<UnitSlot> {
std::mem::take(&mut self.unit_slots)
/// Settles `slot` of `name` once the unit ended: definition form `ran`
/// of the slot, the last whose flag was 1, is kept, or for `None` the
/// slot is put back as it stood before the unit. The other slots stay as
/// the unit made them.
pub fn settle(&mut self, name: &str, slot: Slot, ran: Option<usize>) {
let kept = ran.and_then(|at| self.unit_versions.get(&(name.to_string(), slot))?.get(at));
let unrun = kept.is_none();
let Written { symbol, layout, .. } = match kept {
Some(version) => version.written.clone(),
None => self.unit_pre.get(name).cloned().unwrap_or_default(),
};
match slot {
Slot::Layout => self.put_back_layout(name, layout),
Slot::Function => {
self.put_back_symbol(name, slot, symbol);
if let Some(function) = self.symbols.get_mut(name) {
function.forget_runtime_lexicals();
Slot::Value => {
if unrun {
self.restore_declaration(name, false);
fn put_back_symbol(&mut self, name: &str, slot: Slot, previous: Option<Symbol>) {
let mut restored = self
.symbols
.remove(name)
.unwrap_or_else(|| Symbol::bare(name));
Slot::Function => restored.function = previous.and_then(|symbol| symbol.function),
_ => restored.value = previous.and_then(|symbol| symbol.value),
self.reinstate(name, (!restored.is_bare()).then_some(restored));
fn put_back_layout(&mut self, name: &str, layout: Option<StructId>) {
match layout {
Some(id) => self.structs.restore(name, id),
None => self.structs.forget(name),
fn reinstate(&mut self, name: &str, restored: Option<Symbol>) {
match restored {
Some(symbol) => {
self.symbols.insert(name.to_string(), symbol);
None => {
self.special_names.remove(name);
self.created_globals.remove(name);
#[cfg(test)]
#[path = "definitions_tests.rs"]
mod tests;