Skip to main content

ModuleLoader

Trait ModuleLoader 

Source
pub trait ModuleLoader {
    // Required method
    fn load(
        &self,
        specifier: &str,
        attributes: &[(String, String)],
        referrer: &str,
    ) -> Result<LoadedModule, String>;

    // Provided methods
    fn load_native(
        &self,
        interp: &mut Interp<'_>,
        name: &str,
    ) -> Option<Result<Gc<ModuleRecord>, String>> { ... }
    fn resolve(&self, specifier: &str, referrer: &str) -> Option<String> { ... }
}
Expand description

How the host turns a specifier into bytes.

The engine cannot do this itself: resolving ./x.js needs to know that the importer was a file, and where. A browser-shaped host would answer differently, so the host supplies this and the engine decides what to do with the bytes.

Required Methods§

Source

fn load( &self, specifier: &str, attributes: &[(String, String)], referrer: &str, ) -> Result<LoadedModule, String>

Resolve specifier relative to referrer and read it.

referrer is empty for the entry module.

Provided Methods§

Source

fn load_native( &self, interp: &mut Interp<'_>, name: &str, ) -> Option<Result<Gc<ModuleRecord>, String>>

Load a native module, if this loader has one at name.

The answer is a record, not bytes: a .so is not text to compile, it is a library that hands back exports. None means “this name is not native”, which sends the engine on to ModuleLoader::load — so a loader that knows nothing about native modules needs no change at all.

interp is the interpreter doing the load, because the library’s entry point takes a JSContext * and will call back into this very interpreter.

Source

fn resolve(&self, specifier: &str, referrer: &str) -> Option<String>

The name specifier resolves to from referrer, without reading anything.

A precompiled module is registered under the name a loader would give it, so the engine has to know that name before it can decide whether the module is already in memory. Without this the only way to ask is ModuleLoader::load, which reads the file — and a product that carries its dependencies has no files to read.

None means “this loader can only answer by loading”, which is the behaviour a loader that resolves against something other than text (an in-memory map, a network) legitimately has. Precompiled modules are then matched after the load, by the name the load reported.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§