diff options
Diffstat (limited to 'cli/global_state.rs')
-rw-r--r-- | cli/global_state.rs | 57 |
1 files changed, 15 insertions, 42 deletions
diff --git a/cli/global_state.rs b/cli/global_state.rs index a723bdd2f..a1d4af86c 100644 --- a/cli/global_state.rs +++ b/cli/global_state.rs @@ -17,20 +17,15 @@ use crate::tsc::TsCompiler; use deno_core::ErrBox; use deno_core::ModuleSpecifier; use std::env; -use std::ops::Deref; use std::sync::atomic::AtomicUsize; use std::sync::Arc; use std::sync::Mutex; use tokio::sync::Mutex as AsyncMutex; -/// Holds state of the program and can be accessed by V8 isolate. -#[derive(Clone)] -pub struct GlobalState(Arc<GlobalStateInner>); - /// This structure represents state of single "deno" program. /// /// It is shared by all created workers (thus V8 isolates). -pub struct GlobalStateInner { +pub struct GlobalState { /// Flags parsed from `argv` contents. pub flags: flags::Flags, /// Permissions parsed from `flags`. @@ -44,23 +39,13 @@ pub struct GlobalStateInner { compile_lock: AsyncMutex<()>, } -impl Deref for GlobalState { - type Target = GlobalStateInner; - fn deref(&self) -> &Self::Target { - &self.0 - } -} - impl GlobalState { - pub fn new(flags: flags::Flags) -> Result<Self, ErrBox> { + pub fn new(flags: flags::Flags) -> Result<Arc<Self>, ErrBox> { let custom_root = env::var("DENO_DIR").map(String::into).ok(); let dir = deno_dir::DenoDir::new(custom_root)?; let deps_cache_location = dir.root.join("deps"); let http_cache = http_cache::HttpCache::new(&deps_cache_location); - let ca_file = flags - .ca_file - .clone() - .or_else(|| env::var("DENO_CERT").map(String::into).ok()); + let ca_file = flags.ca_file.clone().or_else(|| env::var("DENO_CERT").ok()); let file_fetcher = SourceFileFetcher::new( http_cache, @@ -68,7 +53,7 @@ impl GlobalState { flags.cache_blocklist.clone(), flags.no_remote, flags.cached_only, - ca_file, + ca_file.as_deref(), )?; let ts_compiler = TsCompiler::new( @@ -95,7 +80,7 @@ impl GlobalState { } }; - let inner = GlobalStateInner { + let global_state = GlobalState { dir, permissions: Permissions::from_flags(&flags), flags, @@ -106,7 +91,7 @@ impl GlobalState { compiler_starts: AtomicUsize::new(0), compile_lock: AsyncMutex::new(()), }; - Ok(GlobalState(Arc::new(inner))) + Ok(Arc::new(global_state)) } /// This function is called when new module load is @@ -114,7 +99,7 @@ impl GlobalState { /// all dependencies and if it is required then also perform TS typecheck /// and traspilation. pub async fn prepare_module_load( - &self, + self: &Arc<Self>, module_specifier: ModuleSpecifier, maybe_referrer: Option<ModuleSpecifier>, target_lib: TargetLib, @@ -178,14 +163,7 @@ impl GlobalState { } else { self .ts_compiler - .compile( - self.clone(), - &out, - target_lib, - permissions, - module_graph, - allow_js, - ) + .compile(self, &out, target_lib, permissions, module_graph, allow_js) .await?; } } @@ -210,7 +188,6 @@ impl GlobalState { module_specifier: ModuleSpecifier, _maybe_referrer: Option<ModuleSpecifier>, ) -> Result<CompiledModule, ErrBox> { - let state1 = self.clone(); let module_specifier = module_specifier.clone(); let out = self @@ -232,7 +209,7 @@ impl GlobalState { }; let compiled_module = if was_compiled { - match state1.ts_compiler.get_compiled_module(&out.url) { + match self.ts_compiler.get_compiled_module(&out.url) { Ok(module) => module, Err(e) => { let msg = format!( @@ -264,16 +241,12 @@ impl GlobalState { pub fn mock( argv: Vec<String>, maybe_flags: Option<flags::Flags>, - ) -> GlobalState { - if let Some(in_flags) = maybe_flags { - GlobalState::new(flags::Flags { argv, ..in_flags }).unwrap() - } else { - GlobalState::new(flags::Flags { - argv, - ..flags::Flags::default() - }) - .unwrap() - } + ) -> Arc<GlobalState> { + GlobalState::new(flags::Flags { + argv, + ..maybe_flags.unwrap_or_default() + }) + .unwrap() } } |