From ec7f1e399e1c1d74bf7af060a21010138de208df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Sat, 21 Nov 2020 16:23:35 +0100 Subject: refactor(core): don't depend on get_identity_hash for modules (#8354) This commit refactors "deno_core::Modules" structure to not depend on "get_identity_hash" function to identify modules, but instead use default hash implementation. --- core/modules.rs | 55 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 24 deletions(-) (limited to 'core/modules.rs') diff --git a/core/modules.rs b/core/modules.rs index f1f540b68..c12f900bb 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -337,9 +337,9 @@ impl Stream for RecursiveModuleLoad { } pub struct ModuleInfo { + pub id: ModuleId, pub main: bool, pub name: String, - pub handle: v8::Global, pub import_specifiers: Vec, } @@ -372,17 +372,12 @@ impl ModuleNameMap { pub fn get(&self, name: &str) -> Option { let mut mod_name = name; loop { - let cond = self.inner.get(mod_name); - match cond { - Some(SymbolicModule::Alias(target)) => { + let symbolic_module = self.inner.get(mod_name)?; + match symbolic_module { + SymbolicModule::Alias(target) => { mod_name = target; } - Some(SymbolicModule::Mod(mod_id)) => { - return Some(*mod_id); - } - _ => { - return None; - } + SymbolicModule::Mod(mod_id) => return Some(*mod_id), } } } @@ -408,15 +403,21 @@ impl ModuleNameMap { /// A collection of JS modules. #[derive(Default)] pub struct Modules { - pub(crate) info: HashMap, + ids_by_handle: HashMap, ModuleId>, + handles_by_id: HashMap>, + info: HashMap, by_name: ModuleNameMap, + next_module_id: ModuleId, } impl Modules { pub fn new() -> Modules { Self { + handles_by_id: HashMap::new(), + ids_by_handle: HashMap::new(), info: HashMap::new(), by_name: ModuleNameMap::new(), + next_module_id: 1, } } @@ -428,35 +429,33 @@ impl Modules { self.info.get(&id).map(|i| &i.import_specifiers) } - pub fn get_name(&self, id: ModuleId) -> Option<&String> { - self.info.get(&id).map(|i| &i.name) - } - pub fn is_registered(&self, specifier: &ModuleSpecifier) -> bool { self.by_name.get(&specifier.to_string()).is_some() } pub fn register( &mut self, - id: ModuleId, name: &str, main: bool, handle: v8::Global, import_specifiers: Vec, - ) { + ) -> ModuleId { let name = String::from(name); - debug!("register_complete {}", name); - + let id = self.next_module_id; + self.next_module_id += 1; self.by_name.insert(name.clone(), id); + self.handles_by_id.insert(id, handle.clone()); + self.ids_by_handle.insert(handle, id); self.info.insert( id, ModuleInfo { + id, main, name, import_specifiers, - handle, }, ); + id } pub fn alias(&mut self, name: &str, target: &str) { @@ -468,11 +467,19 @@ impl Modules { self.by_name.is_alias(name) } - pub fn get_info(&self, id: ModuleId) -> Option<&ModuleInfo> { - if id == 0 { - return None; + pub fn get_handle(&self, id: ModuleId) -> Option> { + self.handles_by_id.get(&id).cloned() + } + + pub fn get_info( + &self, + global: &v8::Global, + ) -> Option<&ModuleInfo> { + if let Some(id) = self.ids_by_handle.get(global) { + return self.info.get(id); } - self.info.get(&id) + + None } } -- cgit v1.2.3