From bb2ee8221831b322879f0adf148b5eedf134b511 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Tue, 6 Feb 2024 00:12:44 -0800 Subject: [PATCH] introduce native api to access RuntimeExecutor (#42882) Summary: 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, i created an async api that will guarantee the runtime executor will be ready. Differential Revision: D53461821 --- .../facebook/react/bridge/ReactContext.java | 14 +++++++++++ .../react/modules/fresco/FrescoModule.java | 10 ++++++++ .../react/runtime/BridgelessReactContext.java | 5 ++++ .../facebook/react/runtime/ReactHostImpl.java | 25 +++++++++++++++++-- .../facebook/react/runtime/ReactInstance.java | 2 +- 5 files changed, 53 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..478ce3af5aa3f8 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 @@ -36,11 +36,18 @@ * CatalystInstance} */ public class ReactContext extends ContextWrapper { + @DoNotStrip public interface RCTDeviceEventEmitter extends JavaScriptModule { + void emit(@NonNull String eventName, @Nullable Object data); } + public interface ReactRuntimeExecutor { + + void execute(RuntimeExecutor runtimeExecutor); + } + private static final String TAG = "ReactContext"; private static final String EARLY_JS_ACCESS_EXCEPTION_MESSAGE = "Tried to access a JS module before the React instance was fully set up. Calls to " @@ -481,6 +488,7 @@ public void handleException(Exception e) { } public class ExceptionHandlerWrapper implements JSExceptionHandler { + @Override public void handleException(Exception e) { ReactContext.this.handleException(e); @@ -547,6 +555,12 @@ public boolean isBridgeless() { return null; } + public void executeRuntimeExecutor(ReactRuntimeExecutor runtimeExecutor) { + if (mCatalystInstance != null) { + runtimeExecutor.execute(mCatalystInstance.getRuntimeExecutor()); + } + } + @DeprecatedInNewArchitecture( message = "This method will be deprecated later as part of Stable APIs with bridge removal and not encouraged usage.") diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java index 34f9fd7121b0c7..9a1bdc3eaf2ad5 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/fresco/FrescoModule.java @@ -18,6 +18,7 @@ import com.facebook.react.bridge.ReactApplicationContext; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.RuntimeExecutor; import com.facebook.react.common.ReactConstants; import com.facebook.react.internal.turbomodule.core.interfaces.TurboModule; import com.facebook.react.module.annotations.ReactModule; @@ -129,6 +130,15 @@ public void initialize() { ReactApplicationContext reactContext = getReactApplicationContext(); reactContext.addLifecycleEventListener(this); + + reactContext.executeRuntimeExecutor( + new ReactContext.ReactRuntimeExecutor() { + @Override + public void execute(RuntimeExecutor runtimeExecutor) { + // do something + } + }); + if (!hasBeenInitialized()) { if (mConfig == null) { mConfig = getDefaultConfig(reactContext); 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..806f82bf9d834c 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 @@ -155,6 +155,11 @@ public void handleException(Exception e) { mReactHost.handleHostException(e); } + @Override + public void executeRuntimeExecutor(ReactRuntimeExecutor runtimeExecutor) { + mReactHost.executeRuntimeExecutor(runtimeExecutor); + } + DefaultHardwareBackBtnHandler getDefaultHardwareBackBtnHandler() { return mReactHost.getDefaultBackButtonHandler(); } 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..d27f94f5b276bc 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 @@ -33,6 +33,7 @@ import com.facebook.react.bridge.NativeArray; import com.facebook.react.bridge.NativeModule; import com.facebook.react.bridge.ReactContext; +import com.facebook.react.bridge.ReactContext.ReactRuntimeExecutor; import com.facebook.react.bridge.ReactMarker; import com.facebook.react.bridge.ReactMarkerConstants; import com.facebook.react.bridge.ReactNoCrashBridgeNotAllowedSoftException; @@ -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,23 @@ FabricUIManager getUIManager() { return new ArrayList<>(); } - /* package */ @Nullable + void executeRuntimeExecutor(ReactRuntimeExecutor reactRuntimeExecutor) { + mReactInstanceTaskRef + .get() + .onSuccess( + task -> { + final ReactInstance reactInstance = task.getResult(); + if (reactInstance == null) { + return false; + } + reactRuntimeExecutor.execute(reactInstance.getBufferedRuntimeExecutor()); + return true; + }, + mBGExecutor); + } + + /* package */ + @Nullable T getNativeModule(Class nativeModuleInterface) { if (nativeModuleInterface == UIManagerModule.class) { ReactSoftExceptionLogger.logSoftExceptionVerbose( @@ -699,6 +717,7 @@ public void removeBeforeDestroyListener(@NonNull Function0 onBeforeDestroy } /* package */ interface VeniceThenable { + void then(T t); } @@ -913,6 +932,7 @@ private Task getOrCreateReactInstanceTask() { }); class Result { + final ReactInstance mInstance = instance; final ReactContext mContext = reactContext; final boolean mIsReloading = mReloadTask != null; @@ -1087,6 +1107,7 @@ private void startAttachedSurfaces(String method, ReactInstance reactInstance) { private @Nullable Task mReloadTask = null; private interface ReactInstanceTaskUnwrapper { + @Nullable ReactInstance unwrap(Task task, String stage); } 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();