2019-01-01 19:58:40 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
2019-07-10 18:53:48 -04:00
|
|
|
use crate::fmt_errors::JSError;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops;
|
2019-04-09 13:11:25 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-03-30 19:30:40 -04:00
|
|
|
use deno;
|
2019-11-04 10:38:52 -05:00
|
|
|
use deno::Buf;
|
2019-07-10 18:53:48 -04:00
|
|
|
use deno::ErrBox;
|
2019-06-12 19:55:59 -04:00
|
|
|
use deno::ModuleSpecifier;
|
2019-08-07 12:55:39 -04:00
|
|
|
use deno::RecursiveLoad;
|
2019-04-08 10:12:43 -04:00
|
|
|
use deno::StartupData;
|
2019-11-16 19:17:47 -05:00
|
|
|
use futures::channel::mpsc;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::future::TryFutureExt;
|
|
|
|
use futures::sink::SinkExt;
|
|
|
|
use futures::stream::StreamExt;
|
2019-08-09 19:33:59 -04:00
|
|
|
use std::env;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
|
|
|
use std::pin::Pin;
|
2019-06-05 16:35:38 -04:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::sync::Mutex;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2019-08-09 19:33:59 -04:00
|
|
|
use url::Url;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
/// Wraps mpsc channels so they can be referenced
|
2019-11-04 10:38:52 -05:00
|
|
|
/// from ops and used to facilitate parent-child communication
|
|
|
|
/// for workers.
|
|
|
|
pub struct WorkerChannels {
|
|
|
|
pub sender: mpsc::Sender<Buf>,
|
|
|
|
pub receiver: mpsc::Receiver<Buf>,
|
|
|
|
}
|
|
|
|
|
2019-03-30 19:30:40 -04:00
|
|
|
/// Wraps deno::Isolate to provide source maps, ops for the CLI, and
|
2019-11-04 10:38:52 -05:00
|
|
|
/// high-level module loading.
|
2019-06-05 16:35:38 -04:00
|
|
|
#[derive(Clone)]
|
2019-04-08 17:10:00 -04:00
|
|
|
pub struct Worker {
|
2019-11-04 10:38:52 -05:00
|
|
|
pub name: String,
|
2019-06-12 13:53:24 -04:00
|
|
|
isolate: Arc<Mutex<deno::Isolate>>,
|
2019-04-16 15:13:42 -04:00
|
|
|
pub state: ThreadSafeState,
|
2019-11-09 15:07:14 -05:00
|
|
|
external_channels: Arc<Mutex<WorkerChannels>>,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
impl Worker {
|
|
|
|
pub fn new(
|
2019-11-04 10:38:52 -05:00
|
|
|
name: String,
|
2019-04-08 17:10:00 -04:00
|
|
|
startup_data: StartupData,
|
2019-04-09 13:11:25 -04:00
|
|
|
state: ThreadSafeState,
|
2019-11-09 15:07:14 -05:00
|
|
|
external_channels: WorkerChannels,
|
2019-11-04 10:38:52 -05:00
|
|
|
) -> Self {
|
2019-06-12 13:53:24 -04:00
|
|
|
let isolate = Arc::new(Mutex::new(deno::Isolate::new(startup_data, false)));
|
|
|
|
{
|
|
|
|
let mut i = isolate.lock().unwrap();
|
2019-12-05 15:30:20 -05:00
|
|
|
let op_registry = i.op_registry.clone();
|
2019-10-01 18:51:05 -04:00
|
|
|
|
2019-10-11 14:41:54 -04:00
|
|
|
ops::compiler::init(&mut i, &state);
|
|
|
|
ops::errors::init(&mut i, &state);
|
|
|
|
ops::fetch::init(&mut i, &state);
|
|
|
|
ops::files::init(&mut i, &state);
|
|
|
|
ops::fs::init(&mut i, &state);
|
|
|
|
ops::io::init(&mut i, &state);
|
2019-12-05 15:30:20 -05:00
|
|
|
ops::plugins::init(&mut i, &state, op_registry);
|
2019-10-11 14:41:54 -04:00
|
|
|
ops::net::init(&mut i, &state);
|
2019-10-13 10:37:37 -04:00
|
|
|
ops::tls::init(&mut i, &state);
|
2019-10-11 14:41:54 -04:00
|
|
|
ops::os::init(&mut i, &state);
|
|
|
|
ops::permissions::init(&mut i, &state);
|
|
|
|
ops::process::init(&mut i, &state);
|
|
|
|
ops::random::init(&mut i, &state);
|
|
|
|
ops::repl::init(&mut i, &state);
|
|
|
|
ops::resources::init(&mut i, &state);
|
|
|
|
ops::timers::init(&mut i, &state);
|
|
|
|
ops::workers::init(&mut i, &state);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
|
|
|
let state_ = state.clone();
|
|
|
|
i.set_dyn_import(move |id, specifier, referrer| {
|
|
|
|
let load_stream = RecursiveLoad::dynamic_import(
|
|
|
|
id,
|
|
|
|
specifier,
|
|
|
|
referrer,
|
|
|
|
state_.clone(),
|
|
|
|
state_.modules.clone(),
|
|
|
|
);
|
|
|
|
Box::new(load_stream)
|
|
|
|
});
|
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
let global_state_ = state.global_state.clone();
|
2019-07-10 18:53:48 -04:00
|
|
|
i.set_js_error_create(move |v8_exception| {
|
2019-11-04 10:38:52 -05:00
|
|
|
JSError::from_v8_exception(v8_exception, &global_state_.ts_compiler)
|
2019-07-10 18:53:48 -04:00
|
|
|
})
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
2019-11-09 15:07:14 -05:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
Self {
|
|
|
|
name,
|
|
|
|
isolate,
|
|
|
|
state,
|
2019-11-09 15:07:14 -05:00
|
|
|
external_channels: Arc::new(Mutex::new(external_channels)),
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2018-12-06 23:05:36 -05:00
|
|
|
}
|
|
|
|
|
2019-08-09 19:33:59 -04:00
|
|
|
/// Same as execute2() but the filename defaults to "$CWD/__anonymous__".
|
2019-07-10 18:53:48 -04:00
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), ErrBox> {
|
2019-08-09 19:33:59 -04:00
|
|
|
let path = env::current_dir().unwrap().join("__anonymous__");
|
|
|
|
let url = Url::from_file_path(path).unwrap();
|
|
|
|
self.execute2(url.as_str(), js_source)
|
2018-12-11 14:03:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2019-07-10 18:53:48 -04:00
|
|
|
) -> Result<(), ErrBox> {
|
2019-06-12 13:53:24 -04:00
|
|
|
let mut isolate = self.isolate.lock().unwrap();
|
2019-07-10 18:53:48 -04:00
|
|
|
isolate.execute(js_filename, js_source)
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2019-06-11 16:09:31 -04:00
|
|
|
/// Executes the provided JavaScript module.
|
2019-04-16 15:13:42 -04:00
|
|
|
pub fn execute_mod_async(
|
2019-06-05 16:35:38 -04:00
|
|
|
&mut self,
|
2019-06-12 15:00:08 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2019-10-19 17:19:19 -04:00
|
|
|
maybe_code: Option<String>,
|
2019-04-16 15:13:42 -04:00
|
|
|
is_prefetch: bool,
|
2019-11-16 19:17:47 -05:00
|
|
|
) -> impl Future<Output = Result<(), ErrBox>> {
|
2019-06-05 16:35:38 -04:00
|
|
|
let worker = self.clone();
|
|
|
|
let loader = self.state.clone();
|
2019-06-12 13:53:24 -04:00
|
|
|
let isolate = self.isolate.clone();
|
2019-06-11 14:35:03 -04:00
|
|
|
let modules = self.state.modules.clone();
|
2019-10-19 17:19:19 -04:00
|
|
|
let recursive_load = RecursiveLoad::main(
|
|
|
|
&module_specifier.to_string(),
|
|
|
|
maybe_code,
|
|
|
|
loader,
|
|
|
|
modules,
|
2019-11-22 12:14:34 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
async move {
|
|
|
|
let id = recursive_load.get_future(isolate).await?;
|
2019-11-04 10:38:52 -05:00
|
|
|
worker.state.global_state.progress.done();
|
2019-11-22 12:14:34 -05:00
|
|
|
|
|
|
|
if !is_prefetch {
|
2019-07-10 18:53:48 -04:00
|
|
|
let mut isolate = worker.isolate.lock().unwrap();
|
2019-11-22 12:14:34 -05:00
|
|
|
return isolate.mod_evaluate(id);
|
2019-07-10 18:53:48 -04:00
|
|
|
}
|
2019-11-22 12:14:34 -05:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
/// Post message to worker as a host.
|
|
|
|
///
|
|
|
|
/// This method blocks current thread.
|
2019-11-19 19:17:05 -05:00
|
|
|
pub fn post_message(
|
|
|
|
self: &Self,
|
|
|
|
buf: Buf,
|
|
|
|
) -> impl Future<Output = Result<(), ErrBox>> {
|
2019-11-16 19:17:47 -05:00
|
|
|
let channels = self.external_channels.lock().unwrap();
|
|
|
|
let mut sender = channels.sender.clone();
|
2019-11-19 19:17:05 -05:00
|
|
|
async move {
|
|
|
|
let result = sender.send(buf).map_err(ErrBox::from).await;
|
|
|
|
drop(sender);
|
|
|
|
result
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2019-11-09 15:07:14 -05:00
|
|
|
/// Get message from worker as a host.
|
2019-11-04 10:38:52 -05:00
|
|
|
pub fn get_message(self: &Self) -> WorkerReceiver {
|
2019-11-09 15:07:14 -05:00
|
|
|
WorkerReceiver {
|
|
|
|
channels: self.external_channels.clone(),
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2019-04-01 21:46:40 -04:00
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
impl Future for Worker {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<(), ErrBox>;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
|
|
|
let mut isolate = inner.isolate.lock().unwrap();
|
|
|
|
isolate.poll_unpin(cx)
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 16:05:41 -04:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
/// This structure wraps worker's resource id to implement future
|
|
|
|
/// that will return message received from worker or None
|
|
|
|
/// if worker's channel has been closed.
|
|
|
|
pub struct WorkerReceiver {
|
2019-11-09 15:07:14 -05:00
|
|
|
channels: Arc<Mutex<WorkerChannels>>,
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for WorkerReceiver {
|
2019-11-16 19:17:47 -05:00
|
|
|
type Output = Result<Option<Buf>, ErrBox>;
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2019-11-16 19:17:47 -05:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
2019-11-09 15:07:14 -05:00
|
|
|
let mut channels = self.channels.lock().unwrap();
|
2019-11-16 19:17:47 -05:00
|
|
|
match channels.receiver.poll_next_unpin(cx) {
|
|
|
|
Poll::Ready(v) => Poll::Ready(Ok(v)),
|
|
|
|
Poll::Pending => Poll::Pending,
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 14:53:16 -04:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-14 19:17:52 -04:00
|
|
|
use crate::flags;
|
2019-11-04 10:38:52 -05:00
|
|
|
use crate::global_state::ThreadSafeGlobalState;
|
2019-05-11 10:23:19 -04:00
|
|
|
use crate::progress::Progress;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::startup_data;
|
2019-04-09 13:11:25 -04:00
|
|
|
use crate::state::ThreadSafeState;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::tokio_util;
|
2019-11-19 19:17:05 -05:00
|
|
|
use futures::executor::block_on;
|
2019-03-14 19:17:52 -04:00
|
|
|
use std::sync::atomic::Ordering;
|
2019-01-01 06:24:05 -05:00
|
|
|
|
2019-11-22 12:46:57 -05:00
|
|
|
pub fn run_in_task<F>(f: F)
|
|
|
|
where
|
|
|
|
F: FnOnce() + Send + 'static,
|
|
|
|
{
|
|
|
|
let fut = futures::future::lazy(move |_cx| {
|
|
|
|
f();
|
|
|
|
Ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
tokio_util::run(fut)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn panic_on_error<I, E, F>(f: F) -> impl Future<Output = Result<I, ()>>
|
|
|
|
where
|
|
|
|
F: Future<Output = Result<I, E>>,
|
|
|
|
E: std::fmt::Debug,
|
|
|
|
{
|
|
|
|
f.map_err(|err| panic!("Future got unexpected error: {:?}", err))
|
|
|
|
}
|
|
|
|
|
2019-01-01 06:24:05 -05:00
|
|
|
#[test]
|
2019-04-16 15:13:42 -04:00
|
|
|
fn execute_mod_esm_imports_a() {
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/esm_imports_a.js")
|
|
|
|
.to_owned();
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2019-11-04 10:38:52 -05:00
|
|
|
let global_state = ThreadSafeGlobalState::new(
|
2019-12-03 17:23:10 -05:00
|
|
|
flags::DenoFlags {
|
|
|
|
argv: vec![String::from("./deno"), module_specifier.to_string()],
|
|
|
|
..flags::DenoFlags::default()
|
|
|
|
},
|
2019-05-11 10:23:19 -04:00
|
|
|
Progress::new(),
|
2019-07-31 17:11:37 -04:00
|
|
|
)
|
|
|
|
.unwrap();
|
2019-11-09 15:07:14 -05:00
|
|
|
let (int, ext) = ThreadSafeState::create_channels();
|
|
|
|
let state = ThreadSafeState::new(
|
|
|
|
global_state,
|
2019-11-24 10:42:30 -05:00
|
|
|
None,
|
2019-11-09 15:07:14 -05:00
|
|
|
Some(module_specifier.clone()),
|
|
|
|
true,
|
|
|
|
int,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
2019-11-16 19:17:47 -05:00
|
|
|
tokio_util::run(async move {
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut worker =
|
2019-11-09 15:07:14 -05:00
|
|
|
Worker::new("TEST".to_string(), StartupData::None, state, ext);
|
2019-11-22 12:14:34 -05:00
|
|
|
let result = worker
|
2019-10-19 17:19:19 -04:00
|
|
|
.execute_mod_async(&module_specifier, None, false)
|
2019-11-22 12:14:34 -05:00
|
|
|
.await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2019-11-22 12:46:57 -05:00
|
|
|
panic_on_error(worker).await
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-04-16 15:13:42 -04:00
|
|
|
assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-05-20 12:06:57 -04:00
|
|
|
// Check that we didn't start the compiler.
|
|
|
|
assert_eq!(metrics.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_circular() {
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/circular1.ts")
|
|
|
|
.to_owned();
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2019-12-03 17:23:10 -05:00
|
|
|
let global_state = ThreadSafeGlobalState::new(
|
|
|
|
flags::DenoFlags {
|
|
|
|
argv: vec![String::from("deno"), module_specifier.to_string()],
|
|
|
|
..flags::DenoFlags::default()
|
|
|
|
},
|
|
|
|
Progress::new(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-11-09 15:07:14 -05:00
|
|
|
let (int, ext) = ThreadSafeState::create_channels();
|
|
|
|
let state = ThreadSafeState::new(
|
|
|
|
global_state,
|
2019-11-24 10:42:30 -05:00
|
|
|
None,
|
2019-11-09 15:07:14 -05:00
|
|
|
Some(module_specifier.clone()),
|
|
|
|
true,
|
|
|
|
int,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
2019-11-16 19:17:47 -05:00
|
|
|
tokio_util::run(async move {
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut worker =
|
2019-11-09 15:07:14 -05:00
|
|
|
Worker::new("TEST".to_string(), StartupData::None, state, ext);
|
2019-11-22 12:14:34 -05:00
|
|
|
let result = worker
|
2019-10-19 17:19:19 -04:00
|
|
|
.execute_mod_async(&module_specifier, None, false)
|
2019-11-22 12:14:34 -05:00
|
|
|
.await;
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2019-11-22 12:46:57 -05:00
|
|
|
panic_on_error(worker).await
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
let metrics = &state_.metrics;
|
2019-09-04 17:16:46 -04:00
|
|
|
// TODO assert_eq!(metrics.resolve_count.load(Ordering::SeqCst), 2);
|
2019-05-20 12:06:57 -04:00
|
|
|
// Check that we didn't start the compiler.
|
|
|
|
assert_eq!(metrics.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-01 06:24:05 -05:00
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2019-05-11 10:23:19 -04:00
|
|
|
#[test]
|
|
|
|
fn execute_006_url_imports() {
|
2019-09-19 14:48:05 -04:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
2019-10-06 15:03:30 -04:00
|
|
|
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-09-19 14:48:05 -04:00
|
|
|
.join("cli/tests/006_url_imports.ts")
|
2019-09-04 17:16:46 -04:00
|
|
|
.to_owned();
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2019-05-11 10:23:19 -04:00
|
|
|
let mut flags = flags::DenoFlags::default();
|
2019-12-03 17:23:10 -05:00
|
|
|
flags.argv = vec![String::from("deno"), module_specifier.to_string()];
|
2019-05-11 10:23:19 -04:00
|
|
|
flags.reload = true;
|
2019-11-04 10:38:52 -05:00
|
|
|
let global_state =
|
2019-12-03 17:23:10 -05:00
|
|
|
ThreadSafeGlobalState::new(flags, Progress::new()).unwrap();
|
2019-11-09 15:07:14 -05:00
|
|
|
let (int, ext) = ThreadSafeState::create_channels();
|
2019-11-04 10:38:52 -05:00
|
|
|
let state = ThreadSafeState::new(
|
|
|
|
global_state.clone(),
|
2019-11-24 10:42:30 -05:00
|
|
|
None,
|
2019-11-04 10:38:52 -05:00
|
|
|
Some(module_specifier.clone()),
|
|
|
|
true,
|
2019-11-09 15:07:14 -05:00
|
|
|
int,
|
2019-11-04 10:38:52 -05:00
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
let global_state_ = global_state.clone();
|
2019-05-11 10:23:19 -04:00
|
|
|
let state_ = state.clone();
|
2019-11-16 19:17:47 -05:00
|
|
|
tokio_util::run(async move {
|
2019-05-11 10:23:19 -04:00
|
|
|
let mut worker = Worker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state,
|
2019-11-09 15:07:14 -05:00
|
|
|
ext,
|
2019-05-11 10:23:19 -04:00
|
|
|
);
|
2019-07-10 18:53:48 -04:00
|
|
|
worker.execute("denoMain()").unwrap();
|
2019-11-22 12:14:34 -05:00
|
|
|
let result = worker
|
2019-10-19 17:19:19 -04:00
|
|
|
.execute_mod_async(&module_specifier, None, false)
|
2019-11-22 12:14:34 -05:00
|
|
|
.await;
|
|
|
|
|
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2019-11-22 12:46:57 -05:00
|
|
|
panic_on_error(worker).await
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2019-05-11 10:23:19 -04:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
assert_eq!(state_.metrics.resolve_count.load(Ordering::SeqCst), 3);
|
2019-05-20 12:06:57 -04:00
|
|
|
// Check that we've only invoked the compiler once.
|
2019-11-04 10:38:52 -05:00
|
|
|
assert_eq!(
|
|
|
|
global_state_.metrics.compiler_starts.load(Ordering::SeqCst),
|
|
|
|
1
|
|
|
|
);
|
2019-09-19 14:48:05 -04:00
|
|
|
drop(http_server_guard);
|
2019-05-11 10:23:19 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
fn create_test_worker() -> Worker {
|
2019-11-09 15:07:14 -05:00
|
|
|
let (int, ext) = ThreadSafeState::create_channels();
|
|
|
|
let state = ThreadSafeState::mock(
|
|
|
|
vec![String::from("./deno"), String::from("hello.js")],
|
|
|
|
int,
|
|
|
|
);
|
|
|
|
let mut worker = Worker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state,
|
|
|
|
ext,
|
|
|
|
);
|
2019-07-10 18:53:48 -04:00
|
|
|
worker.execute("denoMain()").unwrap();
|
|
|
|
worker.execute("workerMain()").unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_worker_messages() {
|
2019-11-22 12:46:57 -05:00
|
|
|
run_in_task(|| {
|
2019-04-08 17:10:00 -04:00
|
|
|
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 16:07:24 -04:00
|
|
|
delete window.onmessage;
|
2019-04-08 17:10:00 -04:00
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
console.assert(e.data === "hi");
|
|
|
|
}
|
|
|
|
postMessage([1, 2, 3]);
|
|
|
|
console.log("after postMessage");
|
|
|
|
}
|
|
|
|
"#;
|
2019-07-10 18:53:48 -04:00
|
|
|
worker.execute(source).unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
let worker_ = worker.clone();
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2019-11-22 12:14:34 -05:00
|
|
|
let fut = async move {
|
|
|
|
let r = worker.await;
|
|
|
|
r.unwrap();
|
|
|
|
Ok(())
|
|
|
|
};
|
|
|
|
|
|
|
|
tokio::spawn(fut.boxed().compat());
|
2019-04-08 17:10:00 -04:00
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
|
|
|
|
2019-11-19 19:17:05 -05:00
|
|
|
let r = block_on(worker_.post_message(msg));
|
2019-04-08 17:10:00 -04:00
|
|
|
assert!(r.is_ok());
|
|
|
|
|
2019-11-19 19:17:05 -05:00
|
|
|
let maybe_msg = block_on(worker_.get_message()).unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
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();
|
2019-11-19 19:17:05 -05:00
|
|
|
let r = block_on(worker_.post_message(msg));
|
2019-04-08 17:10:00 -04:00
|
|
|
assert!(r.is_ok());
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn removed_from_resource_table_on_close() {
|
2019-11-22 12:46:57 -05:00
|
|
|
run_in_task(|| {
|
2019-04-08 17:10:00 -04:00
|
|
|
let mut worker = create_test_worker();
|
2019-07-10 18:53:48 -04:00
|
|
|
worker
|
|
|
|
.execute("onmessage = () => { delete window.onmessage; }")
|
|
|
|
.unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
let worker_ = worker.clone();
|
2019-04-12 18:39:31 -04:00
|
|
|
let worker_future = worker
|
2019-11-16 19:17:47 -05:00
|
|
|
.then(move |r| {
|
2019-04-08 17:10:00 -04:00
|
|
|
println!("workers.rs after resource close");
|
2019-07-10 18:53:48 -04:00
|
|
|
r.unwrap();
|
2019-11-16 19:17:47 -05:00
|
|
|
futures::future::ok(())
|
2019-07-31 17:11:37 -04:00
|
|
|
})
|
|
|
|
.shared();
|
2019-04-12 18:39:31 -04:00
|
|
|
|
|
|
|
let worker_future_ = worker_future.clone();
|
2019-11-16 19:17:47 -05:00
|
|
|
tokio::spawn(
|
|
|
|
worker_future_
|
|
|
|
.then(|_: Result<(), ()>| futures::future::ok(()))
|
|
|
|
.compat(),
|
|
|
|
);
|
2019-04-08 17:10:00 -04:00
|
|
|
|
|
|
|
let msg = json!("hi").to_string().into_boxed_str().into_boxed_bytes();
|
2019-11-19 19:17:05 -05:00
|
|
|
let r = block_on(worker_.post_message(msg));
|
2019-04-08 17:10:00 -04:00
|
|
|
assert!(r.is_ok());
|
|
|
|
|
2019-11-19 19:17:05 -05:00
|
|
|
block_on(worker_future).unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
})
|
|
|
|
}
|
2019-04-16 15:13:42 -04:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_resolve_error() {
|
2019-11-22 12:46:57 -05:00
|
|
|
run_in_task(|| {
|
2019-06-19 22:07:01 -04:00
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut worker = create_test_worker();
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
core: clearly define when module lookup is path-based vs URL-based
The rules are now as follows:
* In `import` statements, as mandated by the WHATWG specification,
the import specifier is always treated as a URL.
If it is a relative URL, it must start with either / or ./ or ../
* A script name passed to deno as a command line argument may be either
an absolute URL or a local path.
- If the name starts with a valid URI scheme followed by a colon, e.g.
'http:', 'https:', 'file:', 'foo+bar:', it always interpreted as a
URL (even if Deno doesn't support the indicated protocol).
- Otherwise, the script name is interpreted as a local path. The local
path may be relative, and operating system semantics determine how
it is resolved. Prefixing a relative path with ./ is not required.
2019-07-08 03:55:24 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
|
2019-11-19 19:17:05 -05:00
|
|
|
let result =
|
|
|
|
block_on(worker.execute_mod_async(&module_specifier, None, false));
|
2019-05-20 12:06:57 -04:00
|
|
|
assert!(result.is_err());
|
|
|
|
})
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_002_hello() {
|
2019-11-22 12:46:57 -05:00
|
|
|
run_in_task(|| {
|
2019-05-20 12:06:57 -04:00
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut worker = create_test_worker();
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("tests/002_hello.ts")
|
|
|
|
.to_owned();
|
2019-06-12 15:00:08 -04:00
|
|
|
let module_specifier =
|
2019-09-04 17:16:46 -04:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2019-11-19 19:17:05 -05:00
|
|
|
let result =
|
|
|
|
block_on(worker.execute_mod_async(&module_specifier, None, false));
|
2019-05-20 12:06:57 -04:00
|
|
|
assert!(result.is_ok());
|
|
|
|
})
|
2019-04-16 15:13:42 -04:00
|
|
|
}
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|