Lines
98.8 %
Functions
100 %
Branches
//! The cell registry of a unit: the globals made cells at its start, what its
//! definitions did to them, and the closures and seeds the next attempt carries.
use std::collections::{BTreeSet, HashMap};
use super::{Asked, Cell, CellBinding, ClosureShape, Seed, locked};
use crate::ast::{Expr, WasmType};
use crate::runtime::SymbolTable;
impl SymbolTable {
/// Records the cell `name` the unit made at its start; `flag` is its
/// bound flag and `seed` what it starts with.
pub(crate) fn note_cell(&self, name: &str, flag: u32, seed: Seed) {
locked(&self.homes).cells.insert(
name.to_string(),
Cell {
flag,
seed,
defined: CellBinding::Unbound,
},
);
}
/// Whether the cell `name` starts with an earlier unit's unreadable value.
#[must_use]
pub(crate) fn is_lost_cell(&self, name: &str) -> bool {
locked(&self.homes)
.cells
.get(name)
.is_some_and(|cell| cell.seed == Seed::Lost)
/// Whether the cell `name` is bound once the definitions compiled so far
/// ran: a seeded one always is.
pub(crate) fn cell_binding(&self, name: &str) -> CellBinding {
match locked(&self.homes).cells.get(name) {
Some(cell) if cell.seed != Seed::Unbound => CellBinding::Bound,
Some(cell) => cell.defined,
None => CellBinding::Unbound,
/// Records a DEFVAR / DEFPARAMETER of the cell `name`; one that `surely`
/// runs binds it.
pub(crate) fn note_cell_definition(&self, name: &str, surely: bool) {
let binding = match surely {
true => CellBinding::Bound,
false => CellBinding::Maybe,
};
if let Some(cell) = locked(&self.homes).cells.get_mut(name) {
cell.defined = cell.defined.max(binding);
/// Whether `name` is a cell of this unit.
pub(crate) fn is_cell(&self, name: &str) -> bool {
locked(&self.homes).cells.contains_key(name)
/// The global holding the cell `name`, once it is registered as the
/// symbol's value.
pub(crate) fn cell_global(&self, name: &str) -> Option<(u32, WasmType)> {
match self.lookup(name)?.value()? {
Expr::WasmGlobal(idx, ty) if self.is_cell(name) => Some((*idx, *ty)),
_ => None,
/// The bound flag of the cell `name`.
pub(crate) fn cell_flag(&self, name: &str) -> Option<u32> {
locked(&self.homes).cells.get(name).map(|cell| cell.flag)
/// Asks the next attempt to make `name` a cell: its one definition's init
/// runs at run time, where an exit may skip it. `asked` types the cell.
pub(crate) fn ask_cell(&self, name: &str, asked: Asked) {
locked(&self.homes).asked.insert(name.to_string(), asked);
/// The cells the next attempt makes for definitions with a run-time init.
pub(crate) fn forced_cells(&self) -> BTreeSet<String> {
let book = locked(&self.homes);
book.forced
.iter()
.chain(book.asked.keys())
.cloned()
.collect()
/// Carries the cells earlier attempts asked for into this one.
pub(crate) fn force_cells(&self, names: BTreeSet<String>) {
locked(&self.homes).forced = names;
/// Asks the next attempt to start the cell `name` without the closure an
/// earlier unit carried: a store of the unit has another signature.
pub(crate) fn refuse_seed(&self, name: &str) {
let mut book = locked(&self.homes);
if book
.is_some_and(|cell| cell.seed == Seed::Closure)
{
book.refused.insert(name.to_string());
/// Whether this attempt starts the cell `name` without the carried closure.
pub(crate) fn is_seed_refused(&self, name: &str) -> bool {
locked(&self.homes).unseeded.contains(name)
/// The cells the next attempt starts without a carried closure.
pub(crate) fn refused_seeds(&self) -> BTreeSet<String> {
book.unseeded.union(&book.refused).cloned().collect()
/// Carries the refused closure seeds of earlier attempts into this one.
pub(crate) fn unseed_cells(&self, names: BTreeSet<String>) {
locked(&self.homes).unseeded = names;
/// The closure the previous attempt asked `name`'s cell to hold.
pub(crate) fn closure_hint(&self, name: &str) -> Option<ClosureShape> {
locked(&self.homes).closure_hints.get(name).cloned()
/// The closures the next attempt allocates cells for.
pub(crate) fn closure_shapes(&self) -> HashMap<String, ClosureShape> {
locked(&self.homes).closure_shapes()
/// Carries the closures of the previous attempt into this one.
pub(crate) fn hint_closures(&self, shapes: HashMap<String, ClosureShape>) {
locked(&self.homes).closure_hints = shapes.into_iter().collect();