From aeaae4294f0765135f7c727dfa62bc2380bd267c Mon Sep 17 00:00:00 2001 From: Andrew Datsenko Date: Tue, 17 Jun 2025 04:18:08 -0700 Subject: [PATCH] Add hermes and jsi (#52060) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/52060 Changelog: [Internal] Add hermes and jsi so we can evaluate JS. Reviewed By: christophpurrer Differential Revision: D76746362 fbshipit-source-id: a5fbb90d06a8608154b6ae80242b969e7cb03cea --- .../hermes-engine/build.gradle.kts | 18 +++++++++++ private/react-native-fantom/build.gradle.kts | 2 ++ .../react-native-fantom/tester/CMakeLists.txt | 10 +++++- private/react-native-fantom/tester/build.sh | 1 + .../react-native-fantom/tester/src/main.cpp | 31 +++++++++++++++++++ 5 files changed, 61 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts b/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts index 466a4e4b3bf..d0228e0d4d9 100644 --- a/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts +++ b/packages/react-native/ReactAndroid/hermes-engine/build.gradle.kts @@ -186,6 +186,24 @@ val prepareHeadersForPrefab by into(prefabHeadersDir) } +val buildHermesLib by + tasks.registering(CustomExecTask::class) { + dependsOn(buildHermesC) + workingDir(hermesDir) + inputs.files(hermesBuildOutputFileTree) + commandLine( + cmakeBinaryPath, + "--build", + hermesBuildDir.toString(), + "--target", + "libhermes", + "-j", + ndkBuildJobs, + ) + standardOutputFile.set(project.file("$buildDir/build-hermes-lib.log")) + errorOutputFile.set(project.file("$buildDir/build-hermes-lib.error.log")) + } + fun windowsAwareCommandLine(vararg commands: String): List { val result = if (Os.isFamily(Os.FAMILY_WINDOWS)) { diff --git a/private/react-native-fantom/build.gradle.kts b/private/react-native-fantom/build.gradle.kts index 35c6daf1ed5..bf3dfd42f88 100644 --- a/private/react-native-fantom/build.gradle.kts +++ b/private/react-native-fantom/build.gradle.kts @@ -59,5 +59,7 @@ val prepareNative3pDependencies by tasks.registering { dependsOn( prepareGflags, + ":packages:react-native:ReactAndroid:hermes-engine:buildHermesLib", + ":packages:react-native:ReactAndroid:hermes-engine:prepareHeadersForPrefab", ) } diff --git a/private/react-native-fantom/tester/CMakeLists.txt b/private/react-native-fantom/tester/CMakeLists.txt index 1845870fa62..395a657a044 100644 --- a/private/react-native-fantom/tester/CMakeLists.txt +++ b/private/react-native-fantom/tester/CMakeLists.txt @@ -8,6 +8,11 @@ set(CMAKE_VERBOSE_MAKEFILE on) project(fantom_tester) +find_library(LIB_HERMES libhermes + HINTS ${REACT_ANDROID_DIR}/hermes-engine/build/hermes/API/hermes + REQUIRED) +include_directories(${REACT_ANDROID_DIR}/hermes-engine/build/prefab-headers) + include(${REACT_COMMON_DIR}/cmake-utils/react-native-flags.cmake) # Convert input paths to CMake format (with forward slashes) @@ -23,7 +28,7 @@ function(add_third_party_subdir relative_path) endfunction() function(add_react_common_subdir relative_path) - add_subdirectory(${REACT_COMMON_DIR}/${relative_path} src/${relative_path}) + add_subdirectory(${REACT_COMMON_DIR}/${relative_path} ReactCommon/${relative_path}) endfunction() function(add_fantom_third_party_subdir relative_path) @@ -42,6 +47,7 @@ add_fantom_third_party_subdir(gflags) # Common targets add_react_common_subdir(yoga) +add_react_common_subdir(jsi) add_react_common_subdir(react/featureflags) file(GLOB SOURCES "src/*.cpp" "src/*.h") @@ -51,6 +57,8 @@ target_link_libraries(fantom_tester PRIVATE glog gflags + jsi + ${LIB_HERMES} boost double-conversion fast_float diff --git a/private/react-native-fantom/tester/build.sh b/private/react-native-fantom/tester/build.sh index 7e68d7f3453..d17bfe0f957 100755 --- a/private/react-native-fantom/tester/build.sh +++ b/private/react-native-fantom/tester/build.sh @@ -11,6 +11,7 @@ BUILD_DIR="$SCRIPT_DIR/build" REACT_NATIVE_ROOT_DIR=$(readlink -f "$SCRIPT_DIR/../../../packages/react-native") cmake -S "$SCRIPT_DIR" -B "$BUILD_DIR" \ + -DREACT_ANDROID_DIR="${REACT_NATIVE_ROOT_DIR}/ReactAndroid" \ -DFANTOM_THIRD_PARTY_DIR="${SCRIPT_DIR}/../build/third-party" \ -DREACT_THIRD_PARTY_NDK_DIR="${REACT_NATIVE_ROOT_DIR}/ReactAndroid/build/third-party-ndk" \ -DREACT_COMMON_DIR="${REACT_NATIVE_ROOT_DIR}/ReactCommon" diff --git a/private/react-native-fantom/tester/src/main.cpp b/private/react-native-fantom/tester/src/main.cpp index ac5dcba560f..38ca0e9813b 100644 --- a/private/react-native-fantom/tester/src/main.cpp +++ b/private/react-native-fantom/tester/src/main.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -23,6 +25,8 @@ DEFINE_string( "JSON representation of the common feature flags to set for the app"); using namespace facebook::react; +using namespace facebook::hermes; +using namespace facebook::jsi; static void setUpLogging() { google::InitGoogleLogging("react-native-fantom"); @@ -46,6 +50,31 @@ static folly::dynamic setUpFeatureFlags() { return dynamicFeatureFlags; } +void createHermesInstance() { + auto gcConfig = ::hermes::vm::GCConfig::Builder() + // Default to 3GB + .withMaxHeapSize(3072 << 20) + .withName("RNBridgeless"); + ::hermes::vm::RuntimeConfig::Builder runtimeConfigBuilder = + ::hermes::vm::RuntimeConfig::Builder() + .withGCConfig(gcConfig.build()) + .withEnableSampleProfiling(true); + + std::unique_ptr hermesRuntime = + makeHermesRuntime(runtimeConfigBuilder.build()); + + hermesRuntime->evaluateJavaScript( + std::make_unique( + "var fantom = 'Hello, I am fantom_tester'"), + "script.js"); + + LOG(INFO) << "JS evaluated value: " + << hermesRuntime->global() + .getProperty(*hermesRuntime, "fantom") + .getString(*hermesRuntime) + .utf8(*hermesRuntime); +} + int main(int argc, char* argv[]) { if (argc > 0 && argv != nullptr) { // Don't exit app on unknown flags, as some of those may be provided when @@ -64,5 +93,7 @@ int main(int argc, char* argv[]) { LOG(INFO) << fmt::format( "[FeatureFlags] overrides: {}", folly::toJson(dynamicFeatureFlags)); + createHermesInstance(); + return 0; }