Trait boa_engine::module::ModuleLoader
source · pub trait ModuleLoader {
// Required method
fn load_imported_module(
&self,
referrer: Referrer,
specifier: JsString,
finish_load: Box<dyn FnOnce(JsResult<Module>, &mut Context<'_>)>,
context: &mut Context<'_>
);
// Provided methods
fn register_module(&self, _specifier: JsString, _module: Module) { ... }
fn get_module(&self, _specifier: JsString) -> Option<Module> { ... }
fn init_import_meta(
&self,
_import_meta: &JsObject,
_module: &Module,
_context: &mut Context<'_>
) { ... }
}
Expand description
Module loading related host hooks.
This trait allows to customize the behaviour of the engine on module load requests and
import.meta
requests.
Required Methods§
sourcefn load_imported_module(
&self,
referrer: Referrer,
specifier: JsString,
finish_load: Box<dyn FnOnce(JsResult<Module>, &mut Context<'_>)>,
context: &mut Context<'_>
)
fn load_imported_module( &self, referrer: Referrer, specifier: JsString, finish_load: Box<dyn FnOnce(JsResult<Module>, &mut Context<'_>)>, context: &mut Context<'_> )
Host hook HostLoadImportedModule ( referrer, specifier, hostDefined, payload )
.
This hook allows to customize the module loading functionality of the engine. Technically,
this should call the FinishLoadingImportedModule
operation, but this simpler API just provides
a closure that replaces FinishLoadingImportedModule
.
Requirements
- The host environment must perform
FinishLoadingImportedModule(referrer, specifier, payload, result)
, where result is either a normal completion containing the loaded Module Record or a throw completion, either synchronously or asynchronously. This is equivalent to calling thefinish_load
callback. - If this operation is called multiple times with the same
(referrer, specifier)
pair and it performs FinishLoadingImportedModule(referrer, specifier, payload, result) where result is a normal completion, then it must performFinishLoadingImportedModule(referrer, specifier, payload, result)
with the same result each time. - The operation must treat payload as an opaque value to be passed through to
FinishLoadingImportedModule
. (can be ignored)
Provided Methods§
sourcefn register_module(&self, _specifier: JsString, _module: Module)
fn register_module(&self, _specifier: JsString, _module: Module)
Registers a new module into the module loader.
This is a convenience method for module loaders caching already parsed modules, since it
allows registering a new module through the &dyn ModuleLoader
provided by
Context::module_loader
.
Does nothing by default.
sourcefn get_module(&self, _specifier: JsString) -> Option<Module>
fn get_module(&self, _specifier: JsString) -> Option<Module>
Gets the module associated with the provided specifier.
This is a convenience method for module loaders caching already parsed modules, since it
allows getting a cached module through the &dyn ModuleLoader
provided by
Context::module_loader
.
Returns None
by default.
sourcefn init_import_meta(
&self,
_import_meta: &JsObject,
_module: &Module,
_context: &mut Context<'_>
)
fn init_import_meta( &self, _import_meta: &JsObject, _module: &Module, _context: &mut Context<'_> )
Host hooks HostGetImportMetaProperties ( moduleRecord )
and
HostFinalizeImportMeta ( importMeta, moduleRecord )
.
This unifies both APIs into a single hook that can be overriden on both cases.
The most common usage is to add properties to import_meta
and return, but this also
allows modifying the import meta object in more exotic ways before exposing it to ECMAScript
code.
The default implementation of HostGetImportMetaProperties
is to return a new empty List.