Skip to content

Commit

Permalink
cargo clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
hansl committed Mar 28, 2024
1 parent 8261a35 commit 79ea902
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 17 deletions.
5 changes: 1 addition & 4 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions core/interop/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ rust-version.workspace = true
[dependencies]
boa_engine.workspace = true
boa_gc.workspace = true
quote = "1.0.34"
syn = { version = "2.0.55", features = ["full"] }
proc-macro2 = "1.0"
synstructure = "0.13"
rustc-hash = { workspace = true, features = ["std"] }

[lints]
workspace = true
14 changes: 10 additions & 4 deletions core/interop/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
//! Interop utilities between Boa and its host.
use std::cell::RefCell;

use boa_engine::builtins::promise::PromiseState;
use boa_engine::module::SyntheticModuleInitializer;
use boa_engine::{Context, JsString, JsValue, Module, NativeFunction, Source};
use boa_engine::{Context, JsString, JsValue, Module, NativeFunction};

pub mod loaders;

/// A trait to convert a type into a JS module.
pub trait IntoJsModule {
/// Converts the type into a JS module.
fn into_js_module(self, context: &mut Context) -> Module;
}

Expand All @@ -32,11 +34,13 @@ impl<T: IntoIterator<Item = (JsString, NativeFunction)> + Clone> IntoJsModule fo
}
}

/// A trait to convert a type into a JS function.
pub trait IntoJsFunction {
/// Converts the type into a JS function.
fn into_js_function(self, context: &mut Context) -> NativeFunction;
}

impl<T: FnMut() -> () + 'static> IntoJsFunction for T {
impl<T: FnMut() + 'static> IntoJsFunction for T {
fn into_js_function(self, _context: &mut Context) -> NativeFunction {
let s = RefCell::new(self);

Expand All @@ -50,8 +54,10 @@ impl<T: FnMut() -> () + 'static> IntoJsFunction for T {
}

#[test]
#[allow(clippy::missing_panics_doc)]
pub fn into_js_module() {
use boa_engine::{js_string, JsValue};
use boa_engine::builtins::promise::PromiseState;
use boa_engine::{js_string, JsValue, Source};
use std::rc::Rc;

let loader = Rc::new(loaders::HashMapModuleLoader::new());
Expand Down
2 changes: 1 addition & 1 deletion core/interop/src/loaders.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! A collection of JS [boa_engine::module::ModuleLoader]s utilities to help in
//! A collection of JS [`boa_engine::module::ModuleLoader`]s utilities to help in
//! creating custom module loaders.
pub use hashmap::HashMapModuleLoader;
Expand Down
25 changes: 21 additions & 4 deletions core/interop/src/loaders/hashmap.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,42 @@
use std::collections::HashMap;
//! A `ModuleLoader` that loads modules from a `HashMap` based on the name.
use rustc_hash::FxHashMap;

use boa_engine::module::{ModuleLoader, Referrer};
use boa_engine::{Context, JsNativeError, JsResult, JsString, Module};
use boa_gc::GcRefCell;

/// A ModuleLoader that loads modules from a HashMap based on the name.
/// A `ModuleLoader` that loads modules from a `HashMap` based on the name.
/// After registering modules, this loader will look for the exact name
/// in its internal map to resolve.
#[derive(Debug, Clone)]
pub struct HashMapModuleLoader(GcRefCell<HashMap<JsString, Module>>);
pub struct HashMapModuleLoader(GcRefCell<FxHashMap<JsString, Module>>);

impl Default for HashMapModuleLoader {
fn default() -> Self {
Self(GcRefCell::new(FxHashMap::default()))
}
}

impl HashMapModuleLoader {
/// Creates an empty `HashMapModuleLoader`.
#[must_use]
pub fn new() -> Self {
Self(GcRefCell::new(HashMap::new()))
Self::default()
}

/// Registers a module with a given name.
pub fn register(&self, key: impl Into<JsString>, value: Module) {
self.0.borrow_mut().insert(key.into(), value);
}
}

impl FromIterator<(JsString, Module)> for HashMapModuleLoader {
fn from_iter<T: IntoIterator<Item = (JsString, Module)>>(iter: T) -> Self {
let map = iter.into_iter().collect();
Self(GcRefCell::new(map))
}
}

impl ModuleLoader for HashMapModuleLoader {
fn load_imported_module(
&self,
Expand Down

0 comments on commit 79ea902

Please sign in to comment.