Skip to main content

PluginResolver

Trait PluginResolver 

Source
pub trait PluginResolver {
    // Required methods
    fn has_binding(&self, name: &str) -> bool;
    fn resolve(
        &self,
        name: &str,
        ctx: &mut EvalContext,
    ) -> Result<JsValue, JErrorType>;
    fn call_method(
        &self,
        object_name: &str,
        method_name: &str,
        ctx: &mut EvalContext,
        this: JsValue,
        args: Vec<JsValue>,
    ) -> Option<Result<JsValue, JErrorType>>;
    fn name(&self) -> &str;

    // Provided method
    fn call_constructor(
        &self,
        _object_name: &str,
        _ctx: &mut EvalContext,
        _args: Vec<JsValue>,
    ) -> Option<Result<JsValue, JErrorType>> { ... }
}
Expand description

A plugin resolver that can dynamically provide named objects and their methods.

Resolvers are queried in registration order when a name lookup reaches the super-global scope. The first resolver that claims a name wins.

Required Methods§

Source

fn has_binding(&self, name: &str) -> bool

Does this resolver provide a binding with the given name?

This should be a cheap check (e.g. a HashSet::contains). It must NOT allocate or materialize the object.

Source

fn resolve( &self, name: &str, ctx: &mut EvalContext, ) -> Result<JsValue, JErrorType>

Materialize the object for the given name.

Called only after has_binding returns true. The returned JsValue is cached in the super-global environment so this is called at most once per name per execution.

Source

fn call_method( &self, object_name: &str, method_name: &str, ctx: &mut EvalContext, this: JsValue, args: Vec<JsValue>, ) -> Option<Result<JsValue, JErrorType>>

Resolve a method on an object this resolver provides.

For example, if this resolver provides "Math", then resolve_method("Math", "abs", ctx, this, args) should execute Math.abs.

Returns None if the method is not found, allowing fallback to property lookup on the materialized object.

Source

fn name(&self) -> &str

Human-readable name for this resolver (for debugging/logging).

Provided Methods§

Source

fn call_constructor( &self, _object_name: &str, _ctx: &mut EvalContext, _args: Vec<JsValue>, ) -> Option<Result<JsValue, JErrorType>>

Get a constructor for the given object name, if available.

Returns None if this resolver doesn’t provide a constructor for the name.

Implementors§