diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2020-02-06 23:05:02 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-06 23:05:02 -0500 |
commit | d9c84eb91eecdf1225091e4d2bea8e4c6a076504 (patch) | |
tree | f9d65e93a5ae4ce2cfd4ebf0c6b7fe7fe1da822f | |
parent | 522e856347b27b939dac92eb592ce64d86a96c03 (diff) |
Rename ThreadSafeGlobalState to GlobalState (#3907)
simplify
-rw-r--r-- | cli/compilers/ts.rs | 24 | ||||
-rw-r--r-- | cli/compilers/wasm.rs | 6 | ||||
-rw-r--r-- | cli/global_state.rs | 59 | ||||
-rw-r--r-- | cli/lib.rs | 25 | ||||
-rw-r--r-- | cli/state.rs | 10 | ||||
-rw-r--r-- | cli/worker.rs | 14 |
6 files changed, 59 insertions, 79 deletions
diff --git a/cli/compilers/ts.rs b/cli/compilers/ts.rs index c882f7d66..303c24b6a 100644 --- a/cli/compilers/ts.rs +++ b/cli/compilers/ts.rs @@ -6,7 +6,7 @@ use crate::diagnostics::Diagnostic; use crate::disk_cache::DiskCache; use crate::file_fetcher::SourceFile; use crate::file_fetcher::SourceFileFetcher; -use crate::global_state::ThreadSafeGlobalState; +use crate::global_state::GlobalState; use crate::msg; use crate::ops::JsonResult; use crate::source_maps::SourceMapGetter; @@ -246,7 +246,7 @@ impl TsCompiler { /// Create a new V8 worker with snapshot of TS compiler and setup compiler's /// runtime. - fn setup_worker(global_state: ThreadSafeGlobalState) -> CompilerWorker { + fn setup_worker(global_state: GlobalState) -> CompilerWorker { let entry_point = ModuleSpecifier::resolve_url_or_path("./__$deno$ts_compiler.ts").unwrap(); let worker_state = @@ -270,7 +270,7 @@ impl TsCompiler { pub async fn bundle_async( &self, - global_state: ThreadSafeGlobalState, + global_state: GlobalState, module_name: String, out_file: Option<String>, ) -> Result<(), ErrBox> { @@ -325,7 +325,7 @@ impl TsCompiler { /// compiler. pub async fn compile_async( &self, - global_state: ThreadSafeGlobalState, + global_state: GlobalState, source_file: &SourceFile, target: TargetLib, ) -> Result<CompiledModule, ErrBox> { @@ -604,7 +604,7 @@ impl TsCompiler { } async fn execute_in_thread( - global_state: ThreadSafeGlobalState, + global_state: GlobalState, req: Buf, ) -> Result<Option<Buf>, ErrBox> { let (load_sender, load_receiver) = @@ -638,7 +638,7 @@ async fn execute_in_thread( async fn execute_in_thread_json( req_msg: Buf, - global_state: ThreadSafeGlobalState, + global_state: GlobalState, ) -> JsonResult { let maybe_msg = execute_in_thread(global_state, req_msg).await?; let msg = maybe_msg.unwrap(); @@ -647,7 +647,7 @@ async fn execute_in_thread_json( } pub fn runtime_compile_async<S: BuildHasher>( - global_state: ThreadSafeGlobalState, + global_state: GlobalState, root_name: &str, sources: &Option<HashMap<String, String, S>>, bundle: bool, @@ -669,7 +669,7 @@ pub fn runtime_compile_async<S: BuildHasher>( } pub fn runtime_transpile_async<S: BuildHasher>( - global_state: ThreadSafeGlobalState, + global_state: GlobalState, sources: &HashMap<String, String, S>, options: &Option<String>, ) -> Pin<Box<CompilationResultFuture>> { @@ -708,10 +708,8 @@ mod tests { source_code: include_bytes!("../tests/002_hello.ts").to_vec(), types_url: None, }; - let mock_state = ThreadSafeGlobalState::mock(vec![ - String::from("deno"), - String::from("hello.js"), - ]); + let mock_state = + GlobalState::mock(vec![String::from("deno"), String::from("hello.js")]); let result = mock_state .ts_compiler .compile_async(mock_state.clone(), &out, TargetLib::Main) @@ -735,7 +733,7 @@ mod tests { .unwrap() .to_string(); - let state = ThreadSafeGlobalState::mock(vec![ + let state = GlobalState::mock(vec![ String::from("deno"), p.to_string_lossy().into(), String::from("$deno$/bundle.js"), diff --git a/cli/compilers/wasm.rs b/cli/compilers/wasm.rs index 63eebadb3..6b8da100a 100644 --- a/cli/compilers/wasm.rs +++ b/cli/compilers/wasm.rs @@ -2,7 +2,7 @@ use super::compiler_worker::CompilerWorker; use crate::compilers::CompiledModule; use crate::file_fetcher::SourceFile; -use crate::global_state::ThreadSafeGlobalState; +use crate::global_state::GlobalState; use crate::startup_data; use crate::state::*; use deno_core::ErrBox; @@ -48,7 +48,7 @@ pub struct WasmCompiler { impl WasmCompiler { /// Create a new V8 worker with snapshot of WASM compiler and setup compiler's runtime. - fn setup_worker(global_state: ThreadSafeGlobalState) -> CompilerWorker { + fn setup_worker(global_state: GlobalState) -> CompilerWorker { let entry_point = ModuleSpecifier::resolve_url_or_path("./__$deno$wasm_compiler.ts") .unwrap(); @@ -73,7 +73,7 @@ impl WasmCompiler { pub async fn compile_async( &self, - global_state: ThreadSafeGlobalState, + global_state: GlobalState, source_file: &SourceFile, ) -> Result<CompiledModule, ErrBox> { let cache = self.cache.clone(); diff --git a/cli/global_state.rs b/cli/global_state.rs index ca040c40c..202bf8c75 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -14,6 +14,7 @@ use crate::metrics::Metrics; use crate::msg; use crate::permissions::DenoPermissions; use crate::progress::Progress; +use crate::shell::Shell; use deno_core::ErrBox; use deno_core::ModuleSpecifier; use std; @@ -27,13 +28,12 @@ use tokio::sync::Mutex as AsyncMutex; /// Holds state of the program and can be accessed by V8 isolate. #[derive(Clone)] -pub struct ThreadSafeGlobalState(Arc<GlobalState>); +pub struct GlobalState(Arc<GlobalStateInner>); /// This structure represents state of single "deno" program. /// /// It is shared by all created workers (thus V8 isolates). -#[cfg_attr(feature = "cargo-clippy", allow(stutter))] -pub struct GlobalState { +pub struct GlobalStateInner { /// Flags parsed from `argv` contents. pub flags: flags::DenoFlags, /// Permissions parsed from `flags`. @@ -50,21 +50,30 @@ pub struct GlobalState { compile_lock: AsyncMutex<()>, } -impl Deref for ThreadSafeGlobalState { - type Target = Arc<GlobalState>; +impl Deref for GlobalState { + type Target = GlobalStateInner; fn deref(&self) -> &Self::Target { &self.0 } } -impl ThreadSafeGlobalState { - pub fn new( - flags: flags::DenoFlags, - progress: Progress, - ) -> Result<Self, ErrBox> { +impl GlobalState { + pub fn new(flags: flags::DenoFlags) -> Result<Self, ErrBox> { let custom_root = env::var("DENO_DIR").map(String::into).ok(); let dir = deno_dir::DenoDir::new(custom_root)?; + // TODO(ry) Shell is a useless abstraction and should be removed at + // some point. + let shell = Arc::new(Mutex::new(Shell::new())); + + let progress = Progress::new(); + progress.set_callback(move |_done, _completed, _total, status, msg| { + if !status.is_empty() { + let mut s = shell.lock().unwrap(); + s.status(status, msg).expect("shell problem"); + } + }); + let file_fetcher = SourceFileFetcher::new( dir.deps_cache.clone(), progress.clone(), @@ -88,7 +97,7 @@ impl ThreadSafeGlobalState { None }; - let state = GlobalState { + let inner = GlobalStateInner { dir, permissions: DenoPermissions::from_flags(&flags), flags, @@ -103,7 +112,7 @@ impl ThreadSafeGlobalState { compile_lock: AsyncMutex::new(()), }; - Ok(ThreadSafeGlobalState(Arc::new(state))) + Ok(GlobalState(Arc::new(inner))) } pub async fn fetch_compiled_module( @@ -231,14 +240,11 @@ impl ThreadSafeGlobalState { } #[cfg(test)] - pub fn mock(argv: Vec<String>) -> ThreadSafeGlobalState { - ThreadSafeGlobalState::new( - flags::DenoFlags { - argv, - ..flags::DenoFlags::default() - }, - Progress::new(), - ) + pub fn mock(argv: Vec<String>) -> GlobalState { + GlobalState::new(flags::DenoFlags { + argv, + ..flags::DenoFlags::default() + }) .unwrap() } } @@ -246,7 +252,7 @@ impl ThreadSafeGlobalState { #[test] fn thread_safe() { fn f<S: Send + Sync>(_: S) {} - f(ThreadSafeGlobalState::mock(vec![ + f(GlobalState::mock(vec![ String::from("./deno"), String::from("hello.js"), ])); @@ -254,11 +260,8 @@ fn thread_safe() { #[test] fn import_map_given_for_repl() { - let _result = ThreadSafeGlobalState::new( - flags::DenoFlags { - import_map_path: Some("import_map.json".to_string()), - ..flags::DenoFlags::default() - }, - Progress::new(), - ); + let _result = GlobalState::new(flags::DenoFlags { + import_map_path: Some("import_map.json".to_string()), + ..flags::DenoFlags::default() + }); } diff --git a/cli/lib.rs b/cli/lib.rs index fee9e079f..5f98f6f67 100644 --- a/cli/lib.rs +++ b/cli/lib.rs @@ -59,9 +59,8 @@ pub mod worker; use crate::compilers::TargetLib; use crate::deno_error::js_check; use crate::deno_error::{print_err_and_exit, print_msg_and_exit}; -use crate::global_state::ThreadSafeGlobalState; +use crate::global_state::GlobalState; use crate::ops::io::get_stdio; -use crate::progress::Progress; use crate::state::ThreadSafeState; use crate::worker::MainWorker; use deno_core::v8_set_flags; @@ -98,28 +97,14 @@ impl log::Log for Logger { fn flush(&self) {} } -fn create_global_state(flags: DenoFlags) -> ThreadSafeGlobalState { - use crate::shell::Shell; - use std::sync::Arc; - use std::sync::Mutex; - - let shell = Arc::new(Mutex::new(Shell::new())); - - let progress = Progress::new(); - progress.set_callback(move |_done, _completed, _total, status, msg| { - if !status.is_empty() { - let mut s = shell.lock().unwrap(); - s.status(status, msg).expect("shell problem"); - } - }); - - ThreadSafeGlobalState::new(flags, progress) +fn create_global_state(flags: DenoFlags) -> GlobalState { + GlobalState::new(flags) .map_err(deno_error::print_err_and_exit) .unwrap() } fn create_main_worker( - global_state: ThreadSafeGlobalState, + global_state: GlobalState, main_module: ModuleSpecifier, ) -> MainWorker { let state = ThreadSafeState::new(global_state, None, main_module) @@ -147,7 +132,7 @@ fn types_command() { ); } -fn print_cache_info(state: &ThreadSafeGlobalState) { +fn print_cache_info(state: &GlobalState) { println!( "{} {:?}", colors::bold("DENO_DIR location:".to_string()), diff --git a/cli/state.rs b/cli/state.rs index f09191f11..86479d707 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -1,7 +1,7 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use crate::compilers::TargetLib; use crate::deno_error::permission_denied; -use crate::global_state::ThreadSafeGlobalState; +use crate::global_state::GlobalState; use crate::global_timer::GlobalTimer; use crate::import_map::ImportMap; use crate::metrics::Metrics; @@ -45,7 +45,7 @@ pub struct ThreadSafeState(Arc<State>); #[cfg_attr(feature = "cargo-clippy", allow(stutter))] pub struct State { - pub global_state: ThreadSafeGlobalState, + pub global_state: GlobalState, pub permissions: Arc<Mutex<DenoPermissions>>, pub main_module: ModuleSpecifier, /// When flags contains a `.import_map_path` option, the content of the @@ -223,7 +223,7 @@ impl Loader for ThreadSafeState { impl ThreadSafeState { /// If `shared_permission` is None then permissions from globa state are used. pub fn new( - global_state: ThreadSafeGlobalState, + global_state: GlobalState, shared_permissions: Option<Arc<Mutex<DenoPermissions>>>, main_module: ModuleSpecifier, ) -> Result<Self, ErrBox> { @@ -267,7 +267,7 @@ impl ThreadSafeState { /// If `shared_permission` is None then permissions from globa state are used. pub fn new_for_worker( - global_state: ThreadSafeGlobalState, + global_state: GlobalState, shared_permissions: Option<Arc<Mutex<DenoPermissions>>>, main_module: ModuleSpecifier, ) -> Result<Self, ErrBox> { @@ -375,7 +375,7 @@ impl ThreadSafeState { let module_specifier = ModuleSpecifier::resolve_url_or_path(main_module) .expect("Invalid entry module"); ThreadSafeState::new( - ThreadSafeGlobalState::mock(vec!["deno".to_string()]), + GlobalState::mock(vec!["deno".to_string()]), None, module_specifier, ) diff --git a/cli/worker.rs b/cli/worker.rs index 1725dbe14..c7efa95fa 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -255,8 +255,7 @@ impl DerefMut for MainWorker { mod tests { use super::*; use crate::flags; - use crate::global_state::ThreadSafeGlobalState; - use crate::progress::Progress; + use crate::global_state::GlobalState; use crate::startup_data; use crate::state::ThreadSafeState; use crate::tokio_util; @@ -279,9 +278,7 @@ mod tests { .join("cli/tests/esm_imports_a.js"); let module_specifier = ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap(); - let global_state = - ThreadSafeGlobalState::new(flags::DenoFlags::default(), Progress::new()) - .unwrap(); + let global_state = GlobalState::new(flags::DenoFlags::default()).unwrap(); let state = ThreadSafeState::new(global_state, None, module_specifier.clone()) .unwrap(); @@ -314,9 +311,7 @@ mod tests { .join("tests/circular1.ts"); let module_specifier = ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap(); - let global_state = - ThreadSafeGlobalState::new(flags::DenoFlags::default(), Progress::new()) - .unwrap(); + let global_state = GlobalState::new(flags::DenoFlags::default()).unwrap(); let state = ThreadSafeState::new(global_state, None, module_specifier.clone()) .unwrap(); @@ -357,8 +352,7 @@ mod tests { reload: true, ..flags::DenoFlags::default() }; - let global_state = - ThreadSafeGlobalState::new(flags, Progress::new()).unwrap(); + let global_state = GlobalState::new(flags).unwrap(); let state = ThreadSafeState::new( global_state.clone(), None, |