From d7dce975f91255c6383f9cc6d3b8f818f6e0420b Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Thu, 8 Feb 2024 18:09:36 -0800 Subject: [PATCH] introduce native api to access RuntimeExecutor (#42882) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/42882 Changelog: [Android][Added] - introduce native api to access RuntimeExecutor This is the android equivalent of [PR#42758](https://github.com/facebook/react-native/pull/42758). From [PR#42758](https://github.com/facebook/react-native/pull/42758) > The goal of this API is to provide a safe way to access the jsi::runtime in bridgeless mode. The decision to limit access to the runtime in bridgeless was a conscious one - the runtime pointer is not thread-safe and its lifecycle must be managed correctly by owners. > However, interacting with the runtime is an advanced use case we would want to support. Our recommended ways to access the runtime in bridgeless mode is either 1) via the RuntimeExecutor, or 2) via a C++ TurboModule. This diff introduces the API that would allow for 1). because react context can be non-null before react instance is ready, this can still return null. however, the callsite should be cognizant of when this will happen. in the case of expomodules, the runtime should be ready when the module is init, unless it is a eager initialized module Reviewed By: RSNara Differential Revision: D53461821 fbshipit-source-id: 69555d0593a59f8655e4dcd2f0ef1f78f4cfff7d --- .../facebook/react/bridge/ReactContext.java | 17 ++++++++++++++++ .../react/runtime/BridgelessReactContext.java | 13 ++++++++++++ .../facebook/react/runtime/ReactHostImpl.java | 20 +++++++++++++++++-- .../facebook/react/runtime/ReactInstance.java | 2 +- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java index d247714c51eaa5..4f5af10bde7056 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ReactContext.java @@ -27,6 +27,8 @@ import com.facebook.react.common.LifecycleState; import com.facebook.react.common.ReactConstants; import com.facebook.react.common.annotations.DeprecatedInNewArchitecture; +import com.facebook.react.common.annotations.FrameworkAPI; +import com.facebook.react.common.annotations.UnstableReactNativeAPI; import java.lang.ref.WeakReference; import java.util.Collection; import java.util.concurrent.CopyOnWriteArraySet; @@ -36,6 +38,7 @@ * CatalystInstance} */ public class ReactContext extends ContextWrapper { + @DoNotStrip public interface RCTDeviceEventEmitter extends JavaScriptModule { void emit(@NonNull String eventName, @Nullable Object data); @@ -206,6 +209,20 @@ public T getNativeModule(Class nativeModuleInterface return mCatalystInstance.getNativeModule(nativeModuleInterface); } + /** + * @return the RuntimeExecutor, a thread-safe handler for accessing the runtime. + * @experimental + */ + @Nullable + @FrameworkAPI + @UnstableReactNativeAPI + public RuntimeExecutor getRuntimeExecutor() { + if (mCatalystInstance == null) { + raiseCatalystInstanceMissingException(); + } + return mCatalystInstance.getRuntimeExecutor(); + } + /** * Calls RCTDeviceEventEmitter.emit to JavaScript, with given event name and an optional list of * arguments. diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java index edaebd1cd0112e..742775ea32bda8 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/BridgelessReactContext.java @@ -19,8 +19,11 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException; import com.facebook.react.bridge.ReactSoftExceptionLogger; +import com.facebook.react.bridge.RuntimeExecutor; import com.facebook.react.bridge.UIManager; import com.facebook.react.bridge.WritableNativeArray; +import com.facebook.react.common.annotations.FrameworkAPI; +import com.facebook.react.common.annotations.UnstableReactNativeAPI; import com.facebook.react.config.ReactFeatureFlags; import com.facebook.react.devsupport.interfaces.DevSupportManager; import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler; @@ -149,6 +152,16 @@ public Collection getNativeModules() { public @Nullable T getNativeModule(Class nativeModuleInterface) { return mReactHost.getNativeModule(nativeModuleInterface); } + /** + * @return the RuntimeExecutor, a thread-safe handler for accessing the runtime. If the runtime is + * not initialized yet, it will return null. + */ + @Override + @FrameworkAPI + @UnstableReactNativeAPI + public @Nullable RuntimeExecutor getRuntimeExecutor() { + return mReactHost.getRuntimeExecutor(); + } @Override public void handleException(Exception e) { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java index cd929cc3b50d24..08dca74feed53e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactHostImpl.java @@ -38,6 +38,7 @@ import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException; import com.facebook.react.bridge.ReactNoCrashSoftException; import com.facebook.react.bridge.ReactSoftExceptionLogger; +import com.facebook.react.bridge.RuntimeExecutor; import com.facebook.react.bridge.UiThreadUtil; import com.facebook.react.bridge.queue.QueueThreadExceptionHandler; import com.facebook.react.bridge.queue.ReactQueueConfiguration; @@ -542,7 +543,8 @@ private void setCurrentActivity(@Nullable Activity activity) { return reactInstance.getEventDispatcher(); } - /* package */ @Nullable + /* package */ + @Nullable FabricUIManager getUIManager() { final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult(); if (reactInstance == null) { @@ -567,7 +569,8 @@ FabricUIManager getUIManager() { return new ArrayList<>(); } - /* package */ @Nullable + /* package */ + @Nullable T getNativeModule(Class nativeModuleInterface) { if (nativeModuleInterface == UIManagerModule.class) { ReactSoftExceptionLogger.logSoftExceptionVerbose( @@ -583,6 +586,19 @@ T getNativeModule(Class nativeModuleInterface) { return null; } + /* package */ + @Nullable + RuntimeExecutor getRuntimeExecutor() { + final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult(); + if (reactInstance != null) { + return reactInstance.getBufferedRuntimeExecutor(); + } + ReactSoftExceptionLogger.logSoftException( + TAG, + new ReactNoCrashSoftException("Tried to get runtime executor while instance is not ready")); + return null; + } + /* package */ DefaultHardwareBackBtnHandler getDefaultBackButtonHandler() { return () -> { diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java index f9191d23d5b81d..d0d42462bf80f6 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/runtime/ReactInstance.java @@ -471,7 +471,7 @@ private native HybridData initHybrid( private native RuntimeExecutor getUnbufferedRuntimeExecutor(); - private native RuntimeExecutor getBufferedRuntimeExecutor(); + /* package */ native RuntimeExecutor getBufferedRuntimeExecutor(); private native RuntimeScheduler getRuntimeScheduler();