diff options
author | Bartek IwaĆczuk <biwanczuk@gmail.com> | 2023-03-08 07:44:54 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-03-08 12:44:54 +0100 |
commit | 72fe9bb47005e720444e65a66e91559287137780 (patch) | |
tree | d529ee7f3b2c33063ba4496f8fd3dfdbac0d1355 /core/modules.rs | |
parent | d24c6ea27f7dea57e3bc0cda342d6cbe59782f7d (diff) |
refactor: rename InternalModuleLoader to ExtModuleLoader, use ext: scheme for snapshotted modules (#18041)
This commit renames "deno_core::InternalModuleLoader" to
"ExtModuleLoader" and changes the specifiers used by the
modules loaded from this loader to "ext:".
"internal:" scheme was really ambiguous and it's more characters than
"ext:", which should result in slightly smaller snapshot size.
Closes https://github.com/denoland/deno/issues/18020
Diffstat (limited to 'core/modules.rs')
-rw-r--r-- | core/modules.rs | 62 |
1 files changed, 30 insertions, 32 deletions
diff --git a/core/modules.rs b/core/modules.rs index 3d335f8eb..3a3b1c370 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -301,7 +301,7 @@ impl ModuleLoader for NoopModuleLoader { } /// Helper function, that calls into `loader.resolve()`, but denies resolution -/// of `internal` scheme if we are running with a snapshot loaded and not +/// of `ext` scheme if we are running with a snapshot loaded and not /// creating a snapshot pub(crate) fn resolve_helper( snapshot_loaded_and_not_snapshotting: bool, @@ -310,29 +310,28 @@ pub(crate) fn resolve_helper( referrer: &str, kind: ResolutionKind, ) -> Result<ModuleSpecifier, Error> { - if snapshot_loaded_and_not_snapshotting && specifier.starts_with("internal:") - { + if snapshot_loaded_and_not_snapshotting && specifier.starts_with("ext:") { return Err(generic_error( - "Cannot load internal module from external code", + "Cannot load extension module from external code", )); } loader.resolve(specifier, referrer, kind) } -/// Function that can be passed to the `InternalModuleLoader` that allows to +/// Function that can be passed to the `ExtModuleLoader` that allows to /// transpile sources before passing to V8. -pub type InternalModuleLoaderCb = +pub type ExtModuleLoaderCb = Box<dyn Fn(&ExtensionFileSource) -> Result<String, Error>>; -pub struct InternalModuleLoader { +pub struct ExtModuleLoader { module_loader: Rc<dyn ModuleLoader>, esm_sources: Vec<ExtensionFileSource>, used_esm_sources: RefCell<HashMap<String, bool>>, - maybe_load_callback: Option<InternalModuleLoaderCb>, + maybe_load_callback: Option<ExtModuleLoaderCb>, } -impl Default for InternalModuleLoader { +impl Default for ExtModuleLoader { fn default() -> Self { Self { module_loader: Rc::new(NoopModuleLoader), @@ -343,18 +342,18 @@ impl Default for InternalModuleLoader { } } -impl InternalModuleLoader { +impl ExtModuleLoader { pub fn new( module_loader: Option<Rc<dyn ModuleLoader>>, esm_sources: Vec<ExtensionFileSource>, - maybe_load_callback: Option<InternalModuleLoaderCb>, + maybe_load_callback: Option<ExtModuleLoaderCb>, ) -> Self { let used_esm_sources: HashMap<String, bool> = esm_sources .iter() .map(|file_source| (file_source.specifier.to_string(), false)) .collect(); - InternalModuleLoader { + ExtModuleLoader { module_loader: module_loader.unwrap_or_else(|| Rc::new(NoopModuleLoader)), esm_sources, used_esm_sources: RefCell::new(used_esm_sources), @@ -363,7 +362,7 @@ impl InternalModuleLoader { } } -impl Drop for InternalModuleLoader { +impl Drop for ExtModuleLoader { fn drop(&mut self) { let used_esm_sources = self.used_esm_sources.get_mut(); let unused_modules: Vec<_> = used_esm_sources @@ -374,8 +373,8 @@ impl Drop for InternalModuleLoader { if !unused_modules.is_empty() { let mut msg = - "Following modules were passed to InternalModuleLoader but never used:\n" - .to_string(); + "Following modules were passed to ExtModuleLoader but never used:\n" + .to_string(); for m in unused_modules { msg.push_str(" - "); msg.push_str(m); @@ -386,7 +385,7 @@ impl Drop for InternalModuleLoader { } } -impl ModuleLoader for InternalModuleLoader { +impl ModuleLoader for ExtModuleLoader { fn resolve( &self, specifier: &str, @@ -394,14 +393,13 @@ impl ModuleLoader for InternalModuleLoader { kind: ResolutionKind, ) -> Result<ModuleSpecifier, Error> { if let Ok(url_specifier) = ModuleSpecifier::parse(specifier) { - if url_specifier.scheme() == "internal" { + if url_specifier.scheme() == "ext" { let referrer_specifier = ModuleSpecifier::parse(referrer).ok(); - if referrer == "." || referrer_specifier.unwrap().scheme() == "internal" - { + if referrer == "." || referrer_specifier.unwrap().scheme() == "ext" { return Ok(url_specifier); } else { return Err(generic_error( - "Cannot load internal module from external code", + "Cannot load extension module from external code", )); }; } @@ -416,7 +414,7 @@ impl ModuleLoader for InternalModuleLoader { maybe_referrer: Option<ModuleSpecifier>, is_dyn_import: bool, ) -> Pin<Box<ModuleSourceFuture>> { - if module_specifier.scheme() != "internal" { + if module_specifier.scheme() != "ext" { return self.module_loader.load( module_specifier, maybe_referrer, @@ -461,7 +459,7 @@ impl ModuleLoader for InternalModuleLoader { async move { Err(generic_error(format!( - "Cannot find internal module source for specifier {specifier}" + "Cannot find extension module source for specifier {specifier}" ))) } .boxed_local() @@ -474,7 +472,7 @@ impl ModuleLoader for InternalModuleLoader { maybe_referrer: Option<String>, is_dyn_import: bool, ) -> Pin<Box<dyn Future<Output = Result<(), Error>>>> { - if module_specifier.scheme() == "internal" { + if module_specifier.scheme() == "ext" { return async { Ok(()) }.boxed_local(); } @@ -2911,17 +2909,17 @@ if (import.meta.url != 'file:///main_with_code.js') throw Error(); } #[test] - fn internal_module_loader() { - let loader = InternalModuleLoader::default(); + fn ext_module_loader() { + let loader = ExtModuleLoader::default(); assert!(loader - .resolve("internal:foo", "internal:bar", ResolutionKind::Import) + .resolve("ext:foo", "ext:bar", ResolutionKind::Import) .is_ok()); assert_eq!( loader - .resolve("internal:foo", "file://bar", ResolutionKind::Import) + .resolve("ext:foo", "file://bar", ResolutionKind::Import) .err() .map(|e| e.to_string()), - Some("Cannot load internal module from external code".to_string()) + Some("Cannot load extension module from external code".to_string()) ); assert_eq!( loader @@ -2935,11 +2933,11 @@ if (import.meta.url != 'file:///main_with_code.js') throw Error(); ); assert_eq!( loader - .resolve("file://foo", "internal:bar", ResolutionKind::Import) + .resolve("file://foo", "ext:bar", ResolutionKind::Import) .err() .map(|e| e.to_string()), Some( - "Module loading is not supported; attempted to resolve: \"file://foo\" from \"internal:bar\"" + "Module loading is not supported; attempted to resolve: \"file://foo\" from \"ext:bar\"" .to_string() ) ); @@ -2947,13 +2945,13 @@ if (import.meta.url != 'file:///main_with_code.js') throw Error(); resolve_helper( true, Rc::new(loader), - "internal:core.js", + "ext:core.js", "file://bar", ResolutionKind::Import, ) .err() .map(|e| e.to_string()), - Some("Cannot load internal module from external code".to_string()) + Some("Cannot load extension module from external code".to_string()) ); } } |