diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2019-02-12 21:14:02 -0500 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-02-18 23:04:59 -0500 |
commit | 42408febe8cdf9e30ff8d1a3bb13f4994906c53b (patch) | |
tree | 92ab3408d426f1d18a511aa16130357ed074410a /src | |
parent | 27afbd135162b435c8af22b18622656ccab12174 (diff) |
Add window.location
Diffstat (limited to 'src')
-rw-r--r-- | src/deno_dir.rs | 61 | ||||
-rw-r--r-- | src/isolate.rs | 16 | ||||
-rw-r--r-- | src/main.rs | 11 | ||||
-rw-r--r-- | src/msg.fbs | 1 | ||||
-rw-r--r-- | src/ops.rs | 3 |
5 files changed, 63 insertions, 29 deletions
diff --git a/src/deno_dir.rs b/src/deno_dir.rs index 9682d9325..1d101dd8a 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -400,14 +400,11 @@ impl DenoDir { } /// Returns (module name, local filename) - pub fn resolve_module( + pub fn resolve_module_url( self: &Self, specifier: &str, referrer: &str, - ) -> Result<(String, String), url::ParseError> { - let module_name; - let filename; - + ) -> Result<Url, url::ParseError> { let specifier = self.src_file_to_url(specifier); let mut referrer = self.src_file_to_url(referrer); @@ -422,8 +419,7 @@ impl DenoDir { referrer = referrer_path.to_str().unwrap().to_string() + "/"; } - let j: Url = if is_remote(&specifier) || Path::new(&specifier).is_absolute() - { + let j = if is_remote(&specifier) || Path::new(&specifier).is_absolute() { parse_local_or_remote(&specifier)? } else if referrer.ends_with('/') { let r = Url::from_directory_path(&referrer); @@ -437,21 +433,29 @@ impl DenoDir { let base = parse_local_or_remote(&referrer)?; base.join(specifier.as_ref())? }; + Ok(j) + } + + /// Returns (module name, local filename) + pub fn resolve_module( + self: &Self, + specifier: &str, + referrer: &str, + ) -> Result<(String, String), url::ParseError> { + let j = self.resolve_module_url(specifier, referrer)?; + let module_name = j.to_string(); + let filename; match j.scheme() { "file" => { - let p = deno_fs::normalize_path(j.to_file_path().unwrap().as_ref()); - module_name = p.clone(); - filename = p; + filename = deno_fs::normalize_path(j.to_file_path().unwrap().as_ref()); } "https" => { - module_name = j.to_string(); filename = deno_fs::normalize_path( get_cache_filename(self.deps_https.as_path(), &j).as_ref(), ) } "http" => { - module_name = j.to_string(); filename = deno_fs::normalize_path( get_cache_filename(self.deps_http.as_path(), &j).as_ref(), ) @@ -517,7 +521,7 @@ fn is_remote(module_name: &str) -> bool { } fn parse_local_or_remote(p: &str) -> Result<url::Url, url::ParseError> { - if is_remote(p) { + if is_remote(p) || p.starts_with("file:") { Url::parse(p) } else { Url::from_file_path(p).map_err(|_err| url::ParseError::IdnaError) @@ -605,6 +609,16 @@ mod tests { }; } + macro_rules! file_url { + ($path:expr) => { + if cfg!(target_os = "windows") { + concat!("file:///C:", $path) + } else { + concat!("file://", $path) + } + }; + } + #[test] fn test_get_cache_filename() { let url = Url::parse("http://example.com:1234/path/to/file.ts").unwrap(); @@ -1019,25 +1033,25 @@ mod tests { ( "./subdir/print_hello.ts", add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/006_url_imports.ts"), - add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"), + file_url!("/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"), add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/subdir/print_hello.ts"), ), ( "testdata/001_hello.js", add_root!("/Users/rld/go/src/github.com/denoland/deno/"), - add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"), + file_url!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"), add_root!("/Users/rld/go/src/github.com/denoland/deno/testdata/001_hello.js"), ), ( add_root!("/Users/rld/src/deno/hello.js"), ".", - add_root!("/Users/rld/src/deno/hello.js"), + file_url!("/Users/rld/src/deno/hello.js"), add_root!("/Users/rld/src/deno/hello.js"), ), ( add_root!("/this/module/got/imported.js"), add_root!("/that/module/did/it.js"), - add_root!("/this/module/got/imported.js"), + file_url!("/this/module/got/imported.js"), add_root!("/this/module/got/imported.js"), ), ]; @@ -1169,8 +1183,7 @@ mod tests { let specifier = "http_test.ts"; let referrer = add_root!("/Users/rld/src/deno_net/"); - let expected_module_name = - add_root!("/Users/rld/src/deno_net/http_test.ts"); + let expected_module_name = file_url!("/Users/rld/src/deno_net/http_test.ts"); let expected_filename = add_root!("/Users/rld/src/deno_net/http_test.ts"); let (module_name, filename) = @@ -1187,8 +1200,9 @@ mod tests { let cwd = std::env::current_dir().unwrap(); let expected_path = cwd.join(specifier); - let expected_module_name = deno_fs::normalize_path(&expected_path); - let expected_filename = expected_module_name.clone(); + let expected_module_name = + Url::from_file_path(&expected_path).unwrap().to_string(); + let expected_filename = deno_fs::normalize_path(&expected_path); let (module_name, filename) = deno_dir.resolve_module(specifier, ".").unwrap(); @@ -1209,8 +1223,9 @@ mod tests { let cwd = std::env::current_dir().unwrap(); let expected_path = cwd.join("..").join(specifier); - let expected_module_name = deno_fs::normalize_path(&expected_path); - let expected_filename = expected_module_name.clone(); + let expected_module_name = + Url::from_file_path(&expected_path).unwrap().to_string(); + let expected_filename = deno_fs::normalize_path(&expected_path); let (module_name, filename) = deno_dir.resolve_module(specifier, "..").unwrap(); diff --git a/src/isolate.rs b/src/isolate.rs index 8775c6f4a..661e49edd 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -100,6 +100,22 @@ impl IsolateState { } } + pub fn main_module(&self) -> Option<String> { + if self.argv.len() <= 1 { + None + } else { + let specifier = self.argv[1].clone(); + let referrer = "."; + match self.dir.resolve_module_url(&specifier, referrer) { + Ok(url) => Some(url.to_string()), + Err(e) => { + debug!("Potentially swallowed error {}", e); + None + } + } + } + } + #[cfg(test)] pub fn mock() -> Arc<IsolateState> { let argv = vec![String::from("./deno"), String::from("hello.js")]; diff --git a/src/main.rs b/src/main.rs index d3ec4b721..10ae15065 100644 --- a/src/main.rs +++ b/src/main.rs @@ -105,19 +105,18 @@ fn main() { .map_err(errors::RustOrJsError::from) .unwrap_or_else(print_err_and_exit); - // Execute input file. - if isolate.state.argv.len() > 1 { - let input_filename = isolate.state.argv[1].clone(); + // Execute main module. + if let Some(main_module) = isolate.state.main_module() { + debug!("main_module {}", main_module); isolate - .execute_mod(&input_filename, should_prefetch) + .execute_mod(&main_module, should_prefetch) .unwrap_or_else(print_err_and_exit); - if should_display_info { // Display file info and exit. Do not run file modules::print_file_info( &isolate.modules.borrow(), &isolate.state.dir, - input_filename, + main_module, ); std::process::exit(0); } diff --git a/src/msg.fbs b/src/msg.fbs index 9776bb893..da03e00a4 100644 --- a/src/msg.fbs +++ b/src/msg.fbs @@ -158,6 +158,7 @@ table StartRes { pid: uint32; argv: [string]; exec_path: string; + main_module: string; // Absolute URL. debug_flag: bool; deps_flag: bool; types_flag: bool; diff --git a/src/ops.rs b/src/ops.rs index c8a005601..8f32ebc03 100644 --- a/src/ops.rs +++ b/src/ops.rs @@ -261,12 +261,15 @@ fn op_start( let deno_version = version::DENO; let deno_version_off = builder.create_string(deno_version); + let main_module = state.main_module().map(|m| builder.create_string(&m)); + let inner = msg::StartRes::create( &mut builder, &msg::StartResArgs { cwd: Some(cwd_off), pid: std::process::id(), argv: Some(argv_off), + main_module, debug_flag: state.flags.log_debug, types_flag: state.flags.types, version_flag: state.flags.version, |