Skip to content

Commit

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

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

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

From [PR#42758](#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
  • Loading branch information
philIip authored and facebook-github-bot committed Feb 9, 2024
1 parent 40bb425 commit d7dce97
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,6 +38,7 @@
* CatalystInstance}
*/
public class ReactContext extends ContextWrapper {

@DoNotStrip
public interface RCTDeviceEventEmitter extends JavaScriptModule {
void emit(@NonNull String eventName, @Nullable Object data);
Expand Down Expand Up @@ -206,6 +209,20 @@ public <T extends NativeModule> T getNativeModule(Class<T> 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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -149,6 +152,16 @@ 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
@FrameworkAPI
@UnstableReactNativeAPI
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,19 @@ <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();
}
ReactSoftExceptionLogger.logSoftException(
TAG,
new ReactNoCrashSoftException("Tried to get runtime executor while instance is not ready"));
return null;
}

/* package */
DefaultHardwareBackBtnHandler getDefaultBackButtonHandler() {
return () -> {
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 d7dce97

Please sign in to comment.