Skip to content
Permalink
Browse files

Reorg to reduce diff with master

  • Loading branch information...
ry committed Mar 12, 2019
1 parent a183615 commit 859093ad8102e8908a17f3aa0789757afac66482
Showing with 178 additions and 188 deletions.
  1. +177 −5 src/isolate.rs
  2. +0 −182 src/isolate_state.rs
  3. +1 −1 src/main.rs
@@ -1,10 +1,14 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
#![allow(dead_code)]
use crate::cli::Cli;
use crate::cli::Isolate as CoreIsolate;
use crate::compiler::compile_sync;
use crate::compiler::ModuleMetaData;
use crate::errors::DenoError;
use crate::errors::RustOrJsError;
use crate::isolate_state::IsolateState;
use crate::js_errors;
use crate::msg;
use deno_core::deno_mod;
use deno_core::JSError;
use futures::Async;
use futures::Future;
@@ -41,22 +45,98 @@ impl Isolate {
self.inner.execute(js_filename, js_source)
}

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

let referrer_name = {
let g = self.state.modules.lock().unwrap();
g.get_name(id).unwrap().clone()
};

for specifier in self.inner.mod_get_imports(id) {
let (name, _local_filename) = self
.state
.dir
.resolve_module(&specifier, &referrer_name)
.map_err(DenoError::from)
.map_err(RustOrJsError::from)?;

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

if !self.state.modules.lock().unwrap().is_registered(&name) {
let out = fetch_module_meta_data_and_maybe_compile(
&self.state,
&specifier,
&referrer_name,
)?;
let child_id = self.mod_new_and_regsiter(
false,
&out.module_name.clone(),
&out.js_source(),
)?;

self.mod_load_deps(child_id)?;
}
}

Ok(())
}

/// Executes the provided JavaScript module.
pub fn mod_execute(
pub fn execute_mod(
&mut self,
js_filename: &str,
is_prefetch: bool,
) -> Result<(), RustOrJsError> {
// TODO move isolate_state::mod_execute impl here.
// TODO move isolate_state::execute_mod impl here.
self
.state
.mod_execute(&self.inner, js_filename, is_prefetch)
.execute_mod_inner(js_filename, is_prefetch)
.map_err(|err| match err {
RustOrJsError::Js(err) => RustOrJsError::Js(self.apply_source_map(err)),
x => x,
})
}

/// High-level way to execute modules.
/// This will issue HTTP requests and file system calls.
/// Blocks. TODO(ry) Don't block.
fn execute_mod_inner(
&self,
url: &str,
is_prefetch: bool,
) -> Result<(), RustOrJsError> {
let out = fetch_module_meta_data_and_maybe_compile(&self.state, url, ".")
.map_err(RustOrJsError::from)?;

let id = self
.mod_new_and_regsiter(true, &out.module_name.clone(), &out.js_source())
.map_err(RustOrJsError::from)?;

self.mod_load_deps(id)?;

self
.inner
.mod_instantiate(id)
.map_err(RustOrJsError::from)?;
if !is_prefetch {
self.inner.mod_evaluate(id).map_err(RustOrJsError::from)?;
}
Ok(())
}

/// Wraps Isolate::mod_new but registers with modules.
fn mod_new_and_regsiter(
&self,
main: bool,
name: &str,
source: &str,
) -> Result<deno_mod, JSError> {
let id = self.inner.mod_new(main, name, source)?;
self.state.modules.lock().unwrap().register(id, &name);
Ok(id)
}

pub fn print_file_info(&self, module: &str) {
let m = self.state.modules.lock().unwrap();
m.print_file_info(&self.state.dir, module.to_string());
@@ -76,3 +156,95 @@ impl Future for Isolate {
self.inner.poll().map_err(|err| self.apply_source_map(err))
}
}

fn fetch_module_meta_data_and_maybe_compile(
state: &Arc<IsolateState>,
specifier: &str,
referrer: &str,
) -> Result<ModuleMetaData, DenoError> {
let mut out = state.dir.fetch_module_meta_data(specifier, referrer)?;
if (out.media_type == msg::MediaType::TypeScript
&& out.maybe_output_code.is_none())
|| state.flags.recompile
{
debug!(">>>>> compile_sync START");
out = compile_sync(state, specifier, &referrer, &out);
debug!(">>>>> compile_sync END");
state.dir.code_cache(&out)?;
}
Ok(out)
}

#[cfg(test)]
mod tests {
use super::*;
use crate::cli::Cli;
use crate::flags;
use crate::isolate_init::IsolateInit;
use crate::permissions::DenoPermissions;
use crate::tokio_util;
use crate::tokio_util::panic_on_error;
use futures::future::lazy;
use std::sync::atomic::Ordering;
use std::sync::Arc;

#[test]
fn execute_mod() {
let filename = std::env::current_dir()
.unwrap()
.join("tests/esm_imports_a.js");
let filename = filename.to_str().unwrap().to_string();

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

let state = Arc::new(IsolateState::new(flags, rest_argv, None));
let state_ = state.clone();
let init = IsolateInit {
snapshot: None,
init_script: None,
};
tokio_util::run(lazy(move || {
let cli = Cli::new(init, state.clone(), DenoPermissions::default());
let mut isolate = Isolate::new(cli);
if let Err(err) = isolate.execute_mod(&filename, false) {
eprintln!("execute_mod err {:?}", err);
}
panic_on_error(isolate)
}));

let metrics = &state_.metrics;
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 1);
}

/*
TODO(ry) uncomment this before landing
#[test]
fn execute_mod_circular() {
let filename = std::env::current_dir().unwrap().join("tests/circular1.js");
let filename = filename.to_str().unwrap();
let argv = vec![String::from("./deno"), String::from(filename)];
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
let state = Arc::new(IsolateState::new(flags, rest_argv, None));
let init = IsolateInit {
snapshot: None,
init_script: None,
};
let mut isolate =
Isolate::new(init, state, dispatch_sync, DenoPermissions::default());
tokio_util::init(|| {
isolate
.execute_mod(filename, false)
.expect("execute_mod error");
isolate.event_loop().ok();
});
let metrics = &isolate.state.metrics;
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
}
*/
}
Oops, something went wrong.

0 comments on commit 859093a

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.