From d9c84eb91eecdf1225091e4d2bea8e4c6a076504 Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 6 Feb 2020 23:05:02 -0500 Subject: Rename ThreadSafeGlobalState to GlobalState (#3907) simplify --- cli/global_state.rs | 59 ++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) (limited to 'cli/global_state.rs') 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); +pub struct GlobalState(Arc); /// 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; +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 { +impl GlobalState { + pub fn new(flags: flags::DenoFlags) -> Result { 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) -> ThreadSafeGlobalState { - ThreadSafeGlobalState::new( - flags::DenoFlags { - argv, - ..flags::DenoFlags::default() - }, - Progress::new(), - ) + pub fn mock(argv: Vec) -> GlobalState { + GlobalState::new(flags::DenoFlags { + argv, + ..flags::DenoFlags::default() + }) .unwrap() } } @@ -246,7 +252,7 @@ impl ThreadSafeGlobalState { #[test] fn thread_safe() { fn f(_: 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() + }); } -- cgit v1.2.3