Skip to content
Permalink
Browse files

execute_mod should take a URL instead of string

  • Loading branch information...
ry committed Apr 15, 2019
1 parent a167233 commit 4bf4a57f2540aa207e893bd2c4baf3593455a393
Showing with 47 additions and 16 deletions.
  1. +19 −0 cli/deno_dir.rs
  2. +4 −1 cli/main.rs
  3. +7 −1 cli/ops.rs
  4. +17 −14 cli/worker.rs
@@ -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::*;
@@ -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
@@ -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;
@@ -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();
@@ -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
@@ -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 {
@@ -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.
@@ -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);
@@ -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);
@@ -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());
}

@@ -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.
You can’t perform that action at this time.