From bca5cc5041172e22ad1851c8510d6521bf70ec22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Thu, 13 Jun 2019 01:55:59 +0200 Subject: [PATCH] Move ModuleSpecifier to //core (#2509) --- Cargo.lock | 1 + cli/compiler.rs | 4 +- cli/flags.rs | 2 +- cli/import_map.rs | 2 +- cli/main.rs | 3 +- cli/ops.rs | 18 +- cli/state.rs | 19 +-- cli/worker.rs | 2 +- core/BUILD.gn | 1 + core/Cargo.toml | 1 + core/lib.rs | 2 + {cli => core}/module_specifier.rs | 13 +- core/modules.rs | 268 +++++++++++++++++------------- 13 files changed, 188 insertions(+), 148 deletions(-) rename {cli => core}/module_specifier.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 8977d27569..a9441b45aa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -205,6 +205,7 @@ dependencies = [ "log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.39 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.1.20 (registry+https://github.com/rust-lang/crates.io-index)", + "url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] diff --git a/cli/compiler.rs b/cli/compiler.rs index a33ccfcb09..de5b5c609f 100644 --- a/cli/compiler.rs +++ b/cli/compiler.rs @@ -247,7 +247,7 @@ mod tests { fn test_compile_sync() { tokio_util::init(|| { let specifier = "./tests/002_hello.ts"; - use crate::module_specifier::ModuleSpecifier; + use deno::ModuleSpecifier; let module_name = ModuleSpecifier::resolve_root(specifier) .unwrap() .to_string(); @@ -294,7 +294,7 @@ mod tests { #[test] fn test_bundle_async() { let specifier = "./tests/002_hello.ts"; - use crate::module_specifier::ModuleSpecifier; + use deno::ModuleSpecifier; let module_name = ModuleSpecifier::resolve_root(specifier) .unwrap() .to_string(); diff --git a/cli/flags.rs b/cli/flags.rs index 6d4a95f9ef..8b95aefae5 100644 --- a/cli/flags.rs +++ b/cli/flags.rs @@ -502,7 +502,7 @@ pub enum DenoSubcommand { } fn get_default_bundle_filename(source_file: &str) -> String { - use crate::module_specifier::ModuleSpecifier; + use deno::ModuleSpecifier; let url = ModuleSpecifier::resolve_root(source_file).unwrap().to_url(); let path_segments = url.path_segments().unwrap(); let last = path_segments.last().unwrap(); diff --git a/cli/import_map.rs b/cli/import_map.rs index 6f4095a908..4321cacadf 100644 --- a/cli/import_map.rs +++ b/cli/import_map.rs @@ -1,4 +1,4 @@ -use crate::module_specifier::ModuleSpecifier; +use deno::ModuleSpecifier; use indexmap::IndexMap; use serde_json::Map; use serde_json::Value; diff --git a/cli/main.rs b/cli/main.rs index 11575ef638..ae453f70fa 100644 --- a/cli/main.rs +++ b/cli/main.rs @@ -27,7 +27,6 @@ mod http_body; mod http_util; mod import_map; pub mod js_errors; -mod module_specifier; pub mod msg; pub mod msg_util; pub mod ops; @@ -46,11 +45,11 @@ pub mod worker; use crate::compiler::bundle_async; use crate::errors::RustOrJsError; -use crate::module_specifier::ModuleSpecifier; use crate::progress::Progress; use crate::state::ThreadSafeState; use crate::worker::Worker; use deno::v8_set_flags; +use deno::ModuleSpecifier; use flags::DenoFlags; use flags::DenoSubcommand; use futures::future; diff --git a/cli/ops.rs b/cli/ops.rs index e9fd48d0db..e8fa47aada 100644 --- a/cli/ops.rs +++ b/cli/ops.rs @@ -10,7 +10,6 @@ use crate::fs as deno_fs; use crate::http_util; use crate::js_errors::apply_source_map; use crate::js_errors::JSErrorColor; -use crate::module_specifier::ModuleSpecifier; use crate::msg; use crate::msg_util; use crate::rand; @@ -29,6 +28,7 @@ use crate::worker::Worker; use deno::js_check; use deno::Buf; use deno::JSError; +use deno::ModuleSpecifier; use deno::Op; use deno::PinnedBuf; use flatbuffers::FlatBufferBuilder; @@ -505,15 +505,13 @@ fn op_fetch_module_meta_data( // import map - why it is not always resolved? Eg. "bad-module.ts" will return NotFound // error whilst it should return RelativeUrlWithCannotBeABaseBase error let resolved_specifier = match &state.import_map { - Some(import_map) => { - match import_map.resolve(specifier, referrer) { - Ok(result) => match result { - Some(module_specifier) => module_specifier.to_string(), - None => specifier.to_string(), - }, - Err(err) => panic!("error resolving using import map: {:?}", err), // TODO: this should be coerced to DenoError - } - } + Some(import_map) => match import_map.resolve(specifier, referrer) { + Ok(result) => match result { + Some(module_specifier) => module_specifier.to_string(), + None => specifier.to_string(), + }, + Err(err) => return odd_future(DenoError::from(err)), + }, None => specifier.to_string(), }; diff --git a/cli/state.rs b/cli/state.rs index df1bf1cc51..f5eb8ae7ab 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -7,7 +7,6 @@ use crate::errors::DenoResult; use crate::flags; use crate::global_timer::GlobalTimer; use crate::import_map::ImportMap; -use crate::module_specifier::ModuleSpecifier; use crate::msg; use crate::ops; use crate::permissions::DenoPermissions; @@ -17,6 +16,7 @@ use crate::resources::ResourceId; use crate::worker::Worker; use deno::Buf; use deno::Loader; +use deno::ModuleSpecifier; use deno::Op; use deno::PinnedBuf; use futures::future::Either; @@ -157,28 +157,27 @@ impl Loader for ThreadSafeState { specifier: &str, referrer: &str, is_root: bool, - ) -> Result { + ) -> Result { if !is_root { if let Some(import_map) = &self.import_map { let result = import_map.resolve(specifier, referrer)?; if result.is_some() { - return Ok(result.unwrap().to_string()); + return Ok(result.unwrap()); } } } - let module_specifier = - ModuleSpecifier::resolve(specifier, referrer).map_err(DenoError::from)?; - Ok(module_specifier.to_string()) + ModuleSpecifier::resolve(specifier, referrer).map_err(DenoError::from) } /// Given an absolute url, load its source code. - fn load(&self, url: &str) -> Box> { + fn load( + &self, + module_specifier: &ModuleSpecifier, + ) -> Box> { self.metrics.resolve_count.fetch_add(1, Ordering::SeqCst); - let module_specifier = ModuleSpecifier::resolve_root(url) - .expect("should already been properly resolved"); Box::new( - fetch_module_meta_data_and_maybe_compile_async(self, &module_specifier) + fetch_module_meta_data_and_maybe_compile_async(self, module_specifier) .map_err(|err| { eprintln!("{}", err); err diff --git a/cli/worker.rs b/cli/worker.rs index f11aa93e9c..9170a293fa 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -2,11 +2,11 @@ use crate::errors::DenoError; use crate::errors::RustOrJsError; use crate::js_errors; -use crate::module_specifier::ModuleSpecifier; use crate::state::ThreadSafeState; use crate::tokio_util; use deno; use deno::JSError; +use deno::ModuleSpecifier; use deno::StartupData; use futures::Async; use futures::Future; diff --git a/core/BUILD.gn b/core/BUILD.gn index 3b43e90374..bf9d910c07 100644 --- a/core/BUILD.gn +++ b/core/BUILD.gn @@ -23,6 +23,7 @@ main_extern_rlib = [ "libc", "serde_json", "log", + "url", ] rust_rlib("deno") { diff --git a/core/Cargo.toml b/core/Cargo.toml index 301606321d..c86248a454 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -19,6 +19,7 @@ lazy_static = "1.3.0" libc = "0.2.55" log = "0.4.6" serde_json = "1.0.39" +url = "1.7.2" [[example]] name = "deno_core_http_bench" diff --git a/core/lib.rs b/core/lib.rs index 10f12815e9..88c0fe1d19 100644 --- a/core/lib.rs +++ b/core/lib.rs @@ -8,6 +8,7 @@ mod flags; mod isolate; mod js_errors; mod libdeno; +mod module_specifier; mod modules; mod shared_queue; @@ -16,6 +17,7 @@ pub use crate::isolate::*; pub use crate::js_errors::*; pub use crate::libdeno::deno_mod; pub use crate::libdeno::PinnedBuf; +pub use crate::module_specifier::ModuleSpecifier; pub use crate::modules::*; pub fn v8_version() -> &'static str { diff --git a/cli/module_specifier.rs b/core/module_specifier.rs similarity index 99% rename from cli/module_specifier.rs rename to core/module_specifier.rs index 475211289a..1bdd5beee2 100644 --- a/cli/module_specifier.rs +++ b/core/module_specifier.rs @@ -9,6 +9,7 @@ impl ModuleSpecifier { pub fn to_url(&self) -> Url { self.0.clone() } + /// Resolves module using this algorithm: /// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier pub fn resolve( @@ -62,18 +63,18 @@ impl ModuleSpecifier { } } -impl From for ModuleSpecifier { - fn from(url: Url) -> Self { - ModuleSpecifier(url) - } -} - impl fmt::Display for ModuleSpecifier { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) } } +impl From for ModuleSpecifier { + fn from(url: Url) -> Self { + ModuleSpecifier(url) + } +} + impl PartialEq for ModuleSpecifier { fn eq(&self, other: &String) -> bool { &self.to_string() == other diff --git a/core/modules.rs b/core/modules.rs index fbfdb0b072..5a0e337986 100644 --- a/core/modules.rs +++ b/core/modules.rs @@ -9,6 +9,7 @@ use crate::isolate::Isolate; use crate::js_errors::JSError; use crate::libdeno::deno_mod; +use crate::module_specifier::ModuleSpecifier; use futures::Async; use futures::Future; use futures::Poll; @@ -48,10 +49,13 @@ pub trait Loader: Send + Sync { specifier: &str, referrer: &str, is_root: bool, - ) -> Result; + ) -> Result; - /// Given an absolute url, load its source code. - fn load(&self, url: &str) -> Box>; + /// Given ModuleSpecifier, load its source code. + fn load( + &self, + module_specifier: &ModuleSpecifier, + ) -> Box>; } struct PendingLoad { @@ -104,12 +108,13 @@ impl RecursiveLoad { parent_id: Option, ) -> Result { let is_root = parent_id.is_none(); - let url = self.loader.resolve(specifier, referrer, is_root)?; + let module_specifier = self.loader.resolve(specifier, referrer, is_root)?; + let module_name = module_specifier.to_string(); if !is_root { { let mut m = self.modules.lock().unwrap(); - m.add_child(parent_id.unwrap(), &url); + m.add_child(parent_id.unwrap(), &module_name); } } @@ -118,22 +123,22 @@ impl RecursiveLoad { // Only short circuit after add_child(). // This impacts possible conditions in #A. let modules = self.modules.lock().unwrap(); - if modules.is_registered(&url) { - return Ok(url); + if modules.is_registered(&module_name) { + return Ok(module_name); } } - if !self.is_pending.contains(&url) { - self.is_pending.insert(url.clone()); - let source_code_info_future = { self.loader.load(&url) }; + if !self.is_pending.contains(&module_name) { + self.is_pending.insert(module_name.to_string()); + let source_code_info_future = { self.loader.load(&module_specifier) }; self.pending.push(PendingLoad { - url: url.clone(), + url: module_name.to_string(), source_code_info_future, is_root, }); } - Ok(url) + Ok(module_name) } } @@ -254,10 +259,9 @@ impl Future for RecursiveLoad { |specifier: &str, referrer_id: deno_mod| -> deno_mod { let modules = self.modules.lock().unwrap(); let referrer = modules.get_name(referrer_id).unwrap(); - // TODO(bartlomieju): there must be a better way - let is_root = referrer == "."; - match self.loader.resolve(specifier, &referrer, is_root) { - Ok(url) => match modules.get_id(&url) { + // this callback is only called for non-root modules + match self.loader.resolve(specifier, &referrer, false) { + Ok(specifier) => match modules.get_id(&specifier.to_string()) { Some(id) => id, None => 0, }, @@ -557,22 +561,25 @@ mod tests { fn mock_source_code(url: &str) -> Option<(&'static str, &'static str)> { // (code, real_module_name) - match url { - "a.js" => Some((A_SRC, "a.js")), - "b.js" => Some((B_SRC, "b.js")), - "c.js" => Some((C_SRC, "c.js")), - "d.js" => Some((D_SRC, "d.js")), - "circular1.js" => Some((CIRCULAR1_SRC, "circular1.js")), - "circular2.js" => Some((CIRCULAR2_SRC, "circular2.js")), - "circular3.js" => Some((CIRCULAR3_SRC, "circular3.js")), - "redirect1.js" => Some((REDIRECT1_SRC, "redirect1.js")), - // pretend redirect - "./redirect2.js" => Some((REDIRECT2_SRC, "./dir/redirect2.js")), - "./dir/redirect3.js" => Some((REDIRECT3_SRC, "./redirect3.js")), - "slow.js" => Some((SLOW_SRC, "slow.js")), - "never_ready.js" => Some(("should never be loaded", "never_ready.js")), - "main.js" => Some((MAIN_SRC, "main.js")), - "bad_import.js" => Some((BAD_IMPORT_SRC, "bad_import.js")), + let spec: Vec<&str> = url.split("file://").collect(); + match spec[1] { + "/a.js" => Some((A_SRC, "file:///a.js")), + "/b.js" => Some((B_SRC, "file:///b.js")), + "/c.js" => Some((C_SRC, "file:///c.js")), + "/d.js" => Some((D_SRC, "file:///d.js")), + "/circular1.js" => Some((CIRCULAR1_SRC, "file:///circular1.js")), + "/circular2.js" => Some((CIRCULAR2_SRC, "file:///circular2.js")), + "/circular3.js" => Some((CIRCULAR3_SRC, "file:///circular3.js")), + "/redirect1.js" => Some((REDIRECT1_SRC, "file:///redirect1.js")), + // pretend redirect - real module name is different than one requested + "/redirect2.js" => Some((REDIRECT2_SRC, "file:///dir/redirect2.js")), + "/dir/redirect3.js" => Some((REDIRECT3_SRC, "file:///redirect3.js")), + "/slow.js" => Some((SLOW_SRC, "file:///slow.js")), + "/never_ready.js" => { + Some(("should never be loaded", "file:///never_ready.js")) + } + "/main.js" => Some((MAIN_SRC, "file:///main.js")), + "/bad_import.js" => Some((BAD_IMPORT_SRC, "file:///bad_import.js")), _ => None, } } @@ -606,8 +613,8 @@ mod tests { fn poll(&mut self) -> Poll { self.counter += 1; - if self.url == "never_ready.js" - || (self.url == "slow.js" && self.counter < 2) + if self.url == "file:///never_ready.js" + || (self.url == "file:///slow.js" && self.counter < 2) { return Ok(Async::NotReady); } @@ -629,72 +636,68 @@ mod tests { specifier: &str, referrer: &str, _is_root: bool, - ) -> Result { - eprintln!(">> RESOLVING, S: {}, R: {}", specifier, referrer); - let output_specifier = - if specifier.starts_with("./") && referrer.starts_with("./") { - // Special fake path resolving logic (for redirect test) - // if both started with "./" - eprintln!(">> SPECIAL!"); - let prefix = { - let mut iter = referrer.rsplitn(2, '/'); - let _ = iter.next(); - iter.next().unwrap() - }; - let suffix = { - let mut iter = specifier.splitn(2, '/'); - let _ = iter.next(); - iter.next().unwrap() - }; - format!("{}/{}", &prefix, &suffix) - } else { - specifier.to_owned() - }; + ) -> Result { + let referrer = if referrer == "." { + "file:///" + } else { + referrer + }; - if mock_source_code(&output_specifier).is_some() { + eprintln!(">> RESOLVING, S: {}, R: {}", specifier, referrer); + + let output_specifier = match ModuleSpecifier::resolve(specifier, referrer) + { + Ok(specifier) => specifier, + Err(_e) => return Err(MockError::ResolveErr), + }; + + if mock_source_code(&output_specifier.to_string()).is_some() { Ok(output_specifier) } else { Err(MockError::ResolveErr) } } - fn load(&self, url: &str) -> Box> { + fn load( + &self, + module_specifier: &ModuleSpecifier, + ) -> Box> { let mut loads = self.loads.lock().unwrap(); - loads.push(url.to_string()); - let url = url.to_string(); + loads.push(module_specifier.to_string()); + let url = module_specifier.to_string(); Box::new(DelayedSourceCodeFuture { url, counter: 0 }) } } const A_SRC: &str = r#" - import { b } from "b.js"; - import { c } from "c.js"; + import { b } from "/b.js"; + import { c } from "/c.js"; if (b() != 'b') throw Error(); if (c() != 'c') throw Error(); if (!import.meta.main) throw Error(); - if (import.meta.url != 'a.js') throw Error(); + if (import.meta.url != 'file:///a.js') throw Error(); "#; const B_SRC: &str = r#" - import { c } from "c.js"; + import { c } from "/c.js"; if (c() != 'c') throw Error(); export function b() { return 'b'; } if (import.meta.main) throw Error(); - if (import.meta.url != 'b.js') throw Error(); + if (import.meta.url != 'file:///b.js') throw Error(); "#; const C_SRC: &str = r#" - import { d } from "d.js"; + import { d } from "/d.js"; export function c() { return 'c'; } if (d() != 'd') throw Error(); if (import.meta.main) throw Error(); - if (import.meta.url != 'c.js') throw Error(); + if (import.meta.url != 'file:///c.js') throw Error(); "#; const D_SRC: &str = r#" export function d() { return 'd'; } if (import.meta.main) throw Error(); - if (import.meta.url != 'd.js') throw Error(); + if (import.meta.url != 'file:///d.js') throw Error(); "#; #[test] @@ -706,7 +709,7 @@ mod tests { let isolate_ = isolate.clone(); let loads = loader.loads.clone(); let mut recursive_load = - RecursiveLoad::new("a.js", loader, isolate, modules); + RecursiveLoad::new("/a.js", loader, isolate, modules); let result = recursive_load.poll(); assert!(result.is_ok()); @@ -715,21 +718,38 @@ mod tests { js_check(isolate.mod_evaluate(a_id)); let l = loads.lock().unwrap(); - assert_eq!(l.to_vec(), vec!["a.js", "b.js", "c.js", "d.js"]); + assert_eq!( + l.to_vec(), + vec![ + "file:///a.js", + "file:///b.js", + "file:///c.js", + "file:///d.js" + ] + ); let modules = modules_.lock().unwrap(); - assert_eq!(modules.get_id("a.js"), Some(a_id)); - let b_id = modules.get_id("b.js").unwrap(); - let c_id = modules.get_id("c.js").unwrap(); - let d_id = modules.get_id("d.js").unwrap(); + assert_eq!(modules.get_id("file:///a.js"), Some(a_id)); + let b_id = modules.get_id("file:///b.js").unwrap(); + let c_id = modules.get_id("file:///c.js").unwrap(); + let d_id = modules.get_id("file:///d.js").unwrap(); assert_eq!( modules.get_children(a_id), - Some(&vec!["b.js".to_string(), "c.js".to_string()]) + Some(&vec![ + "file:///b.js".to_string(), + "file:///c.js".to_string() + ]) + ); + assert_eq!( + modules.get_children(b_id), + Some(&vec!["file:///c.js".to_string()]) + ); + assert_eq!( + modules.get_children(c_id), + Some(&vec!["file:///d.js".to_string()]) ); - assert_eq!(modules.get_children(b_id), Some(&vec!["c.js".to_string()])); - assert_eq!(modules.get_children(c_id), Some(&vec!["d.js".to_string()])); assert_eq!(modules.get_children(d_id), Some(&vec![])); } else { unreachable!(); @@ -737,18 +757,18 @@ mod tests { } const CIRCULAR1_SRC: &str = r#" - import "circular2.js"; + import "/circular2.js"; Deno.core.print("circular1"); "#; const CIRCULAR2_SRC: &str = r#" - import "circular3.js"; + import "/circular3.js"; Deno.core.print("circular2"); "#; const CIRCULAR3_SRC: &str = r#" - import "circular1.js"; - import "circular2.js"; + import "/circular1.js"; + import "/circular2.js"; Deno.core.print("circular3"); "#; @@ -761,7 +781,7 @@ mod tests { let modules_ = modules.clone(); let loads = loader.loads.clone(); let mut recursive_load = - RecursiveLoad::new("circular1.js", loader, isolate, modules); + RecursiveLoad::new("/circular1.js", loader, isolate, modules); let result = recursive_load.poll(); assert!(result.is_ok()); @@ -772,31 +792,35 @@ mod tests { let l = loads.lock().unwrap(); assert_eq!( l.to_vec(), - vec!["circular1.js", "circular2.js", "circular3.js"] + vec![ + "file:///circular1.js", + "file:///circular2.js", + "file:///circular3.js" + ] ); let modules = modules_.lock().unwrap(); - assert_eq!(modules.get_id("circular1.js"), Some(circular1_id)); - let circular2_id = modules.get_id("circular2.js").unwrap(); + assert_eq!(modules.get_id("file:///circular1.js"), Some(circular1_id)); + let circular2_id = modules.get_id("file:///circular2.js").unwrap(); assert_eq!( modules.get_children(circular1_id), - Some(&vec!["circular2.js".to_string()]) + Some(&vec!["file:///circular2.js".to_string()]) ); assert_eq!( modules.get_children(circular2_id), - Some(&vec!["circular3.js".to_string()]) + Some(&vec!["file:///circular3.js".to_string()]) ); - assert!(modules.get_id("circular3.js").is_some()); - let circular3_id = modules.get_id("circular3.js").unwrap(); + assert!(modules.get_id("file:///circular3.js").is_some()); + let circular3_id = modules.get_id("file:///circular3.js").unwrap(); assert_eq!( modules.get_children(circular3_id), Some(&vec![ - "circular1.js".to_string(), - "circular2.js".to_string() + "file:///circular1.js".to_string(), + "file:///circular2.js".to_string() ]) ); } else { @@ -827,7 +851,7 @@ mod tests { let modules_ = modules.clone(); let loads = loader.loads.clone(); let mut recursive_load = - RecursiveLoad::new("redirect1.js", loader, isolate, modules); + RecursiveLoad::new("/redirect1.js", loader, isolate, modules); let result = recursive_load.poll(); assert!(result.is_ok()); @@ -837,22 +861,29 @@ mod tests { let l = loads.lock().unwrap(); assert_eq!( l.to_vec(), - vec!["redirect1.js", "./redirect2.js", "./dir/redirect3.js"] + vec![ + "file:///redirect1.js", + "file:///redirect2.js", + "file:///dir/redirect3.js" + ] ); let modules = modules_.lock().unwrap(); - assert_eq!(modules.get_id("redirect1.js"), Some(redirect1_id)); + assert_eq!(modules.get_id("file:///redirect1.js"), Some(redirect1_id)); - let redirect2_id = modules.get_id("./dir/redirect2.js").unwrap(); - assert!(modules.is_alias("./redirect2.js")); - assert!(!modules.is_alias("./dir/redirect2.js")); - assert_eq!(modules.get_id("./redirect2.js").unwrap(), redirect2_id); + let redirect2_id = modules.get_id("file:///dir/redirect2.js").unwrap(); + assert!(modules.is_alias("file:///redirect2.js")); + assert!(!modules.is_alias("file:///dir/redirect2.js")); + assert_eq!(modules.get_id("file:///redirect2.js"), Some(redirect2_id)); - let redirect3_id = modules.get_id("./redirect3.js").unwrap(); - assert!(modules.is_alias("./dir/redirect3.js")); - assert!(!modules.is_alias("./redirect3.js")); - assert_eq!(modules.get_id("./dir/redirect3.js").unwrap(), redirect3_id); + let redirect3_id = modules.get_id("file:///redirect3.js").unwrap(); + assert!(modules.is_alias("file:///dir/redirect3.js")); + assert!(!modules.is_alias("file:///redirect3.js")); + assert_eq!( + modules.get_id("file:///dir/redirect3.js"), + Some(redirect3_id) + ); } else { unreachable!(); } @@ -861,17 +892,17 @@ mod tests { // main.js const MAIN_SRC: &str = r#" // never_ready.js never loads. - import "never_ready.js"; + import "/never_ready.js"; // slow.js resolves after one tick. - import "slow.js"; + import "/slow.js"; "#; // slow.js const SLOW_SRC: &str = r#" // Circular import of never_ready.js // Does this trigger two Loader calls? It shouldn't. - import "never_ready.js"; - import "a.js"; + import "/never_ready.js"; + import "/a.js"; "#; #[test] @@ -881,7 +912,7 @@ mod tests { let modules = loader.modules.clone(); let loads = loader.loads.clone(); let mut recursive_load = - RecursiveLoad::new("main.js", loader, isolate, modules); + RecursiveLoad::new("/main.js", loader, isolate, modules); let result = recursive_load.poll(); assert!(result.is_ok()); @@ -889,7 +920,14 @@ mod tests { { let l = loads.lock().unwrap(); - assert_eq!(l.to_vec(), vec!["main.js", "never_ready.js", "slow.js"]); + assert_eq!( + l.to_vec(), + vec![ + "file:///main.js", + "file:///never_ready.js", + "file:///slow.js" + ] + ); } for _ in 0..10 { @@ -900,13 +938,13 @@ mod tests { assert_eq!( l.to_vec(), vec![ - "main.js", - "never_ready.js", - "slow.js", - "a.js", - "b.js", - "c.js", - "d.js" + "file:///main.js", + "file:///never_ready.js", + "file:///slow.js", + "file:///a.js", + "file:///b.js", + "file:///c.js", + "file:///d.js" ] ); } @@ -923,7 +961,7 @@ mod tests { let isolate = loader.isolate.clone(); let modules = loader.modules.clone(); let mut recursive_load = - RecursiveLoad::new("bad_import.js", loader, isolate, modules); + RecursiveLoad::new("/bad_import.js", loader, isolate, modules); let result = recursive_load.poll(); assert!(result.is_err()); let either_err = result.err().unwrap();