pub struct SuperGlobalEnvironment { /* private fields */ }Expand description
The super-global environment for lazy resolution of built-in objects.
This is the outermost scope in the resolution chain, sitting below even the global scope. It provides built-in objects (Math, console, etc.) and plugin-provided objects through a lazy resolution mechanism.
§Key Features
- Lazy Resolution: Objects are only materialized when first accessed
- Caching: Once resolved, values are cached for fast subsequent access
- Read-Only: JavaScript code cannot create or modify super-global bindings
- Plugin System: Multiple resolvers can be registered, queried in order
- Method Dispatch: Efficient method calls without object materialization
§Resolution Order
When a name is looked up:
- Check if it’s in the cache → return cached value
- Query each resolver’s
has_binding()in registration order - First resolver that claims the name wins
- Call resolver’s
resolve()to materialize the value - Cache the value and resolver index
- Return the value
§Method Call Optimization
For method calls like Math.abs(-5), the super-global can dispatch directly
to the resolver’s call_method() without materializing the Math object,
providing better performance for built-in method calls.
§Thread Safety
This struct is typically wrapped in Rc<RefCell<>> (see SharedSuperGlobal)
to allow shared mutable access across the evaluation context.
Implementations§
Source§impl SuperGlobalEnvironment
impl SuperGlobalEnvironment
pub fn new() -> Self
Sourcepub fn add_resolver(&mut self, resolver: Box<dyn PluginResolver>)
pub fn add_resolver(&mut self, resolver: Box<dyn PluginResolver>)
Register a plugin resolver. Resolvers are queried in registration order.
Sourcepub fn call_method(
&self,
object_name: &str,
method_name: &str,
ctx: &mut EvalContext,
this: JsValue,
args: Vec<JsValue>,
) -> Option<Result<JsValue, JErrorType>>
pub fn call_method( &self, object_name: &str, method_name: &str, ctx: &mut EvalContext, this: JsValue, args: Vec<JsValue>, ) -> Option<Result<JsValue, JErrorType>>
Call a method on a super-global object, dispatching to the owning resolver.
Returns None if no resolver owns object_name or the resolver
doesn’t provide the method (allowing fallback to property lookup).
Sourcepub fn call_constructor(
&self,
object_name: &str,
ctx: &mut EvalContext,
args: Vec<JsValue>,
) -> Option<Result<JsValue, JErrorType>>
pub fn call_constructor( &self, object_name: &str, ctx: &mut EvalContext, args: Vec<JsValue>, ) -> Option<Result<JsValue, JErrorType>>
Call a constructor on a super-global object.
Sourcepub fn resolve_binding(
&mut self,
name: &str,
ctx: &mut EvalContext,
) -> Result<JsValue, JErrorType>
pub fn resolve_binding( &mut self, name: &str, ctx: &mut EvalContext, ) -> Result<JsValue, JErrorType>
Resolve a name, caching the result.
ctx is needed because some resolvers may need it during materialization.
Sourcepub fn resolvers(&self) -> &[Box<dyn PluginResolver>]
pub fn resolvers(&self) -> &[Box<dyn PluginResolver>]
Get a reference to the resolvers (for inspection/testing).