2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::compiler::compile_sync;
|
2019-02-18 10:42:15 -05:00
|
|
|
use crate::compiler::ModuleMetaData;
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::errors::DenoError;
|
2019-01-30 17:21:31 -05:00
|
|
|
use crate::errors::RustOrJsError;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::isolate_state::IsolateState;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::isolate_state::IsolateStateContainer;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::js_errors;
|
2019-01-14 01:30:38 -05:00
|
|
|
use crate::msg;
|
2019-03-20 11:38:43 -04:00
|
|
|
use crate::tokio_util;
|
2019-03-14 19:17:52 -04:00
|
|
|
use deno_core;
|
|
|
|
use deno_core::deno_mod;
|
2019-03-19 20:55:59 -04:00
|
|
|
use deno_core::Behavior;
|
2019-02-28 16:19:04 -05:00
|
|
|
use deno_core::JSError;
|
2019-03-14 19:17:52 -04:00
|
|
|
use futures::Async;
|
2018-09-18 14:53:16 -04:00
|
|
|
use futures::Future;
|
2019-03-25 17:43:31 -04:00
|
|
|
use std::sync::atomic::Ordering;
|
2018-09-18 14:53:16 -04:00
|
|
|
use std::sync::Arc;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
pub trait DenoBehavior: Behavior + IsolateStateContainer + Send {}
|
|
|
|
impl<T> DenoBehavior for T where T: Behavior + IsolateStateContainer + Send {}
|
|
|
|
|
|
|
|
type CoreIsolate<B> = deno_core::Isolate<B>;
|
2019-02-12 21:14:02 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
/// Wraps deno_core::Isolate to provide source maps, ops for the CLI, and
|
|
|
|
/// high-level module loading
|
2019-03-19 20:55:59 -04:00
|
|
|
pub struct Isolate<B: Behavior> {
|
|
|
|
inner: CoreIsolate<B>,
|
2019-03-14 19:17:52 -04:00
|
|
|
state: Arc<IsolateState>,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
impl<B: DenoBehavior> Isolate<B> {
|
|
|
|
pub fn new(behavior: B) -> Isolate<B> {
|
|
|
|
let state = behavior.state().clone();
|
2019-03-14 19:17:52 -04:00
|
|
|
Self {
|
2019-03-19 20:55:59 -04:00
|
|
|
inner: CoreIsolate::new(behavior),
|
2018-11-30 03:30:49 -05:00
|
|
|
state,
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-11 14:03:58 -05:00
|
|
|
/// Same as execute2() but the filename defaults to "<anonymous>".
|
2019-03-14 19:17:52 -04:00
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), JSError> {
|
2018-12-11 14:03:58 -05:00
|
|
|
self.execute2("<anonymous>", js_source)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Executes the provided JavaScript source code. The js_filename argument is
|
|
|
|
/// provided only for debugging purposes.
|
|
|
|
pub fn execute2(
|
2019-03-14 19:17:52 -04:00
|
|
|
&mut self,
|
2018-09-17 20:41:13 -04:00
|
|
|
js_filename: &str,
|
|
|
|
js_source: &str,
|
2018-12-06 23:05:36 -05:00
|
|
|
) -> Result<(), JSError> {
|
2019-03-14 19:17:52 -04:00
|
|
|
self.inner.execute(js_filename, js_source)
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(ry) make this return a future.
|
2019-03-14 19:17:52 -04:00
|
|
|
fn mod_load_deps(&self, id: deno_mod) -> Result<(), RustOrJsError> {
|
2019-01-30 17:21:31 -05:00
|
|
|
// basically iterate over the imports, start loading them.
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
let referrer_name = {
|
|
|
|
let g = self.state.modules.lock().unwrap();
|
|
|
|
g.get_name(id).unwrap().clone()
|
|
|
|
};
|
2019-01-30 17:21:31 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
for specifier in self.inner.mod_get_imports(id) {
|
2019-01-30 17:21:31 -05:00
|
|
|
let (name, _local_filename) = self
|
|
|
|
.state
|
|
|
|
.dir
|
2019-03-14 19:17:52 -04:00
|
|
|
.resolve_module(&specifier, &referrer_name)
|
2019-01-30 17:21:31 -05:00
|
|
|
.map_err(DenoError::from)
|
|
|
|
.map_err(RustOrJsError::from)?;
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
debug!("mod_load_deps {}", name);
|
2019-01-30 17:21:31 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
if !self.state.modules.lock().unwrap().is_registered(&name) {
|
2019-02-18 10:42:15 -05:00
|
|
|
let out = fetch_module_meta_data_and_maybe_compile(
|
|
|
|
&self.state,
|
2019-03-14 19:17:52 -04:00
|
|
|
&specifier,
|
2019-02-18 10:42:15 -05:00
|
|
|
&referrer_name,
|
|
|
|
)?;
|
2019-03-14 19:17:52 -04:00
|
|
|
let child_id = self.mod_new_and_register(
|
|
|
|
false,
|
|
|
|
&out.module_name.clone(),
|
|
|
|
&out.js_source(),
|
|
|
|
)?;
|
2019-01-30 17:21:31 -05:00
|
|
|
|
|
|
|
self.mod_load_deps(child_id)?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2019-01-01 06:24:05 -05:00
|
|
|
/// Executes the provided JavaScript module.
|
2019-01-15 12:19:58 -05:00
|
|
|
pub fn execute_mod(
|
2019-01-30 17:21:31 -05:00
|
|
|
&mut self,
|
2019-01-15 12:19:58 -05:00
|
|
|
js_filename: &str,
|
|
|
|
is_prefetch: bool,
|
2019-02-02 01:58:53 -05:00
|
|
|
) -> Result<(), RustOrJsError> {
|
2019-03-14 19:17:52 -04:00
|
|
|
// TODO move isolate_state::execute_mod impl here.
|
|
|
|
self
|
|
|
|
.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(
|
|
|
|
&mut self,
|
|
|
|
url: &str,
|
|
|
|
is_prefetch: bool,
|
|
|
|
) -> Result<(), RustOrJsError> {
|
|
|
|
let out = fetch_module_meta_data_and_maybe_compile(&self.state, url, ".")
|
|
|
|
.map_err(RustOrJsError::from)?;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-02-02 01:58:53 -05:00
|
|
|
let id = self
|
2019-03-14 19:17:52 -04:00
|
|
|
.mod_new_and_register(true, &out.module_name.clone(), &out.js_source())
|
2019-02-02 01:58:53 -05:00
|
|
|
.map_err(RustOrJsError::from)?;
|
2019-01-08 14:42:53 -05:00
|
|
|
|
2019-02-02 01:58:53 -05:00
|
|
|
self.mod_load_deps(id)?;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-03-25 17:43:31 -04:00
|
|
|
let state = self.state.clone();
|
|
|
|
|
|
|
|
let mut resolve = move |specifier: &str, referrer: deno_mod| -> deno_mod {
|
|
|
|
state.metrics.resolve_count.fetch_add(1, Ordering::Relaxed);
|
|
|
|
let mut modules = state.modules.lock().unwrap();
|
|
|
|
modules.resolve_cb(&state.dir, specifier, referrer)
|
|
|
|
};
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
self
|
|
|
|
.inner
|
2019-03-25 17:43:31 -04:00
|
|
|
.mod_instantiate(id, &mut resolve)
|
2019-03-14 19:17:52 -04:00
|
|
|
.map_err(RustOrJsError::from)?;
|
2019-01-30 17:21:31 -05:00
|
|
|
if !is_prefetch {
|
2019-03-14 19:17:52 -04:00
|
|
|
self.inner.mod_evaluate(id).map_err(RustOrJsError::from)?;
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
Ok(())
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
/// Wraps Isolate::mod_new but registers with modules.
|
|
|
|
fn mod_new_and_register(
|
|
|
|
&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)
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
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());
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
/// Applies source map to the error.
|
|
|
|
fn apply_source_map(&self, err: JSError) -> JSError {
|
|
|
|
js_errors::apply_source_map(&err, &self.state.dir)
|
2018-09-18 14:53:16 -04:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-03-19 20:55:59 -04:00
|
|
|
impl<B: DenoBehavior> Future for Isolate<B> {
|
2019-03-14 19:17:52 -04:00
|
|
|
type Item = ();
|
|
|
|
type Error = JSError;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Result<Async<()>, Self::Error> {
|
|
|
|
self.inner.poll().map_err(|err| self.apply_source_map(err))
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-20 11:38:43 -04:00
|
|
|
fn fetch_module_meta_data_and_maybe_compile_async(
|
|
|
|
state: &Arc<IsolateState>,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
|
|
|
) -> impl Future<Item = ModuleMetaData, Error = DenoError> {
|
|
|
|
let state_ = state.clone();
|
|
|
|
let specifier = specifier.to_string();
|
|
|
|
let referrer = referrer.to_string();
|
|
|
|
state
|
|
|
|
.dir
|
|
|
|
.fetch_module_meta_data_async(&specifier, &referrer)
|
|
|
|
.and_then(move |mut out| {
|
|
|
|
if (out.media_type == msg::MediaType::TypeScript
|
|
|
|
&& out.maybe_output_code.is_none())
|
|
|
|
|| state_.flags.recompile
|
|
|
|
{
|
|
|
|
debug!(">>>>> compile_sync START");
|
|
|
|
out = compile_sync(state_.clone(), &specifier, &referrer, &out);
|
|
|
|
debug!(">>>>> compile_sync END");
|
|
|
|
state_.dir.code_cache(&out)?;
|
|
|
|
}
|
|
|
|
Ok(out)
|
|
|
|
})
|
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
|
2019-02-18 10:42:15 -05:00
|
|
|
fn fetch_module_meta_data_and_maybe_compile(
|
2019-01-09 12:59:46 -05:00
|
|
|
state: &Arc<IsolateState>,
|
|
|
|
specifier: &str,
|
|
|
|
referrer: &str,
|
2019-02-18 10:42:15 -05:00
|
|
|
) -> Result<ModuleMetaData, DenoError> {
|
2019-03-20 11:38:43 -04:00
|
|
|
tokio_util::block_on(fetch_module_meta_data_and_maybe_compile_async(
|
|
|
|
state, specifier, referrer,
|
|
|
|
))
|
2019-01-09 12:59:46 -05:00
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-19 20:55:59 -04:00
|
|
|
use crate::cli_behavior::CliBehavior;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::flags;
|
|
|
|
use futures::future::lazy;
|
|
|
|
use std::sync::atomic::Ordering;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod() {
|
|
|
|
let filename = std::env::current_dir()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/esm_imports_a.js");
|
2019-03-14 19:17:52 -04:00
|
|
|
let filename = filename.to_str().unwrap().to_string();
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
let argv = vec![String::from("./deno"), filename.clone()];
|
2019-01-01 06:24:05 -05:00
|
|
|
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
|
|
|
|
|
2019-01-08 14:44:06 -05:00
|
|
|
let state = Arc::new(IsolateState::new(flags, rest_argv, None));
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
2019-03-19 20:55:59 -04:00
|
|
|
let cli = CliBehavior::new(None, state.clone());
|
2019-03-14 19:17:52 -04:00
|
|
|
let mut isolate = Isolate::new(cli);
|
|
|
|
if let Err(err) = isolate.execute_mod(&filename, false) {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
|
|
|
tokio_util::panic_on_error(isolate)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-01-30 17:21:31 -05:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_circular() {
|
|
|
|
let filename = std::env::current_dir().unwrap().join("tests/circular1.js");
|
2019-03-14 19:17:52 -04:00
|
|
|
let filename = filename.to_str().unwrap().to_string();
|
2019-01-30 17:21:31 -05:00
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
let argv = vec![String::from("./deno"), filename.clone()];
|
2019-01-30 17:21:31 -05:00
|
|
|
let (flags, rest_argv, _) = flags::set_flags(argv).unwrap();
|
|
|
|
|
|
|
|
let state = Arc::new(IsolateState::new(flags, rest_argv, None));
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
2019-03-19 20:55:59 -04:00
|
|
|
let cli = CliBehavior::new(None, state.clone());
|
2019-03-14 19:17:52 -04:00
|
|
|
let mut isolate = Isolate::new(cli);
|
|
|
|
if let Err(err) = isolate.execute_mod(&filename, false) {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
|
|
|
tokio_util::panic_on_error(isolate)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-01-30 17:21:31 -05:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-01-01 06:24:05 -05:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|