summaryrefslogtreecommitdiff
path: root/cli/ops/mod.rs
diff options
context:
space:
mode:
authorBartek IwaƄczuk <biwanczuk@gmail.com>2020-12-11 18:49:26 +0100
committerGitHub <noreply@github.com>2020-12-11 18:49:26 +0100
commit65e72b68acf57da8462b8e7b057e7adb9393b698 (patch)
tree00e955e1186b9512b009acbb6ee80feb8a3f1733 /cli/ops/mod.rs
parent9414dee9e56a9f42c07ada4f8e1be864a1a1b936 (diff)
refactor(cli): decouple ops from ProgramState and Flags (#8659)
This commit does major refactor of "Worker" and "WebWorker", in order to decouple them from "ProgramState" and "Flags". The main points of interest are "create_main_worker()" and "create_web_worker_callback()" functions which are responsible for creating "Worker" and "WebWorker" in CLI context. As a result it is now possible to factor out common "runtime" functionality into a separate crate.
Diffstat (limited to 'cli/ops/mod.rs')
-rw-r--r--cli/ops/mod.rs37
1 files changed, 22 insertions, 15 deletions
diff --git a/cli/ops/mod.rs b/cli/ops/mod.rs
index b450f8989..56c0f1ad5 100644
--- a/cli/ops/mod.rs
+++ b/cli/ops/mod.rs
@@ -27,7 +27,6 @@ pub mod websocket;
pub mod worker_host;
use crate::metrics::metrics_op;
-use crate::program_state::ProgramState;
use deno_core::error::AnyError;
use deno_core::json_op_async;
use deno_core::json_op_sync;
@@ -39,7 +38,6 @@ use deno_core::ZeroCopyBuf;
use std::cell::RefCell;
use std::future::Future;
use std::rc::Rc;
-use std::sync::Arc;
pub fn reg_json_async<F, R>(rt: &mut JsRuntime, name: &'static str, op_fn: F)
where
@@ -57,24 +55,33 @@ where
rt.register_op(name, metrics_op(json_op_sync(op_fn)));
}
+pub struct UnstableChecker {
+ pub unstable: bool,
+}
+
+impl UnstableChecker {
+ /// 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.
+ // NOTE(bartlomieju): keep in sync with `cli/program_state.rs`
+ pub fn check_unstable(&self, api_name: &str) {
+ if !self.unstable {
+ eprintln!(
+ "Unstable API '{}'. The --unstable flag must be provided.",
+ api_name
+ );
+ std::process::exit(70);
+ }
+ }
+}
/// Helper for checking unstable features. Used for sync ops.
pub fn check_unstable(state: &OpState, api_name: &str) {
- state.borrow::<Arc<ProgramState>>().check_unstable(api_name)
+ state.borrow::<UnstableChecker>().check_unstable(api_name)
}
/// Helper for checking unstable features. Used for async ops.
pub fn check_unstable2(state: &Rc<RefCell<OpState>>, api_name: &str) {
let state = state.borrow();
- state.borrow::<Arc<ProgramState>>().check_unstable(api_name)
-}
-
-/// Helper for extracting the commonly used state. Used for sync ops.
-pub fn program_state(state: &OpState) -> Arc<ProgramState> {
- state.borrow::<Arc<ProgramState>>().clone()
-}
-
-/// Helper for extracting the commonly used state. Used for async ops.
-pub fn global_state2(state: &Rc<RefCell<OpState>>) -> Arc<ProgramState> {
- let state = state.borrow();
- state.borrow::<Arc<ProgramState>>().clone()
+ state.borrow::<UnstableChecker>().check_unstable(api_name)
}