From 65e72b68acf57da8462b8e7b057e7adb9393b698 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 11 Dec 2020 18:49:26 +0100 Subject: 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. --- cli/ops/mod.rs | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'cli/ops/mod.rs') 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(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::>().check_unstable(api_name) + state.borrow::().check_unstable(api_name) } /// Helper for checking unstable features. Used for async ops. pub fn check_unstable2(state: &Rc>, api_name: &str) { let state = state.borrow(); - state.borrow::>().check_unstable(api_name) -} - -/// Helper for extracting the commonly used state. Used for sync ops. -pub fn program_state(state: &OpState) -> Arc { - state.borrow::>().clone() -} - -/// Helper for extracting the commonly used state. Used for async ops. -pub fn global_state2(state: &Rc>) -> Arc { - let state = state.borrow(); - state.borrow::>().clone() + state.borrow::().check_unstable(api_name) } -- cgit v1.2.3