From 70f070706d4de048e1430eebcc0d8de3b469a5bc Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Mon, 14 Sep 2020 21:22:32 -0400 Subject: [PATCH] Rename deno::state::State to deno::state::CliState (#7480) --- cli/ops/mod.rs | 8 ++++---- cli/ops/worker_host.rs | 2 +- cli/state.rs | 44 +++++++++++++++++++++--------------------- cli/tsc.rs | 6 +++--- cli/web_worker.rs | 8 ++++---- cli/worker.rs | 18 ++++++++--------- 6 files changed, 43 insertions(+), 43 deletions(-) diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs index 673f007487..1e622463e3 100644 --- a/cli/ops/mod.rs +++ b/cli/ops/mod.rs @@ -60,12 +60,12 @@ where } /// Helper for extracting the commonly used state. Used for sync ops. -pub fn cli_state(state: &OpState) -> Rc { - state.borrow::>().clone() +pub fn cli_state(state: &OpState) -> Rc { + state.borrow::>().clone() } /// Helper for extracting the commonly used state. Used for async ops. -pub fn cli_state2(state: &Rc>) -> Rc { +pub fn cli_state2(state: &Rc>) -> Rc { let state = state.borrow(); - state.borrow::>().clone() + state.borrow::>().clone() } diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs index 392e43dc69..e9e4ac212f 100644 --- a/cli/ops/worker_host.rs +++ b/cli/ops/worker_host.rs @@ -41,7 +41,7 @@ fn create_web_worker( specifier: ModuleSpecifier, has_deno_namespace: bool, ) -> Result { - let cli_state = crate::state::State::new_for_worker( + let cli_state = crate::state::CliState::new_for_worker( global_state, Some(permissions), specifier, diff --git a/cli/state.rs b/cli/state.rs index 2d0a552149..33b560d8ba 100644 --- a/cli/state.rs +++ b/cli/state.rs @@ -28,9 +28,11 @@ use std::sync::Arc; use std::thread::JoinHandle; use std::time::Instant; -// TODO(ry) Rename to CliState to avoid confusion with other states. -#[cfg_attr(feature = "cargo-clippy", allow(stutter))] -pub struct State { +// This is named "CliState" instead of just "State" to avoid confusion with all +// other state structs (GlobalState, OpState, GothamState). +// TODO(ry) Many of the items in this struct should be moved out and into +// OpState, removing redundant RefCell wrappers if possible. +pub struct CliState { pub global_state: Arc, pub permissions: RefCell, pub main_module: ModuleSpecifier, @@ -49,20 +51,6 @@ pub struct State { pub http_client: RefCell, } -impl State { - /// Quits the process if the --unstable flag was not provided. - /// - /// This is intentionally a non-recoverable check so that people cannot probe - /// for unstable APIs from stable programs. - pub fn check_unstable(&self, api_name: &str) { - // TODO(ry) Maybe use IsolateHandle::terminate_execution here to provide a - // stack trace in JS. - if !self.global_state.flags.unstable { - exit_unstable(api_name); - } - } -} - pub fn exit_unstable(api_name: &str) { eprintln!( "Unstable API '{}'. The --unstable flag must be provided.", @@ -71,7 +59,7 @@ pub fn exit_unstable(api_name: &str) { std::process::exit(70); } -impl ModuleLoader for State { +impl ModuleLoader for CliState { fn resolve( &self, specifier: &str, @@ -166,7 +154,7 @@ impl ModuleLoader for State { } } -impl State { +impl CliState { /// If `shared_permission` is None then permissions from globa state are used. pub fn new( global_state: &Arc, @@ -176,7 +164,7 @@ impl State { is_internal: bool, ) -> Result, AnyError> { let fl = &global_state.flags; - let state = State { + let state = CliState { global_state: global_state.clone(), main_module, permissions: shared_permissions @@ -204,7 +192,7 @@ impl State { main_module: ModuleSpecifier, ) -> Result, AnyError> { let fl = &global_state.flags; - let state = State { + let state = CliState { global_state: global_state.clone(), main_module, permissions: shared_permissions @@ -308,7 +296,7 @@ impl State { pub fn mock(main_module: &str) -> Rc { let module_specifier = ModuleSpecifier::resolve_url_or_path(main_module) .expect("Invalid entry module"); - State::new( + CliState::new( &GlobalState::mock(vec!["deno".to_string()], None), None, module_specifier, @@ -317,4 +305,16 @@ impl State { ) .unwrap() } + + /// Quits the process if the --unstable flag was not provided. + /// + /// This is intentionally a non-recoverable check so that people cannot probe + /// for unstable APIs from stable programs. + pub fn check_unstable(&self, api_name: &str) { + // TODO(ry) Maybe use IsolateHandle::terminate_execution here to provide a + // stack trace in JS. + if !self.global_state.flags.unstable { + exit_unstable(api_name); + } + } } diff --git a/cli/tsc.rs b/cli/tsc.rs index 0d29949db5..888c6fdeb9 100644 --- a/cli/tsc.rs +++ b/cli/tsc.rs @@ -19,7 +19,7 @@ use crate::msg::MediaType; use crate::ops; use crate::permissions::Permissions; use crate::source_maps::SourceMapGetter; -use crate::state::State; +use crate::state::CliState; use crate::tsc_config; use crate::version; use crate::worker::Worker; @@ -132,7 +132,7 @@ pub struct CompilerWorker { } impl CompilerWorker { - pub fn new(name: String, state: &Rc) -> Self { + pub fn new(name: String, state: &Rc) -> Self { let mut worker = Worker::new(name, Some(js::compiler_isolate_init()), state); let response = Arc::new(Mutex::new(None)); @@ -218,7 +218,7 @@ fn create_compiler_worker( let entry_point = ModuleSpecifier::resolve_url_or_path("./$deno$compiler.ts").unwrap(); let worker_state = - State::new(&global_state, Some(permissions), entry_point, None, true) + CliState::new(&global_state, Some(permissions), entry_point, None, true) .expect("Unable to create worker state"); // TODO(bartlomieju): this metric is never used anywhere diff --git a/cli/web_worker.rs b/cli/web_worker.rs index fac7946a69..3484a598d1 100644 --- a/cli/web_worker.rs +++ b/cli/web_worker.rs @@ -2,7 +2,7 @@ use crate::js; use crate::ops; -use crate::state::State; +use crate::state::CliState; use crate::worker::Worker; use crate::worker::WorkerEvent; use crate::worker::WorkerHandle; @@ -85,7 +85,7 @@ pub struct WebWorker { impl WebWorker { pub fn new( name: String, - state: &Rc, + state: &Rc, has_deno_namespace: bool, ) -> Self { let mut worker = Worker::new(name, Some(js::deno_isolate_init()), &state); @@ -239,12 +239,12 @@ impl Future for WebWorker { #[cfg(test)] mod tests { use super::*; - use crate::state::State; + use crate::state::CliState; use crate::tokio_util; use crate::worker::WorkerEvent; fn create_test_worker() -> WebWorker { - let state = State::mock("./hello.js"); + let state = CliState::mock("./hello.js"); let mut worker = WebWorker::new("TEST".to_string(), &state, false); worker .execute("bootstrap.workerRuntime(\"TEST\", false)") diff --git a/cli/worker.rs b/cli/worker.rs index 7e33221007..1265dedb4e 100644 --- a/cli/worker.rs +++ b/cli/worker.rs @@ -6,7 +6,7 @@ use crate::inspector::DenoInspector; use crate::js; use crate::ops; use crate::ops::io::get_stdio; -use crate::state::State; +use crate::state::CliState; use deno_core::error::AnyError; use deno_core::JsRuntime; use deno_core::ModuleId; @@ -94,7 +94,7 @@ pub struct Worker { pub name: String, pub isolate: JsRuntime, pub inspector: Option>, - pub state: Rc, + pub state: Rc, pub waker: AtomicWaker, pub(crate) internal_channels: WorkerChannelsInternal, external_channels: WorkerHandle, @@ -104,7 +104,7 @@ impl Worker { pub fn new( name: String, startup_snapshot: Option, - state: &Rc, + state: &Rc, ) -> Self { let mut isolate = JsRuntime::new(RuntimeOptions { module_loader: Some(state.clone()), @@ -264,7 +264,7 @@ impl MainWorker { fn new( name: String, startup_snapshot: Option, - state: &Rc, + state: &Rc, ) -> Self { let mut worker = Worker::new(name, startup_snapshot, state); { @@ -298,7 +298,7 @@ impl MainWorker { global_state: &Arc, main_module: ModuleSpecifier, ) -> Result { - let state = State::new( + let state = CliState::new( &global_state, None, main_module, @@ -362,7 +362,7 @@ mod tests { ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap(); let global_state = GlobalState::new(flags::Flags::default()).unwrap(); let state = - State::new(&global_state, None, module_specifier.clone(), None, false) + CliState::new(&global_state, None, module_specifier.clone(), None, false) .unwrap(); tokio_util::run_basic(async { let mut worker = MainWorker::new("TEST".to_string(), None, &state); @@ -389,7 +389,7 @@ mod tests { ModuleSpecifier::resolve_url_or_path(&p.to_string_lossy()).unwrap(); let global_state = GlobalState::new(flags::Flags::default()).unwrap(); let state = - State::new(&global_state, None, module_specifier.clone(), None, false) + CliState::new(&global_state, None, module_specifier.clone(), None, false) .unwrap(); tokio_util::run_basic(async { let mut worker = MainWorker::new("TEST".to_string(), None, &state); @@ -424,7 +424,7 @@ mod tests { }; let global_state = GlobalState::new(flags).unwrap(); let state = - State::new(&global_state, None, module_specifier.clone(), None, false) + CliState::new(&global_state, None, module_specifier.clone(), None, false) .unwrap(); let mut worker = MainWorker::new( "TEST".to_string(), @@ -445,7 +445,7 @@ mod tests { } fn create_test_worker() -> MainWorker { - let state = State::mock("./hello.js"); + let state = CliState::mock("./hello.js"); let mut worker = MainWorker::new( "TEST".to_string(), Some(js::deno_isolate_init()),