From 681896d86f3f290987d90c3bd50d64c7fd47a930 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 19 Oct 2023 08:28:30 -0700 Subject: [PATCH] Pass reactNativeConfig to HermesInstance when not using the native module version Summary: We're testing a method to access `ReactNativeConfig` without a dependency on native modules, so we can access it before that infra is initialized in places like Hermes or RuntimeScheduler. When we're in that variant, this passes the configuration to Hermes so we can use it to set flags in the runtime (like enabling microtasks in D50177355). Changelog: [internal] Differential Revision: D50450488 fbshipit-source-id: a8a50d62afaf349acf369f6ea2909db3c1e03a65 --- .../react/runtime/hermes/HermesInstance.kt | 8 ++++++-- .../react/runtime/hermes/jni/JHermesInstance.cpp | 14 ++++++++++---- .../jni/react/runtime/hermes/jni/JHermesInstance.h | 11 +++++++++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/hermes/HermesInstance.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/hermes/HermesInstance.kt index 2f7697b249405b..41f3b4b83854c0 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/hermes/HermesInstance.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/hermes/HermesInstance.kt @@ -9,13 +9,17 @@ package com.facebook.react.runtime.hermes import com.facebook.jni.HybridData import com.facebook.jni.annotations.DoNotStrip +import com.facebook.react.fabric.ReactNativeConfig import com.facebook.react.runtime.JSEngineInstance import com.facebook.soloader.SoLoader -class HermesInstance : JSEngineInstance(initHybrid()!!) { +class HermesInstance constructor(reactNativeConfig: ReactNativeConfig?) : + JSEngineInstance(initHybrid(reactNativeConfig as Object?)) { + + constructor() : this(null) companion object { - @JvmStatic @DoNotStrip protected external fun initHybrid(): HybridData? + @JvmStatic @DoNotStrip protected external fun initHybrid(reactNativeConfig: Object?): HybridData init { SoLoader.loadLibrary("hermesinstancejni") diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.cpp index 1de9439dc87024..3d8a9f74652443 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.cpp @@ -8,12 +8,18 @@ #include "JHermesInstance.h" #include +#include namespace facebook::react { jni::local_ref JHermesInstance::initHybrid( - jni::alias_ref) { - return makeCxxInstance(); + jni::alias_ref /* unused */, + jni::alias_ref reactNativeConfig) { + std::shared_ptr config = reactNativeConfig != nullptr + ? std::make_shared(reactNativeConfig) + : nullptr; + + return makeCxxInstance(config); } void JHermesInstance::registerNatives() { @@ -24,8 +30,8 @@ void JHermesInstance::registerNatives() { std::unique_ptr JHermesInstance::createJSRuntime( std::shared_ptr msgQueueThread) noexcept { - // TODO T105438175 Pass ReactNativeConfig to init Hermes with MobileConfig - return HermesInstance::createJSRuntime(nullptr, nullptr, msgQueueThread); + return HermesInstance::createJSRuntime( + reactNativeConfig_, nullptr, msgQueueThread); } } // namespace facebook::react diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.h b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.h index db12cfc4d5fcb8..9d500aaa3c3c30 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/runtime/hermes/jni/JHermesInstance.h @@ -8,12 +8,12 @@ #pragma once #include -#include #include #include #include #include +#include #include #include #include "../../jni/JJSEngineInstance.h" @@ -26,10 +26,15 @@ class JHermesInstance static constexpr auto kJavaDescriptor = "Lcom/facebook/react/runtime/hermes/HermesInstance;"; - static jni::local_ref initHybrid(jni::alias_ref); + static jni::local_ref initHybrid( + jni::alias_ref /* unused */, + jni::alias_ref reactNativeConfig); static void registerNatives(); + JHermesInstance(std::shared_ptr reactNativeConfig) + : reactNativeConfig_(reactNativeConfig){}; + std::unique_ptr createJSRuntime( std::shared_ptr msgQueueThread) noexcept; @@ -37,6 +42,8 @@ class JHermesInstance private: friend HybridBase; + + std::shared_ptr reactNativeConfig_; }; } // namespace facebook::react