Skip to content
Permalink
Browse files

clean up

  • Loading branch information...
ry committed Apr 15, 2019
1 parent a1f9fc6 commit b7fed32f29cbf9f2007a782b9b91db3f7dbf1f3a
Showing with 61 additions and 60 deletions.
  1. +0 −52 cli/deno_dir.rs
  2. +2 −1 cli/main.rs
  3. +2 −2 cli/ops.rs
  4. +53 −3 cli/worker.rs
  5. +4 −2 core/modules.rs
@@ -853,58 +853,6 @@ fn save_source_code_headers(
}
}

// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
// TODO(ry) Add tests.
// TODO(ry) Move this to core?
pub fn resolve_module2(
specifier: &str,
base: &str,
) -> Result<String, url::ParseError> {
// 1. Apply the URL parser to specifier. If the result is not failure, return
// the result.
// let specifier = parse_local_or_remote(specifier)?.to_string();
if let Ok(specifier_url) = Url::parse(specifier) {
return Ok(specifier_url.to_string());
}

// 2. If specifier does not start with the character U+002F SOLIDUS (/), the
// two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the
// three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F
// SOLIDUS (../), return failure.
if !specifier.starts_with("/")
&& !specifier.starts_with("./")
&& !specifier.starts_with("../")
{
// TODO(ry) This is (probably) not the correct error to return here.
return Err(url::ParseError::RelativeUrlWithCannotBeABaseBase);
}

// 3. Return the result of applying the URL parser to specifier with base URL
// as the base URL.
let base_url = Url::parse(base)?;
let u = base_url.join(&specifier)?;
Ok(u.to_string())
}

/// Takes a string representing a path or URL to a module, but of the type
/// passed through the command-line interface for the main module. This is
/// slightly different than specifiers used in import statements: "foo.js" for
/// example is allowed here, whereas in import statements a leading "./" is
/// required ("./foo.js"). This function is aware of the current working
/// directory and returns an absolute URL.
pub fn root_specifier_to_url(
root_specifier: &str,
) -> Result<Url, url::ParseError> {
let maybe_url = Url::parse(root_specifier);
if let Ok(url) = maybe_url {
Ok(url)
} else {
let cwd = std::env::current_dir().unwrap();
let base = Url::from_directory_path(cwd).unwrap();
base.join(root_specifier)
}
}

#[cfg(test)]
mod tests {
use super::*;
@@ -36,6 +36,7 @@ pub mod worker;

use crate::errors::RustOrJsError;
use crate::state::ThreadSafeState;
use crate::worker::root_specifier_to_url;
use crate::worker::Worker;
use futures::lazy;
use futures::Future;
@@ -183,7 +184,7 @@ fn main() {
js_check(worker.execute("denoMain()"));
debug!("main_module {}", main_module);

let main_url = deno_dir::root_specifier_to_url(&main_module).unwrap();
let main_url = root_specifier_to_url(&main_module).unwrap();

worker
.execute_mod_async(&main_url, should_prefetch)
@@ -1,7 +1,6 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use atty;
use crate::ansi;
use crate::deno_dir;
use crate::errors;
use crate::errors::{DenoError, DenoResult, ErrorKind};
use crate::fs as deno_fs;
@@ -20,6 +19,7 @@ use crate::state::ThreadSafeState;
use crate::tokio_util;
use crate::tokio_write;
use crate::version;
use crate::worker::root_specifier_to_url;
use crate::worker::Worker;
use deno::deno_buf;
use deno::js_check;
@@ -1881,7 +1881,7 @@ fn op_create_worker(
js_check(worker.execute("workerMain()"));

let specifier_url =
deno_dir::root_specifier_to_url(specifier).map_err(DenoError::from)?;
root_specifier_to_url(specifier).map_err(DenoError::from)?;

// TODO(ry) Use execute_mod_async here.
let result = worker.execute_mod(&specifier_url, false);
@@ -101,12 +101,64 @@ impl Worker {
}
}

// https://html.spec.whatwg.org/multipage/webappapis.html#resolve-a-module-specifier
// TODO(ry) Add tests.
// TODO(ry) Move this to core?
pub fn resolve_module_spec(
specifier: &str,
base: &str,
) -> Result<String, url::ParseError> {
// 1. Apply the URL parser to specifier. If the result is not failure, return
// the result.
// let specifier = parse_local_or_remote(specifier)?.to_string();
if let Ok(specifier_url) = Url::parse(specifier) {
return Ok(specifier_url.to_string());
}

// 2. If specifier does not start with the character U+002F SOLIDUS (/), the
// two-character sequence U+002E FULL STOP, U+002F SOLIDUS (./), or the
// three-character sequence U+002E FULL STOP, U+002E FULL STOP, U+002F
// SOLIDUS (../), return failure.
if !specifier.starts_with("/")
&& !specifier.starts_with("./")
&& !specifier.starts_with("../")
{
// TODO(ry) This is (probably) not the correct error to return here.
return Err(url::ParseError::RelativeUrlWithCannotBeABaseBase);
}

// 3. Return the result of applying the URL parser to specifier with base URL
// as the base URL.
let base_url = Url::parse(base)?;
let u = base_url.join(&specifier)?;
Ok(u.to_string())
}

/// Takes a string representing a path or URL to a module, but of the type
/// passed through the command-line interface for the main module. This is
/// slightly different than specifiers used in import statements: "foo.js" for
/// example is allowed here, whereas in import statements a leading "./" is
/// required ("./foo.js"). This function is aware of the current working
/// directory and returns an absolute URL.
pub fn root_specifier_to_url(
root_specifier: &str,
) -> Result<Url, url::ParseError> {
let maybe_url = Url::parse(root_specifier);
if let Ok(url) = maybe_url {
Ok(url)
} else {
let cwd = std::env::current_dir().unwrap();
let base = Url::from_directory_path(cwd).unwrap();
base.join(root_specifier)
}
}

impl Loader for Worker {
type Dispatch = ThreadSafeState;
type Error = DenoError;

fn resolve(specifier: &str, referrer: &str) -> Result<String, Self::Error> {
crate::deno_dir::resolve_module2(specifier, referrer)
resolve_module_spec(specifier, referrer)
.map_err(|url_err| DenoError::from(url_err))
}

@@ -350,8 +402,6 @@ mod tests {
})
}

use crate::deno_dir::root_specifier_to_url;

#[test]
fn execute_mod_resolve_error() {
// "foo" is not a vailid module specifier so this should return an error.
@@ -38,11 +38,13 @@ pub trait Loader {
) -> (&'b mut Isolate<Self::Dispatch>, &'c mut Modules);

fn isolate<'a: 'b, 'b>(&'a mut self) -> &'b mut Isolate<Self::Dispatch> {
self.isolate_and_modules().0
let (isolate, _) = self.isolate_and_modules();
isolate
}

fn modules<'a: 'b, 'b>(&'a mut self) -> &'b mut Modules {
self.isolate_and_modules().1
let (_, modules) = self.isolate_and_modules();
modules
}
}

0 comments on commit b7fed32

Please sign in to comment.
You can’t perform that action at this time.