Skip to content

Commit

Permalink
execute_mod should take a URL instead of string
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Apr 15, 2019
1 parent a167233 commit 4bf4a57
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
19 changes: 19 additions & 0 deletions cli/deno_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -896,6 +896,25 @@ pub fn resolve_module2(
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::*;
Expand Down
5 changes: 4 additions & 1 deletion cli/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,11 @@ fn main() {
// Setup runtime.
js_check(worker.execute("denoMain()"));
debug!("main_module {}", main_module);

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

worker
.execute_mod_async(&main_module, should_prefetch)
.execute_mod_async(&main_url, should_prefetch)
.and_then(move |worker| {
if should_display_info {
// Display file info and exit. Do not run file
Expand Down
8 changes: 7 additions & 1 deletion cli/ops.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
Expand Down Expand Up @@ -1878,8 +1879,13 @@ fn op_create_worker(
Worker::new(name, startup_data::deno_isolate_init(), child_state);
js_check(worker.execute("denoMain()"));
js_check(worker.execute("workerMain()"));

// TODO(ry) don't use unwrap here, map into DenoError
let specifier_url = deno_dir::root_specifier_to_url(specifier).unwrap();

let result = worker.execute_mod(&specifier_url, false);

// TODO(ry) Use execute_mod_async here.
let result = worker.execute_mod(specifier, false);
match result {
Ok(worker) => {
let mut workers_tl = parent_state.workers.lock().unwrap();
Expand Down
31 changes: 17 additions & 14 deletions cli/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use futures::future::Either;
use futures::Async;
use futures::Future;
use std::sync::atomic::Ordering;
use url::Url;

/// Wraps deno::Isolate to provide source maps, ops for the CLI, and
/// high-level module loading
Expand Down Expand Up @@ -57,10 +58,10 @@ impl Worker {
/// Consumes worker. Executes the provided JavaScript module.
pub fn execute_mod_async(
self,
js_filename: &str,
js_url: &Url,
is_prefetch: bool,
) -> impl Future<Item = Self, Error = (RustOrJsError, Self)> {
let recursive_load = deno::RecursiveLoad::new(js_filename, self);
let recursive_load = deno::RecursiveLoad::new(js_url.as_str(), self);
recursive_load.and_then(
move |(id, mut self_)| -> Result<Self, (deno::Either<DenoError>, Self)> {
if is_prefetch {
Expand Down Expand Up @@ -88,10 +89,10 @@ impl Worker {
/// Consumes worker. Executes the provided JavaScript module.
pub fn execute_mod(
self,
js_filename: &str,
js_url: &Url,
is_prefetch: bool,
) -> Result<Self, (RustOrJsError, Self)> {
tokio_util::block_on(self.execute_mod_async(js_filename, is_prefetch))
tokio_util::block_on(self.execute_mod_async(js_url, is_prefetch))
}

/// Applies source map to the error.
Expand Down Expand Up @@ -212,16 +213,16 @@ mod tests {
let filename = std::env::current_dir()
.unwrap()
.join("tests/esm_imports_a.js");
let filename = filename.to_str().unwrap().to_string();
let js_url = Url::from_file_path(filename).unwrap();

let argv = vec![String::from("./deno"), filename.clone()];
let argv = vec![String::from("./deno"), js_url.to_string()];
let (flags, rest_argv) = flags::set_flags(argv).unwrap();

let state = ThreadSafeState::new(flags, rest_argv, op_selector_std);
let state_ = state.clone();
tokio_util::run(lazy(move || {
let worker = Worker::new("TEST".to_string(), StartupData::None, state);
let result = worker.execute_mod(&filename, false);
let result = worker.execute_mod(&js_url, false);
let worker = match result {
Err((err, worker)) => {
eprintln!("execute_mod err {:?}", err);
Expand All @@ -239,16 +240,16 @@ mod tests {
#[test]
fn execute_mod_circular() {
let filename = std::env::current_dir().unwrap().join("tests/circular1.js");
let filename = filename.to_str().unwrap().to_string();
let js_url = Url::from_file_path(filename).unwrap();

let argv = vec![String::from("./deno"), filename.clone()];
let argv = vec![String::from("./deno"), js_url.to_string()];
let (flags, rest_argv) = flags::set_flags(argv).unwrap();

let state = ThreadSafeState::new(flags, rest_argv, op_selector_std);
let state_ = state.clone();
tokio_util::run(lazy(move || {
let worker = Worker::new("TEST".to_string(), StartupData::None, state);
let result = worker.execute_mod(&filename, false);
let result = worker.execute_mod(&js_url, false);
let worker = match result {
Err((err, worker)) => {
eprintln!("execute_mod err {:?}", err);
Expand Down Expand Up @@ -357,11 +358,14 @@ 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.
let worker = create_test_worker();
let result = worker.execute_mod_async("foo", false).wait();
let js_url = root_specifier_to_url("does-not-exist").unwrap();
let result = worker.execute_mod_async(&js_url, false).wait();
assert!(result.is_err());
}

Expand All @@ -370,9 +374,8 @@ mod tests {
// This assumes cwd is project root (an assumption made throughout the
// tests).
let worker = create_test_worker();
let result = worker
.execute_mod_async("./tests/002_hello.ts", false)
.wait();
let js_url = root_specifier_to_url("./tests/002_hello.ts").unwrap();
let result = worker.execute_mod_async(&js_url, false).wait();
assert!(result.is_ok());
}
}

0 comments on commit 4bf4a57

Please sign in to comment.