2019-01-02 00:58:40 +00:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-06-20 02:07:01 +00:00
|
|
|
use crate::deno_error::DenoError;
|
2019-04-09 17:11:25 +00:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-03-20 15:38:43 +00:00
|
|
|
use crate::tokio_util;
|
2019-03-30 23:30:40 +00:00
|
|
|
use deno;
|
2019-06-12 23:55:59 +00:00
|
|
|
use deno::ModuleSpecifier;
|
2019-04-08 14:12:43 +00:00
|
|
|
use deno::StartupData;
|
2019-03-14 23:17:52 +00:00
|
|
|
use futures::Async;
|
2018-09-18 18:53:16 +00:00
|
|
|
use futures::Future;
|
2019-06-05 20:35:38 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2018-10-05 17:21:15 +00:00
|
|
|
|
2019-03-30 23:30:40 +00:00
|
|
|
/// Wraps deno::Isolate to provide source maps, ops for the CLI, and
|
2019-03-14 23:17:52 +00:00
|
|
|
/// high-level module loading
|
2019-06-05 20:35:38 +00:00
|
|
|
#[derive(Clone)]
|
2019-04-08 21:10:00 +00:00
|
|
|
pub struct Worker {
|
2019-06-12 17:53:24 +00:00
|
|
|
isolate: Arc<Mutex<deno::Isolate>>,
|
2019-04-16 19:13:42 +00:00
|
|
|
pub state: ThreadSafeState,
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
impl Worker {
|
|
|
|
pub fn new(
|
|
|
|
_name: String,
|
|
|
|
startup_data: StartupData,
|
2019-04-09 17:11:25 +00:00
|
|
|
state: ThreadSafeState,
|
2019-04-08 21:10:00 +00:00
|
|
|
) -> Worker {
|
2019-04-09 17:11:25 +00:00
|
|
|
let state_ = state.clone();
|
2019-06-12 17:53:24 +00:00
|
|
|
let isolate = Arc::new(Mutex::new(deno::Isolate::new(startup_data, false)));
|
|
|
|
{
|
|
|
|
let mut i = isolate.lock().unwrap();
|
|
|
|
i.set_dispatch(move |control_buf, zero_copy_buf| {
|
|
|
|
state_.dispatch(control_buf, zero_copy_buf)
|
|
|
|
});
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
2019-06-12 17:53:24 +00:00
|
|
|
Self { isolate, state }
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2018-12-11 19:03:58 +00:00
|
|
|
/// Same as execute2() but the filename defaults to "<anonymous>".
|
2019-06-20 02:07:01 +00:00
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), DenoError> {
|
2018-12-11 19:03:58 +00: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 23:17:52 +00:00
|
|
|
&mut self,
|
2018-09-18 00:41:13 +00:00
|
|
|
js_filename: &str,
|
|
|
|
js_source: &str,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> Result<(), DenoError> {
|
2019-06-12 17:53:24 +00:00
|
|
|
let mut isolate = self.isolate.lock().unwrap();
|
2019-06-20 02:07:01 +00:00
|
|
|
match isolate.execute(js_filename, js_source) {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(err) => Err(DenoError::from(err)),
|
|
|
|
}
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:09:31 +00:00
|
|
|
/// Executes the provided JavaScript module.
|
2019-04-16 19:13:42 +00:00
|
|
|
pub fn execute_mod_async(
|
2019-06-05 20:35:38 +00:00
|
|
|
&mut self,
|
2019-06-12 19:00:08 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-04-16 19:13:42 +00:00
|
|
|
is_prefetch: bool,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> impl Future<Item = (), Error = DenoError> {
|
2019-06-05 20:35:38 +00:00
|
|
|
let worker = self.clone();
|
|
|
|
let worker_ = worker.clone();
|
|
|
|
let loader = self.state.clone();
|
2019-06-12 17:53:24 +00:00
|
|
|
let isolate = self.isolate.clone();
|
2019-06-11 18:35:03 +00:00
|
|
|
let modules = self.state.modules.clone();
|
2019-06-12 19:00:08 +00:00
|
|
|
let recursive_load = deno::RecursiveLoad::new(
|
|
|
|
&module_specifier.to_string(),
|
|
|
|
loader,
|
|
|
|
isolate,
|
|
|
|
modules,
|
|
|
|
);
|
2019-06-05 20:35:38 +00:00
|
|
|
recursive_load
|
|
|
|
.and_then(move |id| -> Result<(), deno::JSErrorOr<DenoError>> {
|
|
|
|
worker.state.progress.done();
|
2019-04-16 19:13:42 +00:00
|
|
|
if is_prefetch {
|
2019-06-05 20:35:38 +00:00
|
|
|
Ok(())
|
2019-04-16 19:13:42 +00:00
|
|
|
} else {
|
2019-06-12 17:53:24 +00:00
|
|
|
let mut isolate = worker.isolate.lock().unwrap();
|
2019-06-05 20:35:38 +00:00
|
|
|
let result = isolate.mod_evaluate(id);
|
2019-04-16 19:13:42 +00:00
|
|
|
if let Err(err) = result {
|
2019-06-05 20:35:38 +00:00
|
|
|
Err(deno::JSErrorOr::JSError(err))
|
2019-04-16 19:13:42 +00:00
|
|
|
} else {
|
2019-06-05 20:35:38 +00:00
|
|
|
Ok(())
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2019-04-02 01:46:40 +00:00
|
|
|
}
|
2019-06-05 20:35:38 +00:00
|
|
|
}).map_err(move |err| {
|
|
|
|
worker_.state.progress.done();
|
2019-06-20 02:07:01 +00:00
|
|
|
// Convert to DenoError AND apply_source_map.
|
2019-06-05 20:35:38 +00:00
|
|
|
match err {
|
|
|
|
deno::JSErrorOr::JSError(err) => {
|
2019-06-20 02:07:01 +00:00
|
|
|
worker_.apply_source_map(DenoError::from(err))
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
2019-06-20 02:07:01 +00:00
|
|
|
deno::JSErrorOr::Other(err) => err,
|
2019-06-05 20:35:38 +00:00
|
|
|
}
|
|
|
|
})
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2019-06-11 20:09:31 +00:00
|
|
|
/// Executes the provided JavaScript module.
|
2019-01-15 17:19:58 +00:00
|
|
|
pub fn execute_mod(
|
2019-06-05 20:35:38 +00:00
|
|
|
&mut self,
|
2019-06-12 19:00:08 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-01-15 17:19:58 +00:00
|
|
|
is_prefetch: bool,
|
2019-06-20 02:07:01 +00:00
|
|
|
) -> Result<(), DenoError> {
|
2019-06-12 19:00:08 +00:00
|
|
|
tokio_util::block_on(self.execute_mod_async(module_specifier, is_prefetch))
|
2019-03-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 19:13:42 +00:00
|
|
|
/// Applies source map to the error.
|
2019-06-20 02:07:01 +00:00
|
|
|
fn apply_source_map(&self, err: DenoError) -> DenoError {
|
|
|
|
err.apply_source_map(&self.state.dir)
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-02 01:46:40 +00:00
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
impl Future for Worker {
|
2019-03-14 23:17:52 +00:00
|
|
|
type Item = ();
|
2019-06-20 02:07:01 +00:00
|
|
|
type Error = DenoError;
|
2019-03-14 23:17:52 +00:00
|
|
|
|
|
|
|
fn poll(&mut self) -> Result<Async<()>, Self::Error> {
|
2019-06-12 17:53:24 +00:00
|
|
|
let mut isolate = self.isolate.lock().unwrap();
|
2019-06-20 02:07:01 +00:00
|
|
|
isolate
|
|
|
|
.poll()
|
|
|
|
.map_err(|err| self.apply_source_map(DenoError::from(err)))
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 20:05:41 +00:00
|
|
|
|
2018-09-18 18:53:16 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-06-20 02:07:01 +00:00
|
|
|
use crate::deno_error::err_check;
|
2019-03-14 23:17:52 +00:00
|
|
|
use crate::flags;
|
2019-04-11 14:58:31 +00:00
|
|
|
use crate::ops::op_selector_std;
|
2019-05-11 14:23:19 +00:00
|
|
|
use crate::progress::Progress;
|
2019-04-08 21:10:00 +00:00
|
|
|
use crate::resources;
|
|
|
|
use crate::startup_data;
|
2019-04-09 17:11:25 +00:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-04-08 21:10:00 +00:00
|
|
|
use crate::tokio_util;
|
2019-03-14 23:17:52 +00:00
|
|
|
use futures::future::lazy;
|
|
|
|
use std::sync::atomic::Ordering;
|
2019-01-01 11:24:05 +00:00
|
|
|
|
|
|
|
#[test]
|
2019-04-16 19:13:42 +00:00
|
|
|
fn execute_mod_esm_imports_a() {
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_root("tests/esm_imports_a.js").unwrap();
|
|
|
|
let argv = vec![String::from("./deno"), module_specifier.to_string()];
|
2019-05-11 14:23:19 +00:00
|
|
|
let state = ThreadSafeState::new(
|
|
|
|
flags::DenoFlags::default(),
|
|
|
|
argv,
|
|
|
|
op_selector_std,
|
|
|
|
Progress::new(),
|
|
|
|
);
|
2019-03-14 23:17:52 +00:00
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
2019-06-05 20:35:38 +00:00
|
|
|
let mut worker =
|
|
|
|
Worker::new("TEST".to_string(), StartupData::None, state);
|
2019-06-12 19:00:08 +00:00
|
|
|
let result = worker.execute_mod(&module_specifier, false);
|
2019-06-05 20:35:38 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2019-04-08 21:10:00 +00:00
|
|
|
tokio_util::panic_on_error(worker)
|
2019-03-14 23:17:52 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-04-16 19:13:42 +00:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-05-20 16:06:57 +00:00
|
|
|
// Check that we didn't start the compiler.
|
|
|
|
assert_eq!(metrics.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_circular() {
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_root("tests/circular1.js").unwrap();
|
|
|
|
let argv = vec![String::from("./deno"), module_specifier.to_string()];
|
2019-05-11 14:23:19 +00:00
|
|
|
let state = ThreadSafeState::new(
|
|
|
|
flags::DenoFlags::default(),
|
|
|
|
argv,
|
|
|
|
op_selector_std,
|
|
|
|
Progress::new(),
|
|
|
|
);
|
2019-03-14 23:17:52 +00:00
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
2019-06-05 20:35:38 +00:00
|
|
|
let mut worker =
|
|
|
|
Worker::new("TEST".to_string(), StartupData::None, state);
|
2019-06-12 19:00:08 +00:00
|
|
|
let result = worker.execute_mod(&module_specifier, false);
|
2019-06-05 20:35:38 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2019-04-08 21:10:00 +00:00
|
|
|
tokio_util::panic_on_error(worker)
|
2019-03-14 23:17:52 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-01-30 22:21:31 +00:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-05-20 16:06:57 +00:00
|
|
|
// Check that we didn't start the compiler.
|
|
|
|
assert_eq!(metrics.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-01 11:24:05 +00:00
|
|
|
}
|
2019-04-08 21:10:00 +00:00
|
|
|
|
2019-05-11 14:23:19 +00:00
|
|
|
#[test]
|
|
|
|
fn execute_006_url_imports() {
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_root("tests/006_url_imports.ts").unwrap();
|
|
|
|
let argv = vec![String::from("deno"), module_specifier.to_string()];
|
2019-05-11 14:23:19 +00:00
|
|
|
let mut flags = flags::DenoFlags::default();
|
|
|
|
flags.reload = true;
|
|
|
|
let state =
|
|
|
|
ThreadSafeState::new(flags, argv, op_selector_std, Progress::new());
|
|
|
|
let state_ = state.clone();
|
|
|
|
tokio_util::run(lazy(move || {
|
|
|
|
let mut worker = Worker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state,
|
|
|
|
);
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(worker.execute("denoMain()"));
|
2019-06-12 19:00:08 +00:00
|
|
|
let result = worker.execute_mod(&module_specifier, false);
|
2019-06-05 20:35:38 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2019-05-11 14:23:19 +00:00
|
|
|
tokio_util::panic_on_error(worker)
|
|
|
|
}));
|
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 3);
|
2019-05-20 16:06:57 +00:00
|
|
|
// Check that we've only invoked the compiler once.
|
|
|
|
assert_eq!(metrics.compiler_starts.load(Ordering::SeqCst), 1);
|
2019-05-11 14:23:19 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
fn create_test_worker() -> Worker {
|
2019-06-08 18:42:28 +00:00
|
|
|
let state = ThreadSafeState::mock(vec![
|
|
|
|
String::from("./deno"),
|
|
|
|
String::from("hello.js"),
|
|
|
|
]);
|
2019-04-08 21:10:00 +00:00
|
|
|
let mut worker =
|
2019-04-09 17:11:25 +00:00
|
|
|
Worker::new("TEST".to_string(), startup_data::deno_isolate_init(), state);
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(worker.execute("denoMain()"));
|
|
|
|
err_check(worker.execute("workerMain()"));
|
2019-04-08 21:10:00 +00:00
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_worker_messages() {
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let source = r#"
|
|
|
|
onmessage = function(e) {
|
|
|
|
console.log("msg from main script", e.data);
|
|
|
|
if (e.data == "exit") {
|
2019-04-14 20:07:24 +00:00
|
|
|
delete window.onmessage;
|
2019-04-08 21:10:00 +00:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
console.assert(e.data === "hi");
|
|
|
|
}
|
|
|
|
postMessage([1, 2, 3]);
|
|
|
|
console.log("after postMessage");
|
|
|
|
}
|
|
|
|
"#;
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(worker.execute(source));
|
2019-04-08 21:10:00 +00:00
|
|
|
|
|
|
|
let resource = worker.state.resource.clone();
|
|
|
|
let resource_ = resource.clone();
|
|
|
|
|
|
|
|
tokio::spawn(lazy(move || {
|
|
|
|
worker.then(move |r| -> Result<(), ()> {
|
|
|
|
resource_.close();
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(r);
|
2019-04-08 21:10:00 +00:00
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
}));
|
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
|
|
|
|
let r = resources::post_message_to_worker(resource.rid, msg).wait();
|
|
|
|
assert!(r.is_ok());
|
|
|
|
|
|
|
|
let maybe_msg = resources::get_message_from_worker(resource.rid)
|
|
|
|
.wait()
|
|
|
|
.unwrap();
|
|
|
|
assert!(maybe_msg.is_some());
|
|
|
|
// Check if message received is [1, 2, 3] in json
|
|
|
|
assert_eq!(*maybe_msg.unwrap(), *b"[1,2,3]");
|
|
|
|
|
|
|
|
let msg = json!("exit")
|
|
|
|
.to_string()
|
|
|
|
.into_boxed_str()
|
|
|
|
.into_boxed_bytes();
|
|
|
|
let r = resources::post_message_to_worker(resource.rid, msg).wait();
|
|
|
|
assert!(r.is_ok());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn removed_from_resource_table_on_close() {
|
|
|
|
tokio_util::init(|| {
|
|
|
|
let mut worker = create_test_worker();
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(
|
2019-04-16 19:13:42 +00:00
|
|
|
worker.execute("onmessage = () => { delete window.onmessage; }"),
|
2019-04-08 21:10:00 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
let resource = worker.state.resource.clone();
|
|
|
|
let rid = resource.rid;
|
|
|
|
|
2019-04-12 22:39:31 +00:00
|
|
|
let worker_future = worker
|
|
|
|
.then(move |r| -> Result<(), ()> {
|
2019-04-08 21:10:00 +00:00
|
|
|
resource.close();
|
|
|
|
println!("workers.rs after resource close");
|
2019-06-20 02:07:01 +00:00
|
|
|
err_check(r);
|
2019-04-08 21:10:00 +00:00
|
|
|
Ok(())
|
2019-04-12 22:39:31 +00:00
|
|
|
}).shared();
|
|
|
|
|
|
|
|
let worker_future_ = worker_future.clone();
|
|
|
|
tokio::spawn(lazy(move || worker_future_.then(|_| Ok(()))));
|
2019-04-08 21:10:00 +00:00
|
|
|
|
|
|
|
assert_eq!(resources::get_type(rid), Some("worker".to_string()));
|
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
let r = resources::post_message_to_worker(rid, msg).wait();
|
|
|
|
assert!(r.is_ok());
|
|
|
|
debug!("rid {:?}", rid);
|
|
|
|
|
2019-04-12 22:39:31 +00:00
|
|
|
worker_future.wait().unwrap();
|
2019-04-08 21:10:00 +00:00
|
|
|
assert_eq!(resources::get_type(rid), None);
|
|
|
|
})
|
|
|
|
}
|
2019-04-16 19:13:42 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_resolve_error() {
|
2019-05-20 16:06:57 +00:00
|
|
|
tokio_util::init(|| {
|
2019-06-20 02:07:01 +00:00
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
2019-06-05 20:35:38 +00:00
|
|
|
let mut worker = create_test_worker();
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_root("does-not-exist").unwrap();
|
|
|
|
let result = worker.execute_mod_async(&module_specifier, false).wait();
|
2019-05-20 16:06:57 +00:00
|
|
|
assert!(result.is_err());
|
|
|
|
})
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_002_hello() {
|
2019-05-20 16:06:57 +00:00
|
|
|
tokio_util::init(|| {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
2019-06-05 20:35:38 +00:00
|
|
|
let mut worker = create_test_worker();
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_root("./tests/002_hello.ts").unwrap();
|
|
|
|
let result = worker.execute_mod_async(&module_specifier, false).wait();
|
2019-05-20 16:06:57 +00:00
|
|
|
assert!(result.is_ok());
|
|
|
|
})
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|