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
3 changes: 2 additions & 1 deletion packages/react-native/React/Base/RCTBridgeProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
bundleManager:(RCTBundleManager *)bundleManager
callableJSModules:(RCTCallableJSModules *)callableJSModules
dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread
registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId NS_DESIGNATED_INITIALIZER;
registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId
runtime:(void *)runtime NS_DESIGNATED_INITIALIZER;

- (NSMethodSignature *)methodSignatureForSelector:(SEL)sel;
- (void)forwardInvocation:(NSInvocation *)invocation;
Expand Down
23 changes: 13 additions & 10 deletions packages/react-native/React/Base/RCTBridgeProxy.mm
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ @implementation RCTBridgeProxy {
RCTCallableJSModules *_callableJSModules;
void (^_dispatchToJSThread)(dispatch_block_t);
void (^_registerSegmentWithId)(NSNumber *, NSString *);
void *_runtime;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not jsi::Runtime * for clarity?

}

- (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry
Expand All @@ -36,15 +37,17 @@ - (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry
callableJSModules:(RCTCallableJSModules *)callableJSModules
dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread
registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId
runtime:(void *)runtime
{
self = [super self];
if (self) {
self->_uiManagerProxy = [[RCTUIManagerProxy alloc] initWithViewRegistry:viewRegistry];
self->_moduleRegistry = moduleRegistry;
self->_bundleManager = bundleManager;
self->_callableJSModules = callableJSModules;
self->_dispatchToJSThread = dispatchToJSThread;
self->_registerSegmentWithId = registerSegmentWithId;
_uiManagerProxy = [[RCTUIManagerProxy alloc] initWithViewRegistry:viewRegistry];
_moduleRegistry = moduleRegistry;
_bundleManager = bundleManager;
_callableJSModules = callableJSModules;
_dispatchToJSThread = dispatchToJSThread;
_registerSegmentWithId = registerSegmentWithId;
_runtime = runtime;
}
return self;
}
Expand Down Expand Up @@ -75,10 +78,10 @@ - (Class)executorClass
* Used By:
* - RCTBlobCollector
*/
- (jsi::Runtime *)runtime
- (void *)runtime
{
[self logWarning:@"This method is unsupported. Returning nullptr." cmd:_cmd];
return nullptr;
[self logWarning:@"Please migrate to C++ TurboModule or RuntimeExecutor." cmd:_cmd];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any docs for RuntimeExecutor? How would one create a native C++ module that has access to jsi::Runtime and gets auto-linked/initialized properly on Android and iOS?

return _runtime;
}

/**
Expand Down Expand Up @@ -162,7 +165,7 @@ - (void)enqueueJSCall:(NSString *)module

- (void)registerSegmentWithId:(NSUInteger)segmentId path:(NSString *)path
{
self->_registerSegmentWithId(@(segmentId), path);
_registerSegmentWithId(@(segmentId), path);
}

- (id<RCTBridgeDelegate>)delegate
Expand Down
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
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ - (void)_start
if (strongSelf && strongSelf->_valid) {
[strongSelf registerSegmentWithId:segmentId path:path];
}
}];
}
runtime:_reactInstance->getJavaScriptContext()];
[RCTBridge setCurrentBridge:(RCTBridge *)bridgeProxy];

// Set up TurboModules
Expand Down