pub struct Env {
pub bindings: Rc<RefCell<HashMap<Spur, Value>>>,
pub parent: Option<Rc<Env>>,
pub version: Rc<Cell<u64>>,
}Expand description
A Sema environment: a chain of scopes with bindings.
Fields§
§bindings: Rc<RefCell<HashMap<Spur, Value>>>§parent: Option<Rc<Env>>§version: Rc<Cell<u64>>Monotonic mutation counter the VM’s inline global cache is keyed on.
Held behind an Rc so it travels with bindings: every Env handle
cloned from this one shares this same cell. A mutation through any handle
(a set! on a loaded unit’s per-form-VM clone, a different frame’s
home-globals handle) is therefore observed by a cache entry keyed on any
other handle to the same bindings — a fresh cell per clone would let a
recursive reader keep serving a value its own handle’s set! never bumped
(issue #82). A distinct bindings map (new/with_parent) still gets its
own cell, since it is a genuinely independent scope.
Implementations§
Source§impl Env
impl Env
pub fn new() -> Self
pub fn with_parent(parent: Rc<Env>) -> Self
Sourcepub fn bump_version(&self)
pub fn bump_version(&self)
Bump the version counter shared by every handle to this scope’s bindings. The VM’s inline global cache is keyed on it, so bumping after any mutation makes every VM observing this scope (through any clone) re-read instead of serving a stale cached value.
pub fn get(&self, name: Spur) -> Option<Value>
pub fn get_str(&self, name: &str) -> Option<Value>
pub fn set(&self, name: Spur, val: Value)
pub fn set_str(&self, name: &str, val: Value)
Sourcepub fn update(&self, name: Spur, val: Value)
pub fn update(&self, name: Spur, val: Value)
Update a binding that already exists in the current scope.
Sourcepub fn take(&self, name: Spur) -> Option<Value>
pub fn take(&self, name: Spur) -> Option<Value>
Remove and return a binding from the current scope only.
Sourcepub fn take_anywhere(&self, name: Spur) -> Option<Value>
pub fn take_anywhere(&self, name: Spur) -> Option<Value>
Remove and return a binding from any scope in the parent chain.
Sourcepub fn set_existing(&self, name: Spur, val: Value) -> bool
pub fn set_existing(&self, name: Spur, val: Value) -> bool
Set a variable in the scope where it’s defined (for set!).
Sourcepub fn all_names(&self) -> Vec<Spur>
pub fn all_names(&self) -> Vec<Spur>
Collect all bound variable names across all scopes (for suggestions).
Sourcepub fn iter_bindings(&self, f: impl FnMut(Spur, &Value))
pub fn iter_bindings(&self, f: impl FnMut(Spur, &Value))
Iterate over bindings in the current scope only (not parent scopes).
Sourcepub fn get_local(&self, name: Spur) -> Option<Value>
pub fn get_local(&self, name: Spur) -> Option<Value>
Get a binding from the current scope only (not parent scopes).
Sourcepub fn replace_bindings(
&self,
new_bindings: impl IntoIterator<Item = (Spur, Value)>,
)
pub fn replace_bindings( &self, new_bindings: impl IntoIterator<Item = (Spur, Value)>, )
Replace all bindings in the current scope with the given iterator. Used for bulk restore (e.g., undo/rollback).