Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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