0
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-03-03 17:34:47 -05:00

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.
This commit is contained in:
Bartek Iwańczuk 2020-11-21 16:23:35 +01:00 committed by GitHub
parent 04f4201f30
commit ec7f1e399e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 86 deletions

View file

@ -264,10 +264,11 @@ pub extern "C" fn host_initialize_import_meta_object_callback(
let state_rc = JsRuntime::state(scope); let state_rc = JsRuntime::state(scope);
let state = state_rc.borrow(); let state = state_rc.borrow();
let id = module.get_identity_hash(); let module_global = v8::Global::new(scope, module);
assert_ne!(id, 0); let info = state
.modules
let info = state.modules.get_info(id).expect("Module not found"); .get_info(&module_global)
.expect("Module not found");
let url_key = v8::String::new(scope, "url").unwrap(); let url_key = v8::String::new(scope, "url").unwrap();
let url_val = v8::String::new(scope, &info.name).unwrap(); let url_val = v8::String::new(scope, &info.name).unwrap();
@ -713,6 +714,7 @@ fn shared_getter(
rv.set(shared_ab.into()) rv.set(shared_ab.into())
} }
// Called by V8 during `Isolate::mod_instantiate`.
pub fn module_resolve_callback<'s>( pub fn module_resolve_callback<'s>(
context: v8::Local<'s, v8::Context>, context: v8::Local<'s, v8::Context>,
specifier: v8::Local<'s, v8::String>, specifier: v8::Local<'s, v8::String>,
@ -721,39 +723,38 @@ pub fn module_resolve_callback<'s>(
let scope = &mut unsafe { v8::CallbackScope::new(context) }; let scope = &mut unsafe { v8::CallbackScope::new(context) };
let state_rc = JsRuntime::state(scope); let state_rc = JsRuntime::state(scope);
let mut state = state_rc.borrow_mut(); let state = state_rc.borrow();
let referrer_id = referrer.get_identity_hash(); let referrer_global = v8::Global::new(scope, referrer);
let referrer_name = state let referrer_info = state
.modules .modules
.get_info(referrer_id) .get_info(&referrer_global)
.expect("ModuleInfo not found") .expect("ModuleInfo not found");
.name let referrer_name = referrer_info.name.to_string();
.to_string();
let len_ = referrer.get_module_requests_length();
let specifier_str = specifier.to_rust_string_lossy(scope); let specifier_str = specifier.to_rust_string_lossy(scope);
for i in 0..len_ { let resolved_specifier = state
let req = referrer.get_module_request(i); .loader
let req_str = req.to_rust_string_lossy(scope); .resolve(
state.op_state.clone(),
&specifier_str,
&referrer_name,
false,
)
.expect("Module should have been already resolved");
if let Some(id) = state.modules.get_id(resolved_specifier.as_str()) {
if let Some(handle) = state.modules.get_handle(id) {
return Some(v8::Local::new(scope, handle));
}
}
if req_str == specifier_str {
let id = state.module_resolve_cb(&req_str, referrer_id);
match state.modules.get_info(id) {
Some(info) => return Some(v8::Local::new(scope, &info.handle)),
None => {
let msg = format!( let msg = format!(
r#"Cannot resolve module "{}" from "{}""#, r#"Cannot resolve module "{}" from "{}""#,
req_str, referrer_name specifier_str, referrer_name
); );
throw_type_error(scope, msg); throw_type_error(scope, msg);
return None;
}
}
}
}
None None
} }

View file

@ -337,9 +337,9 @@ impl Stream for RecursiveModuleLoad {
} }
pub struct ModuleInfo { pub struct ModuleInfo {
pub id: ModuleId,
pub main: bool, pub main: bool,
pub name: String, pub name: String,
pub handle: v8::Global<v8::Module>,
pub import_specifiers: Vec<ModuleSpecifier>, pub import_specifiers: Vec<ModuleSpecifier>,
} }
@ -372,17 +372,12 @@ impl ModuleNameMap {
pub fn get(&self, name: &str) -> Option<ModuleId> { pub fn get(&self, name: &str) -> Option<ModuleId> {
let mut mod_name = name; let mut mod_name = name;
loop { loop {
let cond = self.inner.get(mod_name); let symbolic_module = self.inner.get(mod_name)?;
match cond { match symbolic_module {
Some(SymbolicModule::Alias(target)) => { SymbolicModule::Alias(target) => {
mod_name = target; mod_name = target;
} }
Some(SymbolicModule::Mod(mod_id)) => { SymbolicModule::Mod(mod_id) => return Some(*mod_id),
return Some(*mod_id);
}
_ => {
return None;
}
} }
} }
} }
@ -408,15 +403,21 @@ impl ModuleNameMap {
/// A collection of JS modules. /// A collection of JS modules.
#[derive(Default)] #[derive(Default)]
pub struct Modules { 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, by_name: ModuleNameMap,
next_module_id: ModuleId,
} }
impl Modules { impl Modules {
pub fn new() -> Modules { pub fn new() -> Modules {
Self { Self {
handles_by_id: HashMap::new(),
ids_by_handle: HashMap::new(),
info: HashMap::new(), info: HashMap::new(),
by_name: ModuleNameMap::new(), by_name: ModuleNameMap::new(),
next_module_id: 1,
} }
} }
@ -428,35 +429,33 @@ impl Modules {
self.info.get(&id).map(|i| &i.import_specifiers) 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 { pub fn is_registered(&self, specifier: &ModuleSpecifier) -> bool {
self.by_name.get(&specifier.to_string()).is_some() self.by_name.get(&specifier.to_string()).is_some()
} }
pub fn register( pub fn register(
&mut self, &mut self,
id: ModuleId,
name: &str, name: &str,
main: bool, main: bool,
handle: v8::Global<v8::Module>, handle: v8::Global<v8::Module>,
import_specifiers: Vec<ModuleSpecifier>, import_specifiers: Vec<ModuleSpecifier>,
) { ) -> ModuleId {
let name = String::from(name); 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.by_name.insert(name.clone(), id);
self.handles_by_id.insert(id, handle.clone());
self.ids_by_handle.insert(handle, id);
self.info.insert( self.info.insert(
id, id,
ModuleInfo { ModuleInfo {
id,
main, main,
name, name,
import_specifiers, import_specifiers,
handle,
}, },
); );
id
} }
pub fn alias(&mut self, name: &str, target: &str) { pub fn alias(&mut self, name: &str, target: &str) {
@ -468,11 +467,19 @@ impl Modules {
self.by_name.is_alias(name) self.by_name.is_alias(name)
} }
pub fn get_info(&self, id: ModuleId) -> Option<&ModuleInfo> { pub fn get_handle(&self, id: ModuleId) -> Option<v8::Global<v8::Module>> {
if id == 0 { self.handles_by_id.get(&id).cloned()
return None;
} }
self.info.get(&id)
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);
}
None
} }
} }

View file

@ -114,7 +114,7 @@ pub(crate) struct JsRuntimeState {
pub(crate) have_unpolled_ops: Cell<bool>, pub(crate) have_unpolled_ops: Cell<bool>,
//pub(crate) op_table: OpTable, //pub(crate) op_table: OpTable,
pub(crate) op_state: Rc<RefCell<OpState>>, pub(crate) op_state: Rc<RefCell<OpState>>,
loader: Rc<dyn ModuleLoader>, pub loader: Rc<dyn ModuleLoader>,
pub modules: Modules, pub modules: Modules,
pub(crate) dyn_import_map: pub(crate) dyn_import_map:
HashMap<ModuleLoadId, v8::Global<v8::PromiseResolver>>, HashMap<ModuleLoadId, v8::Global<v8::PromiseResolver>>,
@ -572,20 +572,6 @@ where
} }
impl JsRuntimeState { impl JsRuntimeState {
// Called by V8 during `Isolate::mod_instantiate`.
pub fn module_resolve_cb(
&mut self,
specifier: &str,
referrer_id: ModuleId,
) -> ModuleId {
let referrer = self.modules.get_name(referrer_id).unwrap();
let specifier = self
.loader
.resolve(self.op_state.clone(), specifier, referrer, false)
.expect("Module should have been already resolved");
self.modules.get_id(specifier.as_str()).unwrap_or(0)
}
// Called by V8 during `Isolate::mod_instantiate`. // Called by V8 during `Isolate::mod_instantiate`.
pub fn dyn_import_cb( pub fn dyn_import_cb(
&mut self, &mut self,
@ -687,7 +673,6 @@ impl JsRuntime {
} }
let module = maybe_module.unwrap(); let module = maybe_module.unwrap();
let id = module.get_identity_hash();
let mut import_specifiers: Vec<ModuleSpecifier> = vec![]; let mut import_specifiers: Vec<ModuleSpecifier> = vec![];
for i in 0..module.get_module_requests_length() { for i in 0..module.get_module_requests_length() {
@ -703,8 +688,7 @@ impl JsRuntime {
import_specifiers.push(module_specifier); import_specifiers.push(module_specifier);
} }
state_rc.borrow_mut().modules.register( let id = state_rc.borrow_mut().modules.register(
id,
name, name,
main, main,
v8::Global::<v8::Module>::new(tc_scope, module), v8::Global::<v8::Module>::new(tc_scope, module),
@ -726,13 +710,12 @@ impl JsRuntime {
let scope = &mut v8::HandleScope::with_context(self.v8_isolate(), context); let scope = &mut v8::HandleScope::with_context(self.v8_isolate(), context);
let tc_scope = &mut v8::TryCatch::new(scope); let tc_scope = &mut v8::TryCatch::new(scope);
let state = state_rc.borrow(); let module = state_rc
let module = match state.modules.get_info(id) { .borrow()
Some(info) => v8::Local::new(tc_scope, &info.handle), .modules
None if id == 0 => return Ok(()), .get_handle(id)
_ => panic!("module id {} not found in module table", id), .map(|handle| v8::Local::new(tc_scope, handle))
}; .expect("ModuleInfo not found");
drop(state);
if module.get_status() == v8::ModuleStatus::Errored { if module.get_status() == v8::ModuleStatus::Errored {
exception_to_err_result(tc_scope, module.get_exception(), false)? exception_to_err_result(tc_scope, module.get_exception(), false)?
@ -768,10 +751,8 @@ impl JsRuntime {
let module_handle = state_rc let module_handle = state_rc
.borrow() .borrow()
.modules .modules
.get_info(id) .get_handle(id)
.expect("ModuleInfo not found") .expect("ModuleInfo not found");
.handle
.clone();
let status = { let status = {
let scope = let scope =
@ -858,8 +839,8 @@ impl JsRuntime {
let module = state_rc let module = state_rc
.borrow() .borrow()
.modules .modules
.get_info(id) .get_handle(id)
.map(|info| v8::Local::new(scope, &info.handle)) .map(|handle| v8::Local::new(scope, handle))
.expect("ModuleInfo not found"); .expect("ModuleInfo not found");
let mut status = module.get_status(); let mut status = module.get_status();
@ -970,7 +951,6 @@ impl JsRuntime {
let context = self.global_context(); let context = self.global_context();
debug!("dyn_import_done {} {:?}", id, mod_id); debug!("dyn_import_done {} {:?}", id, mod_id);
assert!(mod_id != 0);
let scope = &mut v8::HandleScope::with_context(self.v8_isolate(), context); let scope = &mut v8::HandleScope::with_context(self.v8_isolate(), context);
let resolver_handle = state_rc let resolver_handle = state_rc
@ -984,8 +964,8 @@ impl JsRuntime {
let state = state_rc.borrow(); let state = state_rc.borrow();
state state
.modules .modules
.get_info(mod_id) .get_handle(mod_id)
.map(|info| v8::Local::new(scope, &info.handle)) .map(|handle| v8::Local::new(scope, handle))
.expect("Dyn import module info not found") .expect("Dyn import module info not found")
}; };
// Resolution success // Resolution success