Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ boa_engine = { version = "~0.20.0", path = "core/engine" }
boa_gc = { version = "~0.20.0", path = "core/gc" }
boa_icu_provider = { version = "~0.20.0", path = "core/icu_provider" }
boa_interner = { version = "~0.20.0", path = "core/interner" }
boa_interop = { version = "~0.20.0", path = "core/interop" }
boa_macros = { version = "~0.20.0", path = "core/macros" }
boa_parser = { version = "~0.20.0", path = "core/parser" }
boa_runtime = { version = "~0.20.0", path = "core/runtime" }
Expand Down
2 changes: 2 additions & 0 deletions core/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ rust-version.workspace = true
[features]
default = ["float16", "xsum"]

embedded_lz4 = ["boa_macros/embedded_lz4", "lz4_flex"]

# Replace the NaN-boxing implementation of `JsValueInner` with an
# enum-based implementation. This implementation is less performant
Expand Down Expand Up @@ -95,6 +96,7 @@ boa_string.workspace = true
cow-utils.workspace = true
futures-lite.workspace = true
float16 = { version = "0.1", optional = true }
lz4_flex = { workspace = true, optional = true }
xsum = { version = "0.1.1", optional = true }
serde = { workspace = true, features = ["derive", "rc"] }
serde_json.workspace = true
Expand Down
3 changes: 3 additions & 0 deletions core/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ pub mod prelude {
pub use boa_parser::Source;
}

#[doc(inline)]
pub use boa_macros::{boa_class, boa_module, embed_module_inner as __embed_module_inner};

use std::result::Result as StdResult;

// Export things to root level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use boa_engine::{Context, JsNativeError, JsResult, JsString, Module, Source};
#[macro_export]
macro_rules! embed_module {
($($x: expr),*) => {
$crate::loaders::embedded::EmbeddedModuleLoader::from_iter(
$crate::boa_macros::embed_module_inner!($($x),*),
$crate::module::embedded::EmbeddedModuleLoader::from_iter(
$crate::__embed_module_inner!($($x),*),
)
};
}
Expand Down Expand Up @@ -104,11 +104,12 @@ pub struct EmbeddedModuleLoader {
}

impl EmbeddedModuleLoader {
/// Gets a module in the `EmbeddedModuleLoader`.
/// Get a module if it has been parsed and created. If the module is not found or
/// was not loaded, this will return `None`.
#[must_use]
pub fn get_module(&self, specifier: &JsString) -> Option<Module> {
pub fn get_module(&self, name: &JsString) -> Option<Module> {
self.map
.get(specifier)
.get(name)
.and_then(|module| module.borrow().as_module().cloned())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ use boa_parser::Source;

use crate::script::Script;
use crate::{
Context, JsError, JsNativeError, JsResult, JsString, js_string, object::JsObject, realm::Realm,
vm::ActiveRunnable,
Context, JsError, JsNativeError, JsResult, JsString, js_error, js_string, object::JsObject,
realm::Realm, vm::ActiveRunnable,
};

use super::Module;

pub mod embedded;

/// Resolves paths from the referrer and the specifier, normalize the paths and ensure the path
/// is within a base. If the base is empty, that last verification will be skipped.
///
Expand Down Expand Up @@ -266,6 +268,76 @@ impl ModuleLoader for IdleModuleLoader {
}
}

/// A module loader that uses a map of specifier -> Module to resolve.
/// If the module was not registered, it will not be resolved.
///
/// A resolution relative to the referrer is performed when loading a
/// module.
#[derive(Default, Debug, Clone)]
pub struct MapModuleLoader {
inner: RefCell<FxHashMap<PathBuf, Module>>,
}

impl MapModuleLoader {
/// Creates an empty map module loader.
#[must_use]
#[inline]
pub fn new() -> Self {
Self::default()
}

/// Insert or replace a mapping in the inner map, returning any previous module
/// if there was one.
#[inline]
pub fn insert(&self, specifier: impl AsRef<str>, module: Module) -> Option<Module> {
self.inner
.borrow_mut()
.insert(PathBuf::from(specifier.as_ref()), module)
}

/// Clear the map.
pub fn clear(&self) {
self.inner.borrow_mut().clear();
}
}

impl FromIterator<(String, Module)> for MapModuleLoader {
fn from_iter<T: IntoIterator<Item = (String, Module)>>(iter: T) -> Self {
Self {
inner: RefCell::new(
iter.into_iter()
.map(|(k, v)| (PathBuf::from(k), v))
.collect(),
),
}
}
}

impl ModuleLoader for MapModuleLoader {
fn load_imported_module(
self: Rc<Self>,
referrer: Referrer,
specifier: JsString,
context: &RefCell<&mut Context>,
) -> impl Future<Output = JsResult<Module>> {
let result = (|| {
let path = resolve_module_specifier(
None,
&specifier,
referrer.path(),
&mut context.borrow_mut(),
)?;
if let Some(module) = self.inner.borrow().get(&path) {
Ok(module.clone())
} else {
Err(js_error!(TypeError: "Module could not be found."))
}
})();

async { result }
}
}

/// A simple module loader that loads modules relative to a root path.
///
/// # Note
Expand Down
Loading
Loading