1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-21 23:04:45 -05:00

Rename deno::state::State to deno::state::CliState (#7480)

This commit is contained in:
Ryan Dahl 2020-09-14 21:22:32 -04:00 committed by GitHub
parent 192b8f4b99
commit 70f070706d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 43 deletions

View file

@ -60,12 +60,12 @@ where
}
/// Helper for extracting the commonly used state. Used for sync ops.
pub fn cli_state(state: &OpState) -> Rc<crate::state::State> {
state.borrow::<Rc<crate::state::State>>().clone()
pub fn cli_state(state: &OpState) -> Rc<crate::state::CliState> {
state.borrow::<Rc<crate::state::CliState>>().clone()
}
/// Helper for extracting the commonly used state. Used for async ops.
pub fn cli_state2(state: &Rc<RefCell<OpState>>) -> Rc<crate::state::State> {
pub fn cli_state2(state: &Rc<RefCell<OpState>>) -> Rc<crate::state::CliState> {
let state = state.borrow();
state.borrow::<Rc<crate::state::State>>().clone()
state.borrow::<Rc<crate::state::CliState>>().clone()
}

View file

@ -41,7 +41,7 @@ fn create_web_worker(
specifier: ModuleSpecifier,
has_deno_namespace: bool,
) -> Result<WebWorker, AnyError> {
let cli_state = crate::state::State::new_for_worker(
let cli_state = crate::state::CliState::new_for_worker(
global_state,
Some(permissions),
specifier,

View file

@ -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<GlobalState>,
pub permissions: RefCell<Permissions>,
pub main_module: ModuleSpecifier,
@ -49,20 +51,6 @@ pub struct State {
pub http_client: RefCell<reqwest::Client>,
}
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<GlobalState>,
@ -176,7 +164,7 @@ impl State {
is_internal: bool,
) -> Result<Rc<Self>, 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<Rc<Self>, 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<Self> {
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);
}
}
}

View file

@ -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<State>) -> Self {
pub fn new(name: String, state: &Rc<CliState>) -> 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

View file

@ -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>,
state: &Rc<CliState>,
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)")

View file

@ -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<Box<DenoInspector>>,
pub state: Rc<State>,
pub state: Rc<CliState>,
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<Snapshot>,
state: &Rc<State>,
state: &Rc<CliState>,
) -> 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<Snapshot>,
state: &Rc<State>,
state: &Rc<CliState>,
) -> Self {
let mut worker = Worker::new(name, startup_snapshot, state);
{
@ -298,7 +298,7 @@ impl MainWorker {
global_state: &Arc<GlobalState>,
main_module: ModuleSpecifier,
) -> Result<MainWorker, AnyError> {
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()),