Skip to content

Commit

Permalink
Add deps to --info output. (denoland#1720)
Browse files Browse the repository at this point in the history
Move module stuff into its own file.
  • Loading branch information
ry committed Feb 9, 2019
1 parent 9ab0338 commit 99ce807
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 81 deletions.
22 changes: 0 additions & 22 deletions src/deno_dir.rs
Expand Up @@ -447,28 +447,6 @@ impl DenoDir {
debug!("module_name: {}, filename: {}", module_name, filename);
Ok((module_name, filename))
}

pub fn print_file_info(self: &Self, filename: String) {
let maybe_out = self.code_fetch(&filename, ".");
if maybe_out.is_err() {
println!("{}", maybe_out.unwrap_err());
return;
}
let out = maybe_out.unwrap();

println!("local: {}", &(out.filename));
println!("type: {}", msg::enum_name_media_type(out.media_type));
if out.maybe_output_code_filename.is_some() {
println!(
"compiled: {}",
out.maybe_output_code_filename.as_ref().unwrap(),
);
}
if out.maybe_source_map_filename.is_some() {
println!("map: {}", out.maybe_source_map_filename.as_ref().unwrap());
}
// TODO print deps.
}
}

impl SourceMapGetter for DenoDir {
Expand Down
73 changes: 19 additions & 54 deletions src/isolate.rs
Expand Up @@ -14,17 +14,17 @@ use crate::errors::RustOrJsError;
use crate::flags;
use crate::js_errors::JSError;
use crate::libdeno;
use crate::modules::Modules;
use crate::msg;
use crate::permissions::DenoPermissions;
use crate::tokio_util;

use futures::sync::mpsc as async_mpsc;
use futures::Future;
use libc::c_char;
use libc::c_void;
use std;
use std::cell::Cell;
use std::collections::HashMap;
use std::cell::RefCell;
use std::env;
use std::ffi::CStr;
use std::ffi::CString;
Expand Down Expand Up @@ -52,19 +52,14 @@ pub type Dispatch =
fn(isolate: &Isolate, buf: libdeno::deno_buf, data_buf: libdeno::deno_buf)
-> (bool, Box<Op>);

pub struct ModuleInfo {
name: String,
}

pub struct Isolate {
libdeno_isolate: *const libdeno::isolate,
dispatch: Dispatch,
rx: mpsc::Receiver<(i32, Buf)>,
tx: mpsc::Sender<(i32, Buf)>,
ntasks: Cell<i32>,
timeout_due: Cell<Option<Instant>>,
pub modules: HashMap<libdeno::deno_mod, ModuleInfo>,
pub modules_by_name: HashMap<String, libdeno::deno_mod>,
pub modules: RefCell<Modules>,
pub state: Arc<IsolateState>,
}

Expand Down Expand Up @@ -202,8 +197,7 @@ impl Isolate {
tx,
ntasks: Cell::new(0),
timeout_due: Cell::new(None),
modules: HashMap::new(),
modules_by_name: HashMap::new(),
modules: RefCell::new(Modules::new()),
state,
}
}
Expand Down Expand Up @@ -290,58 +284,20 @@ impl Isolate {
return Err(js_error);
}

let name2 = name.clone();
self.modules.insert(id, ModuleInfo { name });

debug!("modules_by_name insert {}", name2);
self.modules_by_name.insert(name2, id);
self.modules.borrow_mut().register(id, &name);

Ok(id)
}

// TODO(ry) This should be private...
pub fn resolve_cb(
&self,
specifier: &str,
referrer: libdeno::deno_mod,
) -> libdeno::deno_mod {
self
.state
.metrics
.resolve_count
.fetch_add(1, Ordering::Relaxed);

debug!("resolve_cb {}", specifier);

let r = self.modules.get(&referrer);
if r.is_none() {
debug!("cant find referrer {}", referrer);
return 0;
}
let referrer_name = &r.unwrap().name;
let r = self.state.dir.resolve_module(specifier, referrer_name);
if let Err(err) = r {
debug!("potentially swallowed err: {}", err);
return 0;
}
let (name, _local_filename) = r.unwrap();

if let Some(id) = self.modules_by_name.get(&name) {
return *id;
} else {
return 0;
}
}

// TODO(ry) make this return a future.
pub fn mod_load_deps(
&mut self,
id: libdeno::deno_mod,
) -> Result<(), RustOrJsError> {
// basically iterate over the imports, start loading them.

let referrer = &self.modules[&id];
let referrer_name = referrer.name.clone();
let referrer_name =
{ self.modules.borrow_mut().get_name(id).unwrap().clone() };
let len =
unsafe { libdeno::deno_mod_imports_len(self.libdeno_isolate, id) };

Expand All @@ -366,7 +322,7 @@ impl Isolate {

debug!("mod_load_deps {} {}", i, name);

if None == self.modules_by_name.get(&name) {
if !self.modules.borrow_mut().is_registered(&name) {
let out =
code_fetch_and_maybe_compile(&self.state, specifier, &referrer_name)?;
let child_id =
Expand Down Expand Up @@ -415,7 +371,7 @@ impl Isolate {
.map_err(RustOrJsError::from)?;

let id = self
.mod_new(out.filename.clone(), out.js_source())
.mod_new(out.module_name.clone(), out.js_source())
.map_err(RustOrJsError::from)?;

self.mod_load_deps(id)?;
Expand Down Expand Up @@ -539,7 +495,16 @@ extern "C" fn resolve_cb(
let isolate = unsafe { Isolate::from_raw_ptr(user_data) };
let specifier_c: &CStr = unsafe { CStr::from_ptr(specifier_ptr) };
let specifier: &str = specifier_c.to_str().unwrap();
isolate.resolve_cb(specifier, referrer)
isolate
.state
.metrics
.resolve_count
.fetch_add(1, Ordering::Relaxed);
isolate.modules.borrow_mut().resolve_cb(
&isolate.state.dir,
specifier,
referrer,
)
}

// Dereferences the C pointer into the Rust Isolate object.
Expand Down
16 changes: 11 additions & 5 deletions src/main.rs
Expand Up @@ -19,6 +19,7 @@ mod http_util;
pub mod isolate;
pub mod js_errors;
pub mod libdeno;
pub mod modules;
pub mod msg;
pub mod msg_util;
pub mod ops;
Expand Down Expand Up @@ -90,7 +91,7 @@ fn main() {
flags.allow_write = true;
}

let should_prefetch = flags.prefetch;
let should_prefetch = flags.prefetch || flags.info;
let should_display_info = flags.info;

let state = Arc::new(isolate::IsolateState::new(flags, rest_argv, None));
Expand All @@ -107,14 +108,19 @@ fn main() {
// Execute input file.
if isolate.state.argv.len() > 1 {
let input_filename = isolate.state.argv[1].clone();
isolate
.execute_mod(&input_filename, should_prefetch)
.unwrap_or_else(print_err_and_exit);

if should_display_info {
// Display file info and exit. Do not run file
isolate.state.dir.print_file_info(input_filename);
modules::print_file_info(
&isolate.modules.borrow(),
&isolate.state.dir,
input_filename,
);
std::process::exit(0);
}
isolate
.execute_mod(&input_filename, should_prefetch)
.unwrap_or_else(print_err_and_exit);
}

isolate
Expand Down

0 comments on commit 99ce807

Please sign in to comment.