2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-07-10 18:53:48 -04:00
|
|
|
use crate::fmt_errors::JSError;
|
2020-04-03 13:40:11 -04:00
|
|
|
use crate::inspector::DenoInspector;
|
2019-10-11 14:41:54 -04:00
|
|
|
use crate::ops;
|
2020-04-03 13:40:11 -04:00
|
|
|
use crate::state::DebugType;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::Buf;
|
|
|
|
use deno_core::ErrBox;
|
2020-02-18 10:08:18 -05:00
|
|
|
use deno_core::ModuleId;
|
2020-01-05 11:56:18 -05:00
|
|
|
use deno_core::ModuleSpecifier;
|
|
|
|
use deno_core::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;
|
2020-01-17 18:43:53 -05:00
|
|
|
use futures::task::AtomicWaker;
|
2019-08-09 19:33:59 -04:00
|
|
|
use std::env;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::future::Future;
|
2020-01-21 11:50:06 -05:00
|
|
|
use std::ops::Deref;
|
|
|
|
use std::ops::DerefMut;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::pin::Pin;
|
2020-02-14 19:18:36 -05:00
|
|
|
use std::rc::Rc;
|
2019-06-05 16:35:38 -04:00
|
|
|
use std::sync::Arc;
|
2019-11-16 19:17:47 -05:00
|
|
|
use std::task::Context;
|
|
|
|
use std::task::Poll;
|
2020-01-08 09:06:04 -05:00
|
|
|
use tokio::sync::Mutex as AsyncMutex;
|
2019-08-09 19:33:59 -04:00
|
|
|
use url::Url;
|
2018-10-05 13:21:15 -04:00
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
/// Events that are sent to host from child
|
|
|
|
/// worker.
|
|
|
|
pub enum WorkerEvent {
|
|
|
|
Message(Buf),
|
|
|
|
Error(ErrBox),
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
pub struct WorkerChannelsInternal {
|
|
|
|
pub sender: mpsc::Sender<WorkerEvent>,
|
|
|
|
pub receiver: mpsc::Receiver<Buf>,
|
2020-02-03 18:08:44 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct WorkerHandle {
|
|
|
|
pub sender: mpsc::Sender<Buf>,
|
|
|
|
pub receiver: Arc<AsyncMutex<mpsc::Receiver<WorkerEvent>>>,
|
|
|
|
// terminate_channel
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
impl WorkerHandle {
|
|
|
|
pub fn terminate(&self) {
|
|
|
|
todo!()
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
/// Post message to worker as a host.
|
|
|
|
pub async fn post_message(&self, buf: Buf) -> Result<(), ErrBox> {
|
|
|
|
let mut sender = self.sender.clone();
|
|
|
|
sender.send(buf).map_err(ErrBox::from).await
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
// TODO: should use `try_lock` and return error if
|
|
|
|
// more than one listener tries to get event
|
|
|
|
pub async fn get_event(&self) -> Option<WorkerEvent> {
|
|
|
|
let mut receiver = self.receiver.lock().await;
|
|
|
|
receiver.next().await
|
2020-02-05 02:40:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 04:04:59 -05:00
|
|
|
fn create_channels() -> (WorkerChannelsInternal, WorkerHandle) {
|
2020-02-05 02:40:38 -05:00
|
|
|
let (in_tx, in_rx) = mpsc::channel::<Buf>(1);
|
2020-02-11 04:04:59 -05:00
|
|
|
let (out_tx, out_rx) = mpsc::channel::<WorkerEvent>(1);
|
|
|
|
let internal_channels = WorkerChannelsInternal {
|
2020-02-05 02:40:38 -05:00
|
|
|
sender: out_tx,
|
2020-02-11 04:04:59 -05:00
|
|
|
receiver: in_rx,
|
|
|
|
};
|
|
|
|
let external_channels = WorkerHandle {
|
2020-02-05 02:40:38 -05:00
|
|
|
sender: in_tx,
|
|
|
|
receiver: Arc::new(AsyncMutex::new(out_rx)),
|
2020-02-11 04:04:59 -05:00
|
|
|
};
|
2020-02-05 02:40:38 -05:00
|
|
|
(internal_channels, external_channels)
|
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05: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 17:10:00 -04:00
|
|
|
pub struct Worker {
|
2019-11-04 10:38:52 -05:00
|
|
|
pub name: String,
|
2020-02-03 18:08:44 -05:00
|
|
|
pub isolate: Box<deno_core::EsIsolate>,
|
2020-02-08 14:34:31 -05:00
|
|
|
pub state: State,
|
2020-02-11 04:04:59 -05:00
|
|
|
pub waker: AtomicWaker,
|
|
|
|
pub(crate) internal_channels: WorkerChannelsInternal,
|
|
|
|
external_channels: WorkerHandle,
|
2020-04-03 13:40:11 -04:00
|
|
|
inspector: Option<Box<DenoInspector>>,
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
|
2019-04-08 17:10:00 -04:00
|
|
|
impl Worker {
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
2020-02-14 19:18:36 -05:00
|
|
|
let loader = Rc::new(state.clone());
|
|
|
|
let mut isolate = deno_core::EsIsolate::new(loader, startup_data, false);
|
2019-08-07 12:55:39 -04:00
|
|
|
|
2020-03-27 16:09:51 -04:00
|
|
|
let global_state = state.borrow().global_state.clone();
|
|
|
|
|
2020-04-03 13:40:11 -04:00
|
|
|
let inspect = global_state.flags.inspect.as_ref();
|
|
|
|
let inspect_brk = global_state.flags.inspect_brk.as_ref();
|
|
|
|
let inspector = inspect
|
|
|
|
.or(inspect_brk)
|
|
|
|
.and_then(|host| match state.borrow().debug_type {
|
|
|
|
DebugType::Main if inspect_brk.is_some() => Some((host, true)),
|
|
|
|
DebugType::Main | DebugType::Dependent => Some((host, false)),
|
|
|
|
DebugType::Internal => None,
|
|
|
|
})
|
|
|
|
.map(|(host, wait_for_debugger)| {
|
|
|
|
DenoInspector::new(&mut isolate, *host, wait_for_debugger)
|
|
|
|
});
|
2020-03-27 16:09:51 -04:00
|
|
|
|
2020-03-02 17:20:16 -05:00
|
|
|
isolate.set_js_error_create_fn(move |core_js_error| {
|
2020-03-27 16:09:51 -04:00
|
|
|
JSError::create(core_js_error, &global_state.ts_compiler)
|
2020-01-21 03:49:47 -05:00
|
|
|
});
|
2019-11-09 15:07:14 -05:00
|
|
|
|
2020-02-05 02:40:38 -05:00
|
|
|
let (internal_channels, external_channels) = create_channels();
|
|
|
|
|
2019-11-04 10:38:52 -05:00
|
|
|
Self {
|
|
|
|
name,
|
2020-02-03 18:08:44 -05:00
|
|
|
isolate,
|
2019-11-04 10:38:52 -05:00
|
|
|
state,
|
2020-02-11 04:04:59 -05:00
|
|
|
waker: AtomicWaker::new(),
|
|
|
|
internal_channels,
|
2020-01-21 11:50:06 -05:00
|
|
|
external_channels,
|
2020-03-27 16:09:51 -04:00
|
|
|
inspector,
|
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> {
|
2020-02-03 18:08:44 -05:00
|
|
|
self.isolate.execute(js_filename, js_source)
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
|
|
|
|
2020-02-18 10:08:18 -05:00
|
|
|
/// Loads and instantiates specified JavaScript module.
|
|
|
|
pub async fn preload_module(
|
|
|
|
&mut self,
|
|
|
|
module_specifier: &ModuleSpecifier,
|
|
|
|
) -> Result<ModuleId, ErrBox> {
|
|
|
|
self.isolate.load_module(module_specifier, None).await
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Loads, instantiates and executes specified JavaScript module.
|
|
|
|
pub async fn execute_module(
|
2019-06-05 16:35:38 -04:00
|
|
|
&mut self,
|
2019-06-12 15:00:08 -04:00
|
|
|
module_specifier: &ModuleSpecifier,
|
2020-01-08 09:06:04 -05:00
|
|
|
) -> Result<(), ErrBox> {
|
2020-02-18 10:08:18 -05:00
|
|
|
let id = self.preload_module(module_specifier).await?;
|
|
|
|
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,
|
|
|
|
) -> Result<(), ErrBox> {
|
|
|
|
let id = self
|
|
|
|
.isolate
|
|
|
|
.load_module(module_specifier, Some(code))
|
|
|
|
.await?;
|
|
|
|
self.isolate.mod_evaluate(id)
|
2019-01-30 17:21:31 -05:00
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
/// Returns a way to communicate with the Worker from other threads.
|
2020-02-11 04:04:59 -05:00
|
|
|
pub fn thread_safe_handle(&self) -> WorkerHandle {
|
2020-02-03 18:08:44 -05:00
|
|
|
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
|
|
|
|
2020-03-27 16:09:51 -04: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.
|
|
|
|
self.inspector.take();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2020-03-27 16:09:51 -04:00
|
|
|
if let Some(deno_inspector) = inner.inspector.as_mut() {
|
|
|
|
// We always poll the inspector if it exists.
|
|
|
|
let _ = deno_inspector.poll_unpin(cx);
|
|
|
|
}
|
2020-02-11 04:04:59 -05:00
|
|
|
inner.waker.register(cx.waker());
|
2020-02-03 18:08:44 -05:00
|
|
|
inner.isolate.poll_unpin(cx)
|
2018-09-17 20:41:13 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-28 16:05:41 -04:00
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
/// This worker is created and used by Deno executable.
|
|
|
|
///
|
|
|
|
/// It provides ops available in the `Deno` namespace.
|
|
|
|
///
|
|
|
|
/// All WebWorkers created during program execution are decendants of
|
|
|
|
/// this worker.
|
|
|
|
pub struct MainWorker(Worker);
|
|
|
|
|
|
|
|
impl MainWorker {
|
2020-02-08 14:34:31 -05:00
|
|
|
pub fn new(name: String, startup_data: StartupData, state: State) -> Self {
|
2020-01-21 11:50:06 -05:00
|
|
|
let state_ = state.clone();
|
2020-02-05 02:40:38 -05:00
|
|
|
let mut worker = Worker::new(name, startup_data, state_);
|
2020-01-21 11:50:06 -05:00
|
|
|
{
|
2020-02-03 18:08:44 -05:00
|
|
|
let op_registry = worker.isolate.op_registry.clone();
|
|
|
|
let isolate = &mut worker.isolate;
|
|
|
|
ops::runtime::init(isolate, &state);
|
|
|
|
ops::runtime_compiler::init(isolate, &state);
|
|
|
|
ops::errors::init(isolate, &state);
|
|
|
|
ops::fetch::init(isolate, &state);
|
|
|
|
ops::fs::init(isolate, &state);
|
2020-02-21 13:21:51 -05:00
|
|
|
ops::fs_events::init(isolate, &state);
|
2020-02-03 18:08:44 -05:00
|
|
|
ops::io::init(isolate, &state);
|
|
|
|
ops::plugins::init(isolate, &state, op_registry);
|
|
|
|
ops::net::init(isolate, &state);
|
|
|
|
ops::tls::init(isolate, &state);
|
|
|
|
ops::os::init(isolate, &state);
|
|
|
|
ops::permissions::init(isolate, &state);
|
|
|
|
ops::process::init(isolate, &state);
|
|
|
|
ops::random::init(isolate, &state);
|
|
|
|
ops::repl::init(isolate, &state);
|
|
|
|
ops::resources::init(isolate, &state);
|
|
|
|
ops::signal::init(isolate, &state);
|
|
|
|
ops::timers::init(isolate, &state);
|
2020-02-26 01:01:24 -05:00
|
|
|
ops::tty::init(isolate, &state);
|
2020-02-03 18:08:44 -05:00
|
|
|
ops::worker_host::init(isolate, &state);
|
2020-02-11 04:04:59 -05:00
|
|
|
ops::web_worker::init(isolate, &state, &worker.internal_channels.sender);
|
2020-01-21 11:50:06 -05:00
|
|
|
}
|
|
|
|
Self(worker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Deref for MainWorker {
|
|
|
|
type Target = Worker;
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.0
|
|
|
|
}
|
2019-11-04 10:38:52 -05:00
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
impl DerefMut for MainWorker {
|
|
|
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
|
|
|
&mut self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
2020-02-06 23:05:02 -05:00
|
|
|
use crate::global_state::GlobalState;
|
2019-04-08 17:10:00 -04:00
|
|
|
use crate::startup_data;
|
2020-02-08 14:34:31 -05:00
|
|
|
use crate::state::State;
|
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,
|
|
|
|
{
|
2020-01-01 09:51:27 -05:00
|
|
|
let fut = futures::future::lazy(move |_cx| f());
|
2020-02-03 18:08:44 -05:00
|
|
|
tokio_util::run_basic(fut)
|
2019-11-22 12:46:57 -05:00
|
|
|
}
|
|
|
|
|
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()
|
2020-02-02 16:55:22 -05:00
|
|
|
.join("cli/tests/esm_imports_a.js");
|
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();
|
2020-02-26 05:52:15 -05:00
|
|
|
let global_state = GlobalState::new(flags::Flags::default()).unwrap();
|
2020-04-03 13:40:11 -04:00
|
|
|
let state = State::new(
|
|
|
|
global_state,
|
|
|
|
None,
|
|
|
|
module_specifier.clone(),
|
|
|
|
DebugType::Main,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
2020-02-03 18:08:44 -05:00
|
|
|
tokio_util::run_basic(async move {
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut worker =
|
2020-02-05 02:40:38 -05:00
|
|
|
MainWorker::new("TEST".to_string(), StartupData::None, state);
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2019-11-22 12:14:34 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Err(e) = (&mut *worker).await {
|
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2020-02-11 11:23:40 -05:00
|
|
|
let state = state_.borrow();
|
|
|
|
assert_eq!(state.metrics.resolve_count, 2);
|
2019-05-20 12:06:57 -04:00
|
|
|
// Check that we didn't start the compiler.
|
2020-02-11 11:23:40 -05:00
|
|
|
assert_eq!(state.global_state.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()
|
2019-12-23 09:59:44 -05:00
|
|
|
.join("tests/circular1.ts");
|
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();
|
2020-02-26 05:52:15 -05:00
|
|
|
let global_state = GlobalState::new(flags::Flags::default()).unwrap();
|
2020-04-03 13:40:11 -04:00
|
|
|
let state = State::new(
|
|
|
|
global_state,
|
|
|
|
None,
|
|
|
|
module_specifier.clone(),
|
|
|
|
DebugType::Main,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2019-03-14 19:17:52 -04:00
|
|
|
let state_ = state.clone();
|
2020-02-03 18:08:44 -05:00
|
|
|
tokio_util::run_basic(async move {
|
2019-06-05 16:35:38 -04:00
|
|
|
let mut worker =
|
2020-02-05 02:40:38 -05:00
|
|
|
MainWorker::new("TEST".to_string(), StartupData::None, state);
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2019-11-22 12:14:34 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Err(e) = (&mut *worker).await {
|
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2019-11-16 19:17:47 -05:00
|
|
|
});
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2020-02-11 11:23:40 -05:00
|
|
|
let state = state_.borrow();
|
|
|
|
assert_eq!(state.metrics.resolve_count, 1);
|
2019-05-20 12:06:57 -04:00
|
|
|
// Check that we didn't start the compiler.
|
2020-02-11 11:23:40 -05:00
|
|
|
assert_eq!(state.global_state.compiler_starts.load(Ordering::SeqCst), 0);
|
2019-01-01 06:24:05 -05:00
|
|
|
}
|
2019-04-08 17:10:00 -04:00
|
|
|
|
2020-02-03 18:08:44 -05:00
|
|
|
#[tokio::test]
|
|
|
|
async fn execute_006_url_imports() {
|
2019-09-19 14:48:05 -04:00
|
|
|
let http_server_guard = crate::test_util::http_server();
|
2019-09-04 17:16:46 -04:00
|
|
|
let p = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
|
|
.parent()
|
|
|
|
.unwrap()
|
2019-12-23 09:59:44 -05:00
|
|
|
.join("cli/tests/006_url_imports.ts");
|
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();
|
2020-02-26 05:52:15 -05:00
|
|
|
let flags = flags::Flags {
|
2020-02-04 14:24:33 -05:00
|
|
|
subcommand: flags::DenoSubcommand::Run {
|
|
|
|
script: module_specifier.to_string(),
|
|
|
|
},
|
|
|
|
reload: true,
|
2020-02-26 05:52:15 -05:00
|
|
|
..flags::Flags::default()
|
2020-02-04 14:24:33 -05:00
|
|
|
};
|
2020-02-06 23:05:02 -05:00
|
|
|
let global_state = GlobalState::new(flags).unwrap();
|
2020-04-03 13:40:11 -04:00
|
|
|
let state = State::new(
|
|
|
|
global_state.clone(),
|
|
|
|
None,
|
|
|
|
module_specifier.clone(),
|
|
|
|
DebugType::Main,
|
|
|
|
)
|
|
|
|
.unwrap();
|
2020-02-03 18:08:44 -05:00
|
|
|
let mut worker = MainWorker::new(
|
|
|
|
"TEST".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state.clone(),
|
|
|
|
);
|
|
|
|
worker.execute("bootstrapMainRuntime()").unwrap();
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = worker.execute_module(&module_specifier).await;
|
2020-02-03 18:08:44 -05:00
|
|
|
if let Err(err) = result {
|
|
|
|
eprintln!("execute_mod err {:?}", err);
|
|
|
|
}
|
|
|
|
if let Err(e) = (&mut *worker).await {
|
|
|
|
panic!("Future got unexpected error: {:?}", e);
|
|
|
|
}
|
2020-02-11 11:23:40 -05:00
|
|
|
let state = state.borrow();
|
|
|
|
assert_eq!(state.metrics.resolve_count, 3);
|
2019-05-20 12:06:57 -04:00
|
|
|
// Check that we've only invoked the compiler once.
|
2020-02-11 11:23:40 -05:00
|
|
|
assert_eq!(state.global_state.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
|
|
|
}
|
|
|
|
|
2020-01-21 11:50:06 -05:00
|
|
|
fn create_test_worker() -> MainWorker {
|
2020-02-08 14:34:31 -05:00
|
|
|
let state = State::mock("./hello.js");
|
2020-01-21 11:50:06 -05:00
|
|
|
let mut worker = MainWorker::new(
|
2019-11-09 15:07:14 -05:00
|
|
|
"TEST".to_string(),
|
|
|
|
startup_data::deno_isolate_init(),
|
|
|
|
state,
|
|
|
|
);
|
2020-01-21 09:53:29 -05:00
|
|
|
worker.execute("bootstrapMainRuntime()").unwrap();
|
2019-04-08 17:10:00 -04:00
|
|
|
worker
|
|
|
|
}
|
|
|
|
|
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();
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = block_on(worker.execute_module(&module_specifier));
|
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()
|
2020-02-02 16:55:22 -05:00
|
|
|
.join("cli/tests/002_hello.ts");
|
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();
|
2020-02-18 10:08:18 -05:00
|
|
|
let result = block_on(worker.execute_module(&module_specifier));
|
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
|
|
|
}
|