From 7b7c9faa069d0817548ab92549a42dc5826c47f9 Mon Sep 17 00:00:00 2001 From: Alex Hunt Date: Wed, 29 Nov 2023 08:40:47 -0800 Subject: [PATCH] Add InspectorFlags, conditionally disable Hermes CDP registration (#41672) Summary: Progress towards an opt-in setup for our new CDP backend. - Adds `InspectorFlags.h`, a singleton intended to allow convienient access to static boolean feature flags for the new CDP backend/inspector features across platforms. This will be written to in upcoming diffs, with the accessor for `enable_modern_cdp_registry` soft-defaulting to `false` here. - References this to conditionally disable CDP registration in `HermesExecutorFactory` (Bridge) and `HermesInstance` (Bridgeless) code paths. - Stubs a `false` value for `react_native_devx:enable_modern_cdp_registry` in `EmptyReactNativeConfig` (documentation/convenience point for open source partners and integrators). Changelog: [Internal] Differential Revision: D51563107 --- .../hermes/executor/HermesExecutorFactory.cpp | 6 ++++- .../jsinspector-modern/InspectorFlags.h | 24 +++++++++++++++++++ .../React-jsinspector.podspec | 2 ++ .../react/config/ReactNativeConfig.cpp | 3 +++ .../react/runtime/hermes/HermesInstance.cpp | 11 +++++---- 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 packages/react-native/ReactCommon/jsinspector-modern/InspectorFlags.h diff --git a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp index a1fc1e773cc712..b00d18723c028e 100644 --- a/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp +++ b/packages/react-native/ReactCommon/hermes/executor/HermesExecutorFactory.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include @@ -200,11 +201,14 @@ std::unique_ptr HermesExecutorFactory::createJSExecutor( } HermesRuntime& hermesRuntimeRef = *hermesRuntime; + bool enableDebugger = + !jsinspector_modern::InspectorFlags::enableModernCDPRegistry && + enableDebugger_; auto decoratedRuntime = std::make_shared( std::move(hermesRuntime), hermesRuntimeRef, jsQueue, - enableDebugger_, + enableDebugger, debuggerName_); // So what do we have now? diff --git a/packages/react-native/ReactCommon/jsinspector-modern/InspectorFlags.h b/packages/react-native/ReactCommon/jsinspector-modern/InspectorFlags.h new file mode 100644 index 00000000000000..d8d2d8a7c7479c --- /dev/null +++ b/packages/react-native/ReactCommon/jsinspector-modern/InspectorFlags.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +namespace facebook::react::jsinspector_modern { + +/** + * A container for all inspector related feature flags. These will be set + * externally prior to React Native initialization. + */ +class InspectorFlags { + public: + /** + * Primary flag to enable the modern CDP backend. + */ + static bool enableModernCDPRegistry; +}; + +} // namespace facebook::react::jsinspector_modern diff --git a/packages/react-native/ReactCommon/jsinspector-modern/React-jsinspector.podspec b/packages/react-native/ReactCommon/jsinspector-modern/React-jsinspector.podspec index 76bb5b0413d4e7..d72a392ed8ccd0 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/React-jsinspector.podspec +++ b/packages/react-native/ReactCommon/jsinspector-modern/React-jsinspector.podspec @@ -28,4 +28,6 @@ Pod::Spec.new do |s| s.source_files = "*.{cpp,h}" s.header_dir = 'jsinspector' s.pod_target_xcconfig = { "CLANG_CXX_LANGUAGE_STANDARD" => "c++20" } + + add_dependency(s, "React-nativeconfig") end diff --git a/packages/react-native/ReactCommon/react/config/ReactNativeConfig.cpp b/packages/react-native/ReactCommon/react/config/ReactNativeConfig.cpp index 643c197297054b..af75c450789b13 100644 --- a/packages/react-native/ReactCommon/react/config/ReactNativeConfig.cpp +++ b/packages/react-native/ReactCommon/react/config/ReactNativeConfig.cpp @@ -23,6 +23,9 @@ bool EmptyReactNativeConfig::getBool(const std::string& param) const { if (param == "react_fabric:enabled_layout_animations_ios") { return true; } + if (param == "react_native_devx:enable_modern_cdp_registry") { + return false; + } return false; } diff --git a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp index 0918259c77e93c..b9b5436fb8bbae 100644 --- a/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp +++ b/packages/react-native/ReactCommon/react/runtime/hermes/HermesInstance.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #endif using namespace facebook::hermes; @@ -141,10 +142,12 @@ std::unique_ptr HermesInstance::createJSRuntime( hermes::makeHermesRuntime(runtimeConfigBuilder.build()); #ifdef HERMES_ENABLE_DEBUGGER - std::unique_ptr decoratedRuntime = - std::make_unique( - std::move(hermesRuntime), msgQueueThread); - return std::make_unique(std::move(decoratedRuntime)); + if (!jsinspector_modern::InspectorFlags::enableModernCDPRegistry) { + std::unique_ptr decoratedRuntime = + std::make_unique( + std::move(hermesRuntime), msgQueueThread); + return std::make_unique(std::move(decoratedRuntime)); + } #endif return std::make_unique(std::move(hermesRuntime));