Skip to content

Commit

Permalink
Pass reactNativeConfig to HermesInstance when not using the native mo…
Browse files Browse the repository at this point in the history
…dule version (facebook#41082)

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]

Reviewed By: sammy-SC

Differential Revision: D50450488
  • Loading branch information
rubennorte authored and facebook-github-bot committed Oct 23, 2023
1 parent 79d620d commit 7dfce4e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 Any?)) {

constructor() : this(null)

companion object {
@JvmStatic @DoNotStrip protected external fun initHybrid(): HybridData?
@JvmStatic @DoNotStrip protected external fun initHybrid(reactNativeConfig: Any?): HybridData

init {
SoLoader.loadLibrary("hermesinstancejni")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,18 @@
#include "JHermesInstance.h"

#include <fbjni/fbjni.h>
#include <react/fabric/ReactNativeConfigHolder.h>

namespace facebook::react {

jni::local_ref<JHermesInstance::jhybriddata> JHermesInstance::initHybrid(
jni::alias_ref<jhybridobject>) {
return makeCxxInstance();
jni::alias_ref<jclass> /* unused */,
jni::alias_ref<jobject> reactNativeConfig) {
std::shared_ptr<const ReactNativeConfig> config = reactNativeConfig != nullptr
? std::make_shared<const ReactNativeConfigHolder>(reactNativeConfig)
: nullptr;

return makeCxxInstance(config);
}

void JHermesInstance::registerNatives() {
Expand All @@ -24,8 +30,8 @@ void JHermesInstance::registerNatives() {

std::unique_ptr<jsi::Runtime> JHermesInstance::createJSRuntime(
std::shared_ptr<MessageQueueThread> 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
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#pragma once

#include <memory>
#include <string>

#include <cxxreact/MessageQueueThread.h>
#include <fbjni/fbjni.h>
#include <jni.h>
#include <jsi/jsi.h>
#include <react/config/ReactNativeConfig.h>
#include <react/runtime/JSEngineInstance.h>
#include <react/runtime/hermes/HermesInstance.h>
#include "../../jni/JJSEngineInstance.h"
Expand All @@ -26,17 +26,24 @@ class JHermesInstance
static constexpr auto kJavaDescriptor =
"Lcom/facebook/react/runtime/hermes/HermesInstance;";

static jni::local_ref<jhybriddata> initHybrid(jni::alias_ref<jhybridobject>);
static jni::local_ref<jhybriddata> initHybrid(
jni::alias_ref<jclass> /* unused */,
jni::alias_ref<jobject> reactNativeConfig);

static void registerNatives();

JHermesInstance(std::shared_ptr<const ReactNativeConfig> reactNativeConfig)
: reactNativeConfig_(reactNativeConfig){};

std::unique_ptr<jsi::Runtime> createJSRuntime(
std::shared_ptr<MessageQueueThread> msgQueueThread) noexcept;

~JHermesInstance() {}

private:
friend HybridBase;

std::shared_ptr<const ReactNativeConfig> reactNativeConfig_;
};

} // namespace facebook::react

0 comments on commit 7dfce4e

Please sign in to comment.