From f9f10229a41d000ae9e96f0907ca321f9ffdeea7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 8 May 2020 16:18:00 +0200 Subject: [PATCH] refactor: Remove cli::compilers module (#5138) This PR removes "cli/compilers/" directory. "cli/compilers/ts.rs" has been renamed to "cli/tsc.rs" --- cli/compilers/compiler_worker.rs | 66 -------------------------------- cli/compilers/js.rs | 21 ---------- cli/compilers/mod.rs | 25 ------------ cli/global_state.rs | 19 +++++---- cli/lib.rs | 4 +- cli/lockfile.rs | 2 +- cli/ops/runtime_compiler.rs | 4 +- cli/state.rs | 2 +- cli/{compilers/ts.rs => tsc.rs} | 56 +++++++++++++++++++++++++-- 9 files changed, 70 insertions(+), 129 deletions(-) delete mode 100644 cli/compilers/compiler_worker.rs delete mode 100644 cli/compilers/js.rs delete mode 100644 cli/compilers/mod.rs rename cli/{compilers/ts.rs => tsc.rs} (95%) diff --git a/cli/compilers/compiler_worker.rs b/cli/compilers/compiler_worker.rs deleted file mode 100644 index aa84c8695e..0000000000 --- a/cli/compilers/compiler_worker.rs +++ /dev/null @@ -1,66 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::ops; -use crate::state::State; -use crate::web_worker::WebWorker; -use core::task::Context; -use deno_core::ErrBox; -use deno_core::StartupData; -use futures::future::Future; -use futures::future::FutureExt; -use std::ops::Deref; -use std::ops::DerefMut; -use std::pin::Pin; -use std::task::Poll; - -/// This worker is used to host TypeScript and WASM compilers. -/// -/// It provides minimal set of ops that are necessary to facilitate -/// compilation. -/// -/// NOTE: This worker is considered priveleged, because it may -/// access file system without permission check. -/// -/// At the moment this worker is meant to be single-use - after -/// performing single compilation/bundling it should be destroyed. -/// -/// TODO(bartlomieju): add support to reuse the worker - or in other -/// words support stateful TS compiler -pub struct CompilerWorker(WebWorker); - -impl CompilerWorker { - pub fn new(name: String, startup_data: StartupData, state: State) -> Self { - let state_ = state.clone(); - let mut worker = WebWorker::new(name, startup_data, state_, false); - { - let isolate = &mut worker.isolate; - ops::compiler::init(isolate, &state); - // TODO(bartlomieju): CompilerWorker should not - // depend on those ops - ops::os::init(isolate, &state); - ops::fs::init(isolate, &state); - } - Self(worker) - } -} - -impl Deref for CompilerWorker { - type Target = WebWorker; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl DerefMut for CompilerWorker { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl Future for CompilerWorker { - type Output = Result<(), ErrBox>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { - let inner = self.get_mut(); - inner.0.poll_unpin(cx) - } -} diff --git a/cli/compilers/js.rs b/cli/compilers/js.rs deleted file mode 100644 index d90960bfc3..0000000000 --- a/cli/compilers/js.rs +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::compilers::CompiledModule; -use crate::file_fetcher::SourceFile; -use deno_core::ErrBox; -use std::str; - -pub struct JsCompiler {} - -impl JsCompiler { - pub async fn compile( - &self, - source_file: SourceFile, - ) -> Result { - Ok(CompiledModule { - code: str::from_utf8(&source_file.source_code) - .unwrap() - .to_string(), - name: source_file.url.to_string(), - }) - } -} diff --git a/cli/compilers/mod.rs b/cli/compilers/mod.rs deleted file mode 100644 index bc3bfade2b..0000000000 --- a/cli/compilers/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::ops::JsonResult; -use deno_core::ErrBox; -use futures::Future; - -mod compiler_worker; -mod js; -mod ts; - -pub use js::JsCompiler; -pub use ts::runtime_compile; -pub use ts::runtime_transpile; -pub use ts::TargetLib; -pub use ts::TsCompiler; - -pub type CompilationResultFuture = dyn Future; - -#[derive(Debug, Clone)] -pub struct CompiledModule { - pub code: String, - pub name: String, -} - -pub type CompiledModuleFuture = - dyn Future>; diff --git a/cli/global_state.rs b/cli/global_state.rs index 4602033649..b91ba5b108 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -1,8 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::compilers::CompiledModule; -use crate::compilers::JsCompiler; -use crate::compilers::TargetLib; -use crate::compilers::TsCompiler; use crate::deno_dir; use crate::file_fetcher::SourceFileFetcher; use crate::flags; @@ -10,6 +6,9 @@ use crate::http_cache; use crate::lockfile::Lockfile; use crate::msg; use crate::permissions::Permissions; +use crate::tsc::CompiledModule; +use crate::tsc::TargetLib; +use crate::tsc::TsCompiler; use deno_core::ErrBox; use deno_core::ModuleSpecifier; use std::env; @@ -33,7 +32,6 @@ pub struct GlobalStateInner { pub permissions: Permissions, pub dir: deno_dir::DenoDir, pub file_fetcher: SourceFileFetcher, - pub js_compiler: JsCompiler, pub ts_compiler: TsCompiler, pub lockfile: Option>, pub compiler_starts: AtomicUsize, @@ -84,7 +82,6 @@ impl GlobalState { flags, file_fetcher, ts_compiler, - js_compiler: JsCompiler {}, lockfile, compiler_starts: AtomicUsize::new(0), compile_lock: AsyncMutex::new(()), @@ -140,10 +137,16 @@ impl GlobalState { .ok(); }; - state1.js_compiler.compile(out).await + Ok(CompiledModule { + code: String::from_utf8(out.source_code)?, + name: out.url.to_string(), + }) } } - _ => state1.js_compiler.compile(out).await, + _ => Ok(CompiledModule { + code: String::from_utf8(out.source_code)?, + name: out.url.to_string(), + }), }?; drop(compile_lock); diff --git a/cli/lib.rs b/cli/lib.rs index 673340cb9c..060c86b55c 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -23,7 +23,6 @@ extern crate url; mod checksum; pub mod colors; -pub mod compilers; pub mod deno_dir; pub mod diagnostics; mod disk_cache; @@ -57,6 +56,7 @@ mod swc_util; mod test_runner; pub mod test_util; mod tokio_util; +mod tsc; mod upgrade; pub mod version; mod web_worker; @@ -66,7 +66,6 @@ pub use dprint_plugin_typescript::swc_common; pub use dprint_plugin_typescript::swc_ecma_ast; pub use dprint_plugin_typescript::swc_ecma_parser; -use crate::compilers::TargetLib; use crate::doc::parser::DocFileLoader; use crate::file_fetcher::SourceFile; use crate::file_fetcher::SourceFileFetcher; @@ -76,6 +75,7 @@ use crate::op_error::OpError; use crate::ops::io::get_stdio; use crate::state::DebugType; use crate::state::State; +use crate::tsc::TargetLib; use crate::worker::MainWorker; use deno_core::v8_set_flags; use deno_core::ErrBox; diff --git a/cli/lockfile.rs b/cli/lockfile.rs index f8700dac14..5e43e3420f 100644 --- a/cli/lockfile.rs +++ b/cli/lockfile.rs @@ -1,4 +1,4 @@ -use crate::compilers::CompiledModule; +use crate::tsc::CompiledModule; use serde_json::json; pub use serde_json::Value; use std::collections::HashMap; diff --git a/cli/ops/runtime_compiler.rs b/cli/ops/runtime_compiler.rs index c7225b9441..e44d6fa8b2 100644 --- a/cli/ops/runtime_compiler.rs +++ b/cli/ops/runtime_compiler.rs @@ -1,10 +1,10 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use super::dispatch_json::{Deserialize, JsonOp, Value}; -use crate::compilers::runtime_compile; -use crate::compilers::runtime_transpile; use crate::futures::FutureExt; use crate::op_error::OpError; use crate::state::State; +use crate::tsc::runtime_compile; +use crate::tsc::runtime_transpile; use deno_core::CoreIsolate; use deno_core::ZeroCopyBuf; use std::collections::HashMap; diff --git a/cli/state.rs b/cli/state.rs index 9fd719646c..8c425d7005 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -1,5 +1,4 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use crate::compilers::TargetLib; use crate::file_fetcher::SourceFileFetcher; use crate::global_state::GlobalState; use crate::global_timer::GlobalTimer; @@ -9,6 +8,7 @@ use crate::op_error::OpError; use crate::ops::JsonOp; use crate::ops::MinimalOp; use crate::permissions::Permissions; +use crate::tsc::TargetLib; use crate::web_worker::WebWorkerHandle; use deno_core::Buf; use deno_core::ErrBox; diff --git a/cli/compilers/ts.rs b/cli/tsc.rs similarity index 95% rename from cli/compilers/ts.rs rename to cli/tsc.rs index 832d5e6df6..f0b6172465 100644 --- a/cli/compilers/ts.rs +++ b/cli/tsc.rs @@ -1,7 +1,5 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. -use super::compiler_worker::CompilerWorker; use crate::colors; -use crate::compilers::CompiledModule; use crate::diagnostics::Diagnostic; use crate::diagnostics::DiagnosticItem; use crate::disk_cache::DiskCache; @@ -12,16 +10,23 @@ use crate::fs as deno_fs; use crate::global_state::GlobalState; use crate::msg; use crate::op_error::OpError; +use crate::ops; use crate::source_maps::SourceMapGetter; use crate::startup_data; +use crate::state::State; use crate::state::*; use crate::tokio_util; use crate::version; +use crate::web_worker::WebWorker; use crate::web_worker::WebWorkerHandle; use crate::worker::WorkerEvent; +use core::task::Context; use deno_core::Buf; use deno_core::ErrBox; use deno_core::ModuleSpecifier; +use deno_core::StartupData; +use futures::future::Future; +use futures::future::FutureExt; use log::info; use regex::Regex; use serde::Deserialize; @@ -33,13 +38,58 @@ use std::fs; use std::hash::BuildHasher; use std::io; use std::ops::Deref; +use std::ops::DerefMut; use std::path::PathBuf; +use std::pin::Pin; use std::str; use std::sync::atomic::Ordering; use std::sync::Arc; use std::sync::Mutex; +use std::task::Poll; use url::Url; +#[derive(Debug, Clone)] +pub struct CompiledModule { + pub code: String, + pub name: String, +} + +pub struct CompilerWorker(WebWorker); + +impl CompilerWorker { + pub fn new(name: String, startup_data: StartupData, state: State) -> Self { + let state_ = state.clone(); + let mut worker = WebWorker::new(name, startup_data, state_, false); + { + let isolate = &mut worker.isolate; + ops::compiler::init(isolate, &state); + } + Self(worker) + } +} + +impl Deref for CompilerWorker { + type Target = WebWorker; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for CompilerWorker { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} + +impl Future for CompilerWorker { + type Output = Result<(), ErrBox>; + + fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll { + let inner = self.get_mut(); + inner.0.poll_unpin(cx) + } +} + lazy_static! { static ref CHECK_JS_RE: Regex = Regex::new(r#""checkJs"\s*?:\s*?true"#).unwrap(); @@ -805,7 +855,7 @@ mod tests { url: specifier.as_url().clone(), filename: PathBuf::from(p.to_str().unwrap().to_string()), media_type: msg::MediaType::TypeScript, - source_code: include_bytes!("../tests/002_hello.ts").to_vec(), + source_code: include_bytes!("./tests/002_hello.ts").to_vec(), types_url: None, }; let mock_state =