Skip to main content

Scope

Struct Scope 

Source
pub struct Scope {
    pub parent: Option<Arc<Scope>>,
    pub path_node: Option<Arc<str>>,
    pub locals: Locals,
    pub current_dir: Arc<str>,
    pub cache_namespace: Arc<str>,
    pub root_ref: Option<RootRef>,
    pub list_context: Option<Arc<ListContext>>,
    pub thunks: Thunks,
}
Expand description

Single environment frame. Cheap to derive (every field is either Clone or Arc-shared) and wrapped in Arc<Scope> at every call site so backtracking through parent stays copy-free.

locals and thunks are kept as Mutex<HashMap> rather than raw HashMap so multiple evaluator threads can share an embedder scope (e.g. the empty root scope) without external locking. The inner HashMap is constructed empty (HashMap::new() does not allocate until first insert), so hot-loop children that never register a binding pay no heap cost for these fields beyond the inline mutex word and the Scope’s own Arc allocation.

Fields§

§parent: Option<Arc<Scope>>

Enclosing scope. None only at the document root.

§path_node: Option<Arc<str>>

Most-recent path segment opened by Scope::with_path / Scope::with_list_context; &sibling / &uncle peel these off when rebuilding the relative target path.

§locals: Locals

Bindings introduced inside this frame (closure params, comprehension loop vars, where clauses, imported aliases).

§current_dir: Arc<str>

Working directory used when resolving relative #import paths. Wrapped in Arc<str> so child-scope construction (which clones the field on every list / dict / closure descent) is a refcount bump rather than a heap String alloc.

§cache_namespace: Arc<str>

Stable namespace for the path cache; usually the canonical id of the surrounding module so different modules can’t collide on identical paths. Same Arc<str> reasoning as current_dir.

§root_ref: Option<RootRef>

&root anchor. None only at scopes that haven’t yet acquired one (typically just the pre-eval root scope before the evaluator’s eval_root stamps it). See RootRef for invariants.

§list_context: Option<Arc<ListContext>>

Active list iteration, if any.

§thunks: Thunks

Lazily-resolved bindings for the dict that owns this scope. Backends register and force entries through the public helpers below (Scope::thunks_for_write / Scope::get_thunk).

Implementations§

Source§

impl Scope

Source

pub fn get_local(&self, name: &str) -> Option<Value>

Look up name in this scope’s locals, walking up parent chain.

Hot path optimization: before the HashMap lookup, peek at list_context.iter_binding. Comprehension hot loops park the active for x in xs binding there (no HashMap allocation per element), so x resolves with a single Mutex peek instead of going through locals.

Source

pub fn current_iter_binding(&self) -> Option<(Arc<str>, Value)>

Snapshot the active iter binding, if any, into a (name, value) pair. Closure construction calls this so the captured environment holds a value, not a pointer into a mutable iter slot that a later iteration would clobber.

Source

pub fn get_thunk(&self, name: &str) -> Option<Arc<Thunk>>

Look up a lazy thunk in this scope’s own table, walking up the parent chain on miss.

Source

pub fn get_own_thunk(&self, name: &str) -> Option<Arc<Thunk>>

Look up a lazy thunk in this scope’s own table only (no parent chain). Used by the evaluator when validating that a key belongs to the immediate dict frame.

Source

pub fn locals_for_write(&self) -> MutexGuard<'_, HashMap<Arc<str>, Value>>

Acquire a write lock on this scope’s locals.

Centralizes every write-side scope.locals.lock().unwrap() so the locking strategy can evolve (e.g. swap to a lock-free or thread-local representation under a feature) without touching every call site. Read paths still go through Scope::get_local which walks the parent chain.

Keys are Arc<str>; callers writing a String should hand it over via Arc::<str>::from(name) so the buffer moves into the Arc allocation once, instead of paying a String::clone on every rebind.

Source

pub fn thunks_for_write(&self) -> MutexGuard<'_, HashMap<Arc<str>, Arc<Thunk>>>

Same as Scope::locals_for_write but for the dict thunks table.

Source

pub fn full_path(&self) -> Vec<String>

Reconstruct the path from the document root to the current scope by walking parent pointers and collecting path_node segments.

Source

pub fn path_cache_key(&self, path: &[String]) -> String

Build a stable cache key for path under this scope’s namespace.

Source

pub fn child(self: &Arc<Self>) -> Arc<Self>

Open a fresh child frame inheriting flow-state fields (current_dir, cache_namespace, root_ref, list_context) from self but with empty locals/thunks and no path_node.

This is the workhorse for every new lexical block — Dict body, comprehension iteration, closure body. The with_* methods below all build on top of it and only differ by which field they override. The empty Mutex<HashMap> pair doesn’t touch the heap until somebody actually inserts a binding (zero-capacity HashMap + inline mutex), so the comprehension hot loop only pays for the Scope’s own Arc allocation.

Source

pub fn with_local( self: &Arc<Self>, name: impl Into<Arc<str>>, val: Value, ) -> Arc<Self>

Bind a single name -> val in a fresh child frame.

Accepts anything that can be cheaply turned into an Arc<str> (a String, a &str, or an already-shared Arc<str>) so hot paths can hand the key over without an extra clone. Callers that already hold an Arc<str> should pass Arc::clone(&id) to skip the heap copy entirely.

Source

pub fn with_locals( self: &Arc<Self>, new_locals: HashMap<Arc<str>, Value>, ) -> Arc<Self>

Source

pub fn with_path(self: &Arc<Self>, node: impl Into<Arc<str>>) -> Arc<Self>

Source

pub fn with_list_context( self: &Arc<Self>, index: usize, elements: Arc<[Arc<Thunk>]>, ) -> Arc<Self>

Source

pub fn with_iter_loop( self: &Arc<Self>, elements: Arc<[Arc<Thunk>]>, ) -> Arc<Self>

Open the outer frame for a comprehension’s hot loop.

Unlike Scope::with_list_context, this builds the frame once per comprehension. The caller then drives the loop with Scope::set_iter_binding which updates the binding + index in place — no per-element Arc<Scope> allocation, eliminating the 48 MB / 200 K-block hot site flagged by P1-B.

Note: the returned Arc<Scope> is mutated through the inner Mutexes on ListContext. Callers must NOT alias this scope as a closure’s captured environment without first snapshotting the binding via Scope::current_iter_binding (see the Expr::Closure branch in eval.rs).

Source

pub fn set_iter_binding(&self, name: Arc<str>, value: Value, index: usize)

Refresh the named iter binding + index on a comprehension outer frame previously built by Scope::with_iter_loop.

O(1): one Mutex lock and one Value clone — no heap allocation for the scope frame itself. The path node is rewritten too so full_path() reports the right segment if the body asks for it.

Trait Implementations§

Source§

impl Clone for Scope

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Scope

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Scope

Source§

fn default() -> Scope

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !Freeze for Scope

§

impl RefUnwindSafe for Scope

§

impl Send for Scope

§

impl Sync for Scope

§

impl Unpin for Scope

§

impl UnsafeUnpin for Scope

§

impl UnwindSafe for Scope

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.