Skip to content

Commit

Permalink
build backwards compat API for runtime pointer (#43013)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #43013

Changelog: [Android][Added]

This is a pre-deprecated API to give access to the jsi::Runtime in Android in bridgeless. In bridge, this value is exposed via the ReactContext, but is not implemented in the BridgelessReactContext. We do that here.

This should work out of the box in bridgeless if you are already retrieveing the pointer via ReactContext. However, we recommend users to eventually migrate towards C++ TurboModule or the RuntimeExecutor if possible. This will be removed in the future.

Reviewed By: RSNara

Differential Revision: D53645247

fbshipit-source-id: b98657560c43a625bdf947d19d186952c9b44364
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 15, 2024
1 parent 45b1aef commit 315be82
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.CatalystInstance;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.JavaScriptModuleRegistry;
import com.facebook.react.bridge.NativeArray;
Expand Down Expand Up @@ -163,6 +164,13 @@ public Collection<NativeModule> getNativeModules() {
return mReactHost.getRuntimeExecutor();
}

@Override
@FrameworkAPI
@UnstableReactNativeAPI
public @Nullable JavaScriptContextHolder getJavaScriptContextHolder() {
return mReactHost.getJavaScriptContextHolder();
}

@Override
public void handleException(Exception e) {
mReactHost.handleHostException(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import com.facebook.react.ReactInstanceEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.MemoryPressureListener;
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeModule;
Expand Down Expand Up @@ -599,6 +600,15 @@ RuntimeExecutor getRuntimeExecutor() {
return null;
}

@Nullable
JavaScriptContextHolder getJavaScriptContextHolder() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
if (reactInstance != null) {
return reactInstance.getJavaScriptContextHolder();
}
return null;
}

/* package */
DefaultHardwareBackBtnHandler getDefaultBackButtonHandler() {
return () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.JSBundleLoader;
import com.facebook.react.bridge.JSBundleLoaderDelegate;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.NativeArray;
import com.facebook.react.bridge.NativeMap;
Expand Down Expand Up @@ -95,6 +96,8 @@ final class ReactInstance {
private final JavaTimerManager mJavaTimerManager;
private final BridgelessViewManagerResolver mViewManagerResolver;

private JavaScriptContextHolder mJavaScriptContextHolder;

@DoNotStrip @Nullable private ComponentNameResolverManager mComponentNameResolverManager;
@DoNotStrip @Nullable private UIConstantsProviderManager mUIConstantsProviderManager;

Expand Down Expand Up @@ -181,6 +184,8 @@ public void onHostDestroy() {
bindingsInstaller,
isProfiling);

mJavaScriptContextHolder = new JavaScriptContextHolder(getJavaScriptContext());

// Set up TurboModules
Systrace.beginSection(
Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "ReactInstance.initialize#initTurboModules");
Expand Down Expand Up @@ -430,6 +435,10 @@ public Collection<NativeModule> getNativeModules() {
mFabricUIManager.stopSurface(surface.getSurfaceHandler());
}

/* package */ JavaScriptContextHolder getJavaScriptContextHolder() {
return mJavaScriptContextHolder;
}

/* --- Lifecycle methods --- */
@ThreadConfined("ReactHost")
/* package */ void destroy() {
Expand All @@ -440,6 +449,7 @@ public Collection<NativeModule> getNativeModules() {
mHybridData.resetNative();
mComponentNameResolverManager = null;
mUIConstantsProviderManager = null;
mJavaScriptContextHolder.clear();
}

/* --- Native methods --- */
Expand Down Expand Up @@ -475,6 +485,8 @@ private native HybridData initHybrid(

private native RuntimeScheduler getRuntimeScheduler();

private native long getJavaScriptContext();

/* package */ native void callFunctionOnModule(
String moduleName, String methodName, NativeArray args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ void JReactInstance::handleMemoryPressureJs(jint level) {
instance_->handleMemoryPressureJs(level);
}

jlong JReactInstance::getJavaScriptContext() {
return (jlong)(intptr_t)instance_->getJavaScriptContext();
}

void JReactInstance::registerNatives() {
registerHybrid({
makeNativeMethod("initHybrid", JReactInstance::initHybrid),
Expand Down Expand Up @@ -230,6 +234,8 @@ void JReactInstance::registerNatives() {
"registerSegmentNative", JReactInstance::registerSegment),
makeNativeMethod(
"handleMemoryPressureJs", JReactInstance::handleMemoryPressureJs),
makeNativeMethod(
"getJavaScriptContext", JReactInstance::getJavaScriptContext),
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ class JReactInstance : public jni::HybridClass<JReactInstance> {
nativeMethodCallInvokerHolder_;
jni::global_ref<JReactExceptionManager::javaobject> jReactExceptionManager_;
jni::global_ref<JBindingsInstaller::javaobject> jBindingsInstaller_;

jlong getJavaScriptContext();
};

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -465,4 +465,8 @@ void ReactInstance::handleMemoryPressureJs(int pressureLevel) {
}
}

void* ReactInstance::getJavaScriptContext() {
return &runtime_->getRuntime();
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class ReactInstance final : private jsinspector_modern::InstanceTargetDelegate {
*/
void unregisterFromInspector();

void* getJavaScriptContext();

private:
std::shared_ptr<JSRuntime> runtime_;
std::shared_ptr<MessageQueueThread> jsMessageQueueThread_;
Expand Down

0 comments on commit 315be82

Please sign in to comment.