2020-01-02 20:13:47 +00:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2020-09-06 00:34:02 +00:00
|
|
|
|
2020-09-06 19:44:29 +00:00
|
|
|
use crate::fmt_errors::JsError;
|
2020-06-02 02:44:17 +00:00
|
|
|
use crate::global_state::GlobalState;
|
2020-04-03 17:40:11 +00:00
|
|
|
use crate::inspector::DenoInspector;
|
2020-09-11 13:18:49 +00:00
|
|
|
use crate::js;
|
2020-09-18 18:39:47 +00:00
|
|
|
use crate::metrics::Metrics;
|
2019-10-11 18:41:54 +00:00
|
|
|
use crate::ops;
|
2020-06-02 02:44:17 +00:00
|
|
|
use crate::ops::io::get_stdio;
|
2020-09-19 23:17:35 +00:00
|
|
|
use crate::ops::timers;
|
|
|
|
use crate::ops::worker_host::WorkerId;
|
|
|
|
use crate::ops::worker_host::WorkersTable;
|
|
|
|
use crate::permissions::Permissions;
|
|
|
|
use crate::state::CliModuleLoader;
|
2020-09-14 16:48:57 +00:00
|
|
|
use deno_core::error::AnyError;
|
2020-09-16 18:28:07 +00:00
|
|
|
use deno_core::url::Url;
|
2020-09-06 19:44:29 +00:00
|
|
|
use deno_core::JsRuntime;
|
2020-02-18 15:08:18 +00:00
|
|
|
use deno_core::ModuleId;
|
2020-01-05 16:56:18 +00:00
|
|
|
use deno_core::ModuleSpecifier;
|
2020-09-11 13:18:49 +00:00
|
|
|
use deno_core::RuntimeOptions;
|
|
|
|
use deno_core::Snapshot;
|
2019-11-17 00:17:47 +00:00
|
|
|
use futures::channel::mpsc;
|
|
|
|
use futures::future::FutureExt;
|
|
|
|
use futures::stream::StreamExt;
|
2020-01-17 23:43:53 +00:00
|
|
|
use futures::task::AtomicWaker;
|
2020-09-18 18:39:47 +00:00
|
|
|
use rand::rngs::StdRng;
|
|
|
|
use rand::SeedableRng;
|
2019-08-09 23:33:59 +00:00
|
|
|
use std::env;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::future::Future;
|
2020-01-21 16:50:06 +00:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::pin::Pin;
|
2020-02-15 00:18:36 +00:00
|
|
|
use std::rc::Rc;
|
2019-06-05 20:35:38 +00:00
|
|
|
use std::sync::Arc;
|
2019-11-17 00:17:47 +00:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2020-01-08 14:06:04 +00:00
|
|
|
use tokio::sync::Mutex as AsyncMutex;
|
2018-10-05 17:21:15 +00:00
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
/// Events that are sent to host from child
|
|
|
|
/// worker.
|
|
|
|
pub enum WorkerEvent {
|
2020-09-06 00:34:02 +00:00
|
|
|
Message(Box<[u8]>),
|
2020-09-14 16:48:57 +00:00
|
|
|
Error(AnyError),
|
|
|
|
TerminalError(AnyError),
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
pub struct WorkerChannelsInternal {
|
|
|
|
pub sender: mpsc::Sender<WorkerEvent>,
|
2020-09-06 00:34:02 +00:00
|
|
|
pub receiver: mpsc::Receiver<Box<[u8]>>,
|
2020-02-03 23:08:44 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WorkerHandle {
|
2020-09-06 00:34:02 +00:00
|
|
|
pub sender: mpsc::Sender<Box<[u8]>>,
|
2020-02-11 09:04:59 +00:00
|
|
|
pub receiver: Arc<AsyncMutex<mpsc::Receiver<WorkerEvent>>>,
|
2020-02-05 07:40:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
impl WorkerHandle {
|
|
|
|
/// Post message to worker as a host.
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn post_message(&self, buf: Box<[u8]>) -> Result<(), AnyError> {
|
2020-02-11 09:04:59 +00:00
|
|
|
let mut sender = self.sender.clone();
|
2020-08-25 22:22:15 +00:00
|
|
|
sender.try_send(buf)?;
|
|
|
|
Ok(())
|
2020-02-05 07:40:38 +00:00
|
|
|
}
|
|
|
|
|
2020-05-17 04:50:38 +00:00
|
|
|
/// Get the event with lock.
|
|
|
|
/// Return error if more than one listener tries to get event
|
2020-09-14 16:48:57 +00:00
|
|
|
pub async fn get_event(&self) -> Result<Option<WorkerEvent>, AnyError> {
|
2020-05-17 04:50:38 +00:00
|
|
|
let mut receiver = self.receiver.try_lock()?;
|
|
|
|
Ok(receiver.next().await)
|
2020-02-05 07:40:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 09:04:59 +00:00
|
|
|
fn create_channels() -> (WorkerChannelsInternal, WorkerHandle) {
|
2020-09-06 00:34:02 +00:00
|
|
|
let (in_tx, in_rx) = mpsc::channel::<Box<[u8]>>(1);
|
2020-02-11 09:04:59 +00:00
|
|
|
let (out_tx, out_rx) = mpsc::channel::<WorkerEvent>(1);
|
|
|
|
let internal_channels = WorkerChannelsInternal {
|
2020-02-05 07:40:38 +00:00
|
|
|
sender: out_tx,
|
2020-02-11 09:04:59 +00:00
|
|
|
receiver: in_rx,
|
|
|
|
};
|
|
|
|
let external_channels = WorkerHandle {
|
2020-02-05 07:40:38 +00:00
|
|
|
sender: in_tx,
|
|
|
|
receiver: Arc::new(AsyncMutex::new(out_rx)),
|
2020-02-11 09:04:59 +00:00
|
|
|
};
|
2020-02-05 07:40:38 +00:00
|
|
|
(internal_channels, external_channels)
|
|
|
|
}
|
|
|
|
|
2020-01-21 16:50:06 +00:00
|
|
|
/// Worker is a CLI wrapper for `deno_core::Isolate`.
|
|
|
|
///
|
|
|
|
/// It provides infrastructure to communicate with a worker and
|
|
|
|
/// consequently between workers.
|
|
|
|
///
|
|
|
|
/// This struct is meant to be used as a base struct for concrete
|
|
|
|
/// type of worker that registers set of ops.
|
|
|
|
///
|
|
|
|
/// Currently there are three types of workers:
|
|
|
|
/// - `MainWorker`
|
|
|
|
/// - `CompilerWorker`
|
|
|
|
/// - `WebWorker`
|
2019-04-08 21:10:00 +00:00
|
|
|
pub struct Worker {
|
2019-11-04 15:38:52 +00:00
|
|
|
pub name: String,
|
2020-09-10 13:57:45 +00:00
|
|
|
pub isolate: JsRuntime,
|
2020-05-24 20:28:19 +00:00
|
|
|
pub inspector: Option<Box<DenoInspector>>,
|
2020-02-11 09:04:59 +00:00
|
|
|
pub waker: AtomicWaker,
|
|
|
|
pub(crate) internal_channels: WorkerChannelsInternal,
|
|
|
|
external_channels: WorkerHandle,
|
2020-09-19 23:17:35 +00:00
|
|
|
should_break_on_first_statement: bool,
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
impl Worker {
|
2020-09-19 23:17:35 +00:00
|
|
|
#[allow(clippy::too_many_arguments)]
|
2020-08-18 16:30:13 +00:00
|
|
|
pub fn new(
|
|
|
|
name: String,
|
2020-09-11 13:18:49 +00:00
|
|
|
startup_snapshot: Option<Snapshot>,
|
2020-09-19 23:17:35 +00:00
|
|
|
permissions: Permissions,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
global_state: Arc<GlobalState>,
|
|
|
|
state: Rc<CliModuleLoader>,
|
|
|
|
is_main: bool,
|
|
|
|
is_internal: bool,
|
2020-08-18 16:30:13 +00:00
|
|
|
) -> Self {
|
2020-09-15 20:15:01 +00:00
|
|
|
let global_state_ = global_state.clone();
|
2020-09-15 01:23:48 +00:00
|
|
|
|
2020-09-11 13:18:49 +00:00
|
|
|
let mut isolate = JsRuntime::new(RuntimeOptions {
|
2020-09-19 23:17:35 +00:00
|
|
|
module_loader: Some(state),
|
2020-09-11 13:18:49 +00:00
|
|
|
startup_snapshot,
|
2020-09-15 01:23:48 +00:00
|
|
|
js_error_create_fn: Some(Box::new(move |core_js_error| {
|
2020-09-15 20:15:01 +00:00
|
|
|
JsError::create(core_js_error, &global_state_.ts_compiler)
|
2020-09-15 01:23:48 +00:00
|
|
|
})),
|
2020-09-11 13:18:49 +00:00
|
|
|
..Default::default()
|
|
|
|
});
|
2020-09-10 13:57:45 +00:00
|
|
|
{
|
|
|
|
let op_state = isolate.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
op_state.get_error_class_fn = &crate::errors::get_error_class_name;
|
2020-09-15 20:15:01 +00:00
|
|
|
|
|
|
|
let ca_file = global_state.flags.ca_file.as_deref();
|
|
|
|
let client = crate::http_util::create_http_client(ca_file).unwrap();
|
|
|
|
op_state.put(client);
|
2020-09-18 18:39:47 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
op_state.put(timers::GlobalTimer::default());
|
|
|
|
op_state.put(timers::StartTime::now());
|
2020-09-18 18:39:47 +00:00
|
|
|
|
|
|
|
if let Some(seed) = global_state.flags.seed {
|
|
|
|
op_state.put(StdRng::seed_from_u64(seed));
|
|
|
|
}
|
|
|
|
|
|
|
|
op_state.put(Metrics::default());
|
2020-09-19 23:17:35 +00:00
|
|
|
|
|
|
|
op_state.put(WorkersTable::default());
|
|
|
|
op_state.put(WorkerId::default());
|
|
|
|
|
|
|
|
op_state.put(permissions);
|
|
|
|
|
|
|
|
op_state.put(main_module);
|
|
|
|
op_state.put(global_state.clone());
|
2020-09-10 13:57:45 +00:00
|
|
|
}
|
2020-05-24 20:28:19 +00:00
|
|
|
let inspector = {
|
|
|
|
global_state
|
|
|
|
.flags
|
|
|
|
.inspect
|
|
|
|
.or(global_state.flags.inspect_brk)
|
2020-09-19 23:17:35 +00:00
|
|
|
.filter(|_| !is_internal)
|
2020-05-24 20:28:19 +00:00
|
|
|
.map(|inspector_host| DenoInspector::new(&mut isolate, inspector_host))
|
|
|
|
};
|
2019-11-09 20:07:14 +00:00
|
|
|
|
2020-09-19 23:17:35 +00:00
|
|
|
let should_break_on_first_statement = inspector.is_some()
|
|
|
|
&& is_main
|
|
|
|
&& global_state.flags.inspect_brk.is_some();
|
|
|
|
|
2020-02-05 07:40:38 +00:00
|
|
|
let (internal_channels, external_channels) = create_channels();
|
|
|
|
|
2019-11-04 15:38:52 +00:00
|
|
|
Self {
|
|
|
|
name,
|
2020-02-03 23:08:44 +00:00
|
|
|
isolate,
|
2020-05-24 20:28:19 +00:00
|
|
|
inspector,
|
2020-02-11 09:04:59 +00:00
|
|
|
waker: AtomicWaker::new(),
|
|
|
|
internal_channels,
|
2020-01-21 16:50:06 +00:00
|
|
|
external_channels,
|
2020-09-19 23:17:35 +00:00
|
|
|
should_break_on_first_statement,
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
2018-12-07 04:05:36 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 23:33:59 +00:00
|
|
|
/// Same as execute2() but the filename defaults to "$CWD/__anonymous__".
|
2020-09-14 16:48:57 +00:00
|
|
|
pub fn execute(&mut self, js_source: &str) -> Result<(), AnyError> {
|
2019-08-09 23:33:59 +00: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 19:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// 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,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2020-02-03 23:08:44 +00:00
|
|
|
self.isolate.execute(js_filename, js_source)
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 15:08:18 +00:00
|
|
|
/// Loads and instantiates specified JavaScript module.
|
|
|
|
pub async fn preload_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<ModuleId, AnyError> {
|
2020-02-18 15:08:18 +00:00
|
|
|
self.isolate.load_module(module_specifier, None).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_module(
|
2019-06-05 20:35:38 +00:00
|
|
|
&mut self,
|
2019-06-12 19:00:08 +00:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2020-02-18 15:08:18 +00:00
|
|
|
let id = self.preload_module(module_specifier).await?;
|
2020-05-12 19:08:22 +00:00
|
|
|
self.wait_for_inspector_session();
|
2020-02-18 15:08:18 +00:00
|
|
|
self.isolate.mod_evaluate(id)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads, instantiates and executes provided source code
|
|
|
|
/// as module.
|
|
|
|
pub async fn execute_module_from_code(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
code: String,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<(), AnyError> {
|
2020-02-18 15:08:18 +00:00
|
|
|
let id = self
|
|
|
|
.isolate
|
|
|
|
.load_module(module_specifier, Some(code))
|
|
|
|
.await?;
|
2020-05-12 19:08:22 +00:00
|
|
|
self.wait_for_inspector_session();
|
2020-02-18 15:08:18 +00:00
|
|
|
self.isolate.mod_evaluate(id)
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
2019-11-04 15:38:52 +00:00
|
|
|
|
2020-02-03 23:08:44 +00:00
|
|
|
/// Returns a way to communicate with the Worker from other threads.
|
2020-02-11 09:04:59 +00:00
|
|
|
pub fn thread_safe_handle(&self) -> WorkerHandle {
|
2020-02-03 23:08:44 +00:00
|
|
|
self.external_channels.clone()
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
2020-05-12 19:08:22 +00:00
|
|
|
|
2020-05-24 20:28:19 +00:00
|
|
|
fn wait_for_inspector_session(&mut self) {
|
2020-09-19 23:17:35 +00:00
|
|
|
if self.should_break_on_first_statement {
|
2020-05-12 19:08:22 +00:00
|
|
|
self
|
2020-05-24 20:28:19 +00:00
|
|
|
.inspector
|
2020-05-12 19:08:22 +00:00
|
|
|
.as_mut()
|
|
|
|
.unwrap()
|
|
|
|
.wait_for_session_and_break_on_next_statement()
|
|
|
|
}
|
|
|
|
}
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2019-04-02 01:46:40 +00:00
|
|
|
|
2020-03-27 20:09:51 +00:00
|
|
|
impl Drop for Worker {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// The Isolate object must outlive the Inspector object, but this is
|
|
|
|
// currently not enforced by the type system.
|
2020-05-24 20:28:19 +00:00
|
|
|
self.inspector.take();
|
2020-03-27 20:09:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-08 21:10:00 +00:00
|
|
|
impl Future for Worker {
|
2020-09-14 16:48:57 +00:00
|
|
|
type Output = Result<(), AnyError>;
|
2019-03-14 23:17:52 +00:00
|
|
|
|
2019-11-17 00:17:47 +00:00
|
|
|
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
|
|
|
|
let inner = self.get_mut();
|
2020-04-09 22:15:17 +00:00
|
|
|
|
2020-05-12 19:08:22 +00:00
|
|
|
// We always poll the inspector if it exists.
|
2020-05-24 20:28:19 +00:00
|
|
|
let _ = inner.inspector.as_mut().map(|i| i.poll_unpin(cx));
|
2020-02-11 09:04:59 +00:00
|
|
|
inner.waker.register(cx.waker());
|
2020-02-03 23:08:44 +00:00
|
|
|
inner.isolate.poll_unpin(cx)
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 20:05:41 +00:00
|
|
|
|
2020-04-21 13:48:44 +00:00
|
|
|
impl Deref for Worker {
|
2020-09-10 13:57:45 +00:00
|
|
|
type Target = JsRuntime;
|
2020-04-21 13:48:44 +00:00
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.isolate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DerefMut for Worker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.isolate
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-21 16:50:06 +00:00
|
|
|
/// This worker is created and used by Deno executable.
|
|
|
|
///
|
|
|
|
/// It provides ops available in the `Deno` namespace.
|
|
|
|
///
|
2020-05-14 04:38:42 +00:00
|
|
|
/// All WebWorkers created during program execution are descendants of
|
2020-01-21 16:50:06 +00:00
|
|
|
/// this worker.
|
|
|
|
pub struct MainWorker(Worker);
|
|
|
|
|
|
|
|
impl MainWorker {
|
2020-06-02 02:44:17 +00:00
|
|
|
// TODO(ry) combine MainWorker::new and MainWorker::create.
|
2020-09-11 13:18:49 +00:00
|
|
|
fn new(
|
|
|
|
name: String,
|
|
|
|
startup_snapshot: Option<Snapshot>,
|
2020-09-19 23:17:35 +00:00
|
|
|
permissions: Permissions,
|
|
|
|
main_module: ModuleSpecifier,
|
|
|
|
global_state: Arc<GlobalState>,
|
2020-09-11 13:18:49 +00:00
|
|
|
) -> Self {
|
2020-09-19 23:17:35 +00:00
|
|
|
let loader = CliModuleLoader::new(global_state.maybe_import_map.clone());
|
|
|
|
let mut worker = Worker::new(
|
|
|
|
name,
|
|
|
|
startup_snapshot,
|
|
|
|
permissions,
|
|
|
|
main_module,
|
|
|
|
global_state,
|
|
|
|
loader,
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
);
|
2020-01-21 16:50:06 +00:00
|
|
|
{
|
2020-09-10 13:57:45 +00:00
|
|
|
ops::runtime::init(&mut worker);
|
|
|
|
ops::runtime_compiler::init(&mut worker);
|
|
|
|
ops::errors::init(&mut worker);
|
|
|
|
ops::fetch::init(&mut worker);
|
|
|
|
ops::websocket::init(&mut worker);
|
|
|
|
ops::fs::init(&mut worker);
|
|
|
|
ops::fs_events::init(&mut worker);
|
2020-09-17 17:13:20 +00:00
|
|
|
ops::reg_json_sync(
|
|
|
|
&mut worker,
|
|
|
|
"op_domain_to_ascii",
|
|
|
|
deno_web::op_domain_to_ascii,
|
|
|
|
);
|
2020-09-10 13:57:45 +00:00
|
|
|
ops::io::init(&mut worker);
|
|
|
|
ops::plugin::init(&mut worker);
|
|
|
|
ops::net::init(&mut worker);
|
|
|
|
ops::tls::init(&mut worker);
|
|
|
|
ops::os::init(&mut worker);
|
|
|
|
ops::permissions::init(&mut worker);
|
|
|
|
ops::process::init(&mut worker);
|
|
|
|
ops::random::init(&mut worker);
|
|
|
|
ops::repl::init(&mut worker);
|
2020-09-17 16:09:50 +00:00
|
|
|
ops::reg_json_sync(&mut worker, "op_close", deno_core::op_close);
|
|
|
|
ops::reg_json_sync(&mut worker, "op_resources", deno_core::op_resources);
|
2020-09-10 13:57:45 +00:00
|
|
|
ops::signal::init(&mut worker);
|
|
|
|
ops::timers::init(&mut worker);
|
|
|
|
ops::tty::init(&mut worker);
|
|
|
|
ops::worker_host::init(&mut worker);
|
2020-01-21 16:50:06 +00:00
|
|
|
}
|
|
|
|
Self(worker)
|
|
|
|
}
|
2020-06-02 02:44:17 +00:00
|
|
|
|
|
|
|
pub fn create(
|
2020-08-18 16:30:13 +00:00
|
|
|
global_state: &Arc<GlobalState>,
|
2020-06-02 02:44:17 +00:00
|
|
|
main_module: ModuleSpecifier,
|
2020-09-14 16:48:57 +00:00
|
|
|
) -> Result<MainWorker, AnyError> {
|
2020-06-02 02:44:17 +00:00
|
|
|
let mut worker = MainWorker::new(
|
|
|
|
"main".to_string(),
|
2020-09-11 13:18:49 +00:00
|
|
|
Some(js::deno_isolate_init()),
|
2020-09-19 23:17:35 +00:00
|
|
|
global_state.permissions.clone(),
|
|
|
|
main_module,
|
|
|
|
global_state.clone(),
|
2020-06-02 02:44:17 +00:00
|
|
|
);
|
|
|
|
{
|
2020-09-10 13:57:45 +00:00
|
|
|
let op_state = worker.op_state();
|
|
|
|
let mut op_state = op_state.borrow_mut();
|
|
|
|
let t = &mut op_state.resource_table;
|
2020-09-06 00:34:02 +00:00
|
|
|
let (stdin, stdout, stderr) = get_stdio();
|
2020-07-09 19:06:51 +00:00
|
|
|
if let Some(stream) = stdin {
|
|
|
|
t.add("stdin", Box::new(stream));
|
|
|
|
}
|
|
|
|
if let Some(stream) = stdout {
|
|
|
|
t.add("stdout", Box::new(stream));
|
|
|
|
}
|
|
|
|
if let Some(stream) = stderr {
|
|
|
|
t.add("stderr", Box::new(stream));
|
|
|
|
}
|
2020-06-02 02:44:17 +00:00
|
|
|
}
|
|
|
|
worker.execute("bootstrap.mainRuntime()")?;
|
|
|
|
Ok(worker)
|
|
|
|
}
|
2020-01-21 16:50:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for MainWorker {
|
|
|
|
type Target = Worker;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2019-11-04 15:38:52 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:50:06 +00:00
|
|
|
impl DerefMut for MainWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-18 18:53:16 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-03-14 23:17:52 +00:00
|
|
|
use crate::flags;
|
2020-02-07 04:05:02 +00:00
|
|
|
use crate::global_state::GlobalState;
|
2020-09-11 13:18:49 +00:00
|
|
|
use crate::js;
|
2019-04-08 21:10:00 +00:00
|
|
|
use crate::tokio_util;
|
2019-03-14 23:17:52 +00:00
|
|
|
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-09-04 21:16:46 +00:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2020-02-02 21:55:22 +00:00
|
|
|
.join("cli/tests/esm_imports_a.js");
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
2019-09-04 21:16:46 +00:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-02-26 10:52:15 +00:00
|
|
|
let global_state = GlobalState::new(flags::Flags::default()).unwrap();
|
2020-09-19 23:17:35 +00:00
|
|
|
let global_state_ = global_state.clone();
|
2020-08-18 16:30:13 +00:00
|
|
|
tokio_util::run_basic(async {
|
2020-09-19 23:17:35 +00:00
|
|
|
let mut worker = MainWorker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
None,
|
|
|
|
global_state.permissions.clone(),
|
|
|
|
module_specifier.clone(),
|
|
|
|
global_state_,
|
|
|
|
);
|
2020-02-18 15:08:18 +00:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2019-11-22 17:14:34 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-02-03 23:08:44 +00:00
|
|
|
if let Err(e) = (&mut *worker).await {
|
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-11-17 00:17:47 +00:00
|
|
|
});
|
2019-05-20 16:06:57 +00:00
|
|
|
// Check that we didn't start the compiler.
|
2020-09-19 23:17:35 +00:00
|
|
|
assert_eq!(global_state.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-30 22:21:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn execute_mod_circular() {
|
2019-09-04 21:16:46 +00:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 14:59:44 +00:00
|
|
|
.join("tests/circular1.ts");
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
2019-09-04 21:16:46 +00:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-02-26 10:52:15 +00:00
|
|
|
let global_state = GlobalState::new(flags::Flags::default()).unwrap();
|
2020-09-19 23:17:35 +00:00
|
|
|
let global_state_ = global_state.clone();
|
2020-08-18 16:30:13 +00:00
|
|
|
tokio_util::run_basic(async {
|
2020-09-19 23:17:35 +00:00
|
|
|
let mut worker = MainWorker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
None,
|
|
|
|
global_state_.permissions.clone(),
|
|
|
|
module_specifier.clone(),
|
|
|
|
global_state_,
|
|
|
|
);
|
2020-02-18 15:08:18 +00:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2019-11-22 17:14:34 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-02-03 23:08:44 +00:00
|
|
|
if let Err(e) = (&mut *worker).await {
|
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-11-17 00:17:47 +00:00
|
|
|
});
|
2019-03-14 23:17:52 +00:00
|
|
|
|
2019-05-20 16:06:57 +00:00
|
|
|
// Check that we didn't start the compiler.
|
2020-09-19 23:17:35 +00:00
|
|
|
assert_eq!(global_state.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-01 11:24:05 +00:00
|
|
|
}
|
2019-04-08 21:10:00 +00:00
|
|
|
|
2020-02-03 23:08:44 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_006_url_imports() {
|
2020-08-10 21:31:05 +00:00
|
|
|
let _http_server_guard = test_util::http_server();
|
2019-09-04 21:16:46 +00:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 14:59:44 +00:00
|
|
|
.join("cli/tests/006_url_imports.ts");
|
2019-06-12 19:00:08 +00:00
|
|
|
let module_specifier =
|
2019-09-04 21:16:46 +00:00
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
2020-02-26 10:52:15 +00:00
|
|
|
let flags = flags::Flags {
|
2020-02-04 19:24:33 +00:00
|
|
|
subcommand: flags::DenoSubcommand::Run {
|
|
|
|
script: module_specifier.to_string(),
|
|
|
|
},
|
|
|
|
reload: true,
|
2020-02-26 10:52:15 +00:00
|
|
|
..flags::Flags::default()
|
2020-02-04 19:24:33 +00:00
|
|
|
};
|
2020-02-07 04:05:02 +00:00
|
|
|
let global_state = GlobalState::new(flags).unwrap();
|
2020-02-03 23:08:44 +00:00
|
|
|
let mut worker = MainWorker::new(
|
|
|
|
"TEST".to_string(),
|
2020-09-11 13:18:49 +00:00
|
|
|
Some(js::deno_isolate_init()),
|
2020-09-19 23:17:35 +00:00
|
|
|
global_state.permissions.clone(),
|
|
|
|
module_specifier.clone(),
|
|
|
|
global_state.clone(),
|
2020-02-03 23:08:44 +00:00
|
|
|
);
|
2020-04-24 23:03:45 +00:00
|
|
|
worker.execute("bootstrap.mainRuntime()").unwrap();
|
2020-02-18 15:08:18 +00:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2020-02-03 23:08:44 +00:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
|
|
|
if let Err(e) = (&mut *worker).await {
|
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-05-20 16:06:57 +00:00
|
|
|
// Check that we've only invoked the compiler once.
|
2020-09-19 23:17:35 +00:00
|
|
|
assert_eq!(global_state.compiler_starts.load(Ordering::SeqCst), 1);
|
2019-05-11 14:23:19 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 16:50:06 +00:00
|
|
|
fn create_test_worker() -> MainWorker {
|
2020-09-19 23:17:35 +00:00
|
|
|
let main_module =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("./hello.js").unwrap();
|
|
|
|
let global_state = GlobalState::mock(vec!["deno".to_string()], None);
|
2020-01-21 16:50:06 +00:00
|
|
|
let mut worker = MainWorker::new(
|
2019-11-09 20:07:14 +00:00
|
|
|
"TEST".to_string(),
|
2020-09-11 13:18:49 +00:00
|
|
|
Some(js::deno_isolate_init()),
|
2020-09-19 23:17:35 +00:00
|
|
|
Permissions::allow_all(),
|
|
|
|
main_module,
|
|
|
|
global_state,
|
2019-11-09 20:07:14 +00:00
|
|
|
);
|
2020-04-24 23:03:45 +00:00
|
|
|
worker.execute("bootstrap.mainRuntime()").unwrap();
|
2019-04-08 21:10:00 +00:00
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:29:28 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_resolve_error() {
|
|
|
|
// "foo" is not a valid module specifier so this should return an error.
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path("does-not-exist").unwrap();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_err());
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 14:29:28 +00:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_mod_002_hello() {
|
|
|
|
// This assumes cwd is project root (an assumption made throughout the
|
|
|
|
// tests).
|
|
|
|
let mut worker = create_test_worker();
|
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
|
|
|
.join("cli/tests/002_hello.ts");
|
|
|
|
let module_specifier =
|
|
|
|
ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap();
|
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
|
|
|
assert!(result.is_ok());
|
2019-04-16 19:13:42 +00:00
|
|
|
}
|
2018-09-18 00:41:13 +00:00
|
|
|
}
|