Allow ReactInstance to evaluate SH unit when possible (#53471)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/53471

When IHermes::getSHUnitCreator() returns non-null pointer, call
`evaluateSHUnit()` on that instead of `evaluateJavaScript()`.

Changelog: [Internal]

Reviewed By: dannysu

Differential Revision: D80916868

fbshipit-source-id: 9c1e2327b720cab4b374d4752a9c64f87c592ad6
This commit is contained in:
Gang Zhao
2025-09-04 16:27:46 -07:00
committed by Facebook GitHub Bot
parent 1f57ae5249
commit d7315563fe
@@ -15,6 +15,7 @@
#include <cxxreact/TraceSection.h>
#include <glog/logging.h>
#include <jsi/JSIDynamic.h>
#include <jsi/hermes.h>
#include <jsi/instrumentation.h>
#include <jsinspector-modern/HostTarget.h>
#include <jsireact/JSIExecutor.h>
@@ -242,7 +243,17 @@ void ReactInstance::loadScript(
ReactMarker::RUN_JS_BUNDLE_START, scriptName.c_str());
}
runtime.evaluateJavaScript(buffer, sourceURL);
// Check if the shermes unit is avaliable.
auto* shUnitAPI = jsi::castInterface<hermes::IHermesSHUnit>(&runtime);
auto* shUnitCreator = shUnitAPI ? shUnitAPI->getSHUnitCreator() : nullptr;
if (shUnitCreator) {
LOG(WARNING) << "ReactInstance: evaluateSHUnit";
auto* hermesAPI = jsi::castInterface<hermes::IHermes>(&runtime);
hermesAPI->evaluateSHUnit(shUnitCreator);
} else {
LOG(WARNING) << "ReactInstance: evaluateJavaScript() with JS bundle";
runtime.evaluateJavaScript(buffer, sourceURL);
}
/**
* TODO(T183610671): We need a safe/reliable way to enable the js