diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2020-11-21 16:23:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-21 16:23:35 +0100 |
commit | ec7f1e399e1c1d74bf7af060a21010138de208df (patch) | |
tree | 885c8e83203d149efbf5d74660fa856590c3c9c3 /core/modules.rs | |
parent | 04f4201f30b03f5afd183eb79cd3f9329a0426b7 (diff) |
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.
Diffstat (limited to 'core/modules.rs')
-rw-r--r-- | core/modules.rs | 55 |
1 files changed, 31 insertions, 24 deletions
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<v8::Module>, pub import_specifiers: Vec<ModuleSpecifier>, } @@ -372,17 +372,12 @@ impl ModuleNameMap { pub fn get(&self, name: &str) -> Option<ModuleId> { 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<ModuleId, ModuleInfo>, + ids_by_handle: HashMap<v8::Global<v8::Module>, ModuleId>, + handles_by_id: HashMap<ModuleId, v8::Global<v8::Module>>, + info: HashMap<ModuleId, ModuleInfo>, 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<v8::Module>, import_specifiers: Vec<ModuleSpecifier>, - ) { + ) -> 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<v8::Global<v8::Module>> { + self.handles_by_id.get(&id).cloned() + } + + pub fn get_info( + &self, + global: &v8::Global<v8::Module>, + ) -> Option<&ModuleInfo> { + if let Some(id) = self.ids_by_handle.get(global) { + return self.info.get(id); } - self.info.get(&id) + + None } } |