Skip to content

Commit

Permalink
introduce native api to access RuntimeExecutor (facebook#42882)
Browse files Browse the repository at this point in the history
Summary:

Changelog: [Android][Added] - introduce native api to access RuntimeExecutor

This is the android equivalent of [PR#42758](facebook#42758).

From [PR#42758](facebook#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

Differential Revision: D53461821
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 8, 2024
1 parent e37da1e commit ff47c1b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,15 @@ public <T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface
return mCatalystInstance.getNativeModule(nativeModuleInterface);
}

/** @return the RuntimeExecutor, a thread-safe handler for accessing the runtime. */
@Nullable
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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.config.ReactFeatureFlags;
Expand Down Expand Up @@ -149,6 +150,14 @@ public Collection<NativeModule> getNativeModules() {
public @Nullable <T extends NativeModule> T getNativeModule(Class<T> 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
public @Nullable RuntimeExecutor getRuntimeExecutor() {
return mReactHost.getRuntimeExecutor();
}

@Override
public void handleException(Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -567,7 +569,8 @@ FabricUIManager getUIManager() {
return new ArrayList<>();
}

/* package */ @Nullable
/* package */
@Nullable
<T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface) {
if (nativeModuleInterface == UIManagerModule.class) {
ReactSoftExceptionLogger.logSoftExceptionVerbose(
Expand All @@ -583,6 +586,16 @@ <T extends NativeModule> T getNativeModule(Class<T> nativeModuleInterface) {
return null;
}

/* package */
@Nullable
RuntimeExecutor getRuntimeExecutor() {
final ReactInstance reactInstance = mReactInstanceTaskRef.get().getResult();
if (reactInstance != null) {
return reactInstance.getBufferedRuntimeExecutor();
}
return null;
}

/* package */
DefaultHardwareBackBtnHandler getDefaultBackButtonHandler() {
return () -> {
Expand Down Expand Up @@ -699,6 +712,7 @@ public void removeBeforeDestroyListener(@NonNull Function0<Unit> onBeforeDestroy
}

/* package */ interface VeniceThenable<T> {

void then(T t);
}

Expand Down Expand Up @@ -913,6 +927,7 @@ private Task<ReactInstance> getOrCreateReactInstanceTask() {
});

class Result {

final ReactInstance mInstance = instance;
final ReactContext mContext = reactContext;
final boolean mIsReloading = mReloadTask != null;
Expand Down Expand Up @@ -1087,6 +1102,7 @@ private void startAttachedSurfaces(String method, ReactInstance reactInstance) {
private @Nullable Task<ReactInstance> mReloadTask = null;

private interface ReactInstanceTaskUnwrapper {

@Nullable
ReactInstance unwrap(Task<ReactInstance> task, String stage);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ private native HybridData initHybrid(

private native RuntimeExecutor getUnbufferedRuntimeExecutor();

private native RuntimeExecutor getBufferedRuntimeExecutor();
/* package */ native RuntimeExecutor getBufferedRuntimeExecutor();

private native RuntimeScheduler getRuntimeScheduler();

Expand Down

0 comments on commit ff47c1b

Please sign in to comment.