-
Notifications
You must be signed in to change notification settings - Fork 24.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate from native CallInvoker to NativeMethodCallInvoker (#37473)
Summary: Pull Request resolved: #37473 ## Context The TurboModule system uses a CallInvoker interface to schedule JavaScript → native, and native → JavaScript calls. ## Problem JavaScript → native and native → JavaScript calls are different. They can have different behaviours/properties. And, in the future, we might want to evolve them differently. e.g: - JavaScript → native **always** have a method name. Native → JavaScript calls don't. - Native → JavaScript can have priorities, since D41492849. JavaScript → native don't. ## Changes Instead of tying both types of calls to the CallInvoker abstraction, this diff creates a **separate** abstraction for Native → JavaScript calls: NativeMethodCallInvoker. This way, we can evolve both abstractions separately over time: - We can evolve the CallInvoker abstraction to suit the needs of JavaScript → native calls. - We can evolve the NativeMethodCallInvoker abstraction to suite the needs of native → JavaScript calls. This ultimately makes TurboModule system more extensible. ## Motivation For the TurboModule interop layer on iOS, React Native needs to execute the "constantsToExport" method on the main queue, when the module requires main queue setup. (implementation: D45924977). The simplest way to implement this behaviour is to introduce a `methodName` to CallInvoker, and customize the legacy module's CallInvoker::invokeSync method, like so: ``` void invokeSync(std::string methodName, std::function<void()> &&work) override { if (requiresMainQueueSetup_ && methodName == "getConstants") { __block auto retainedWork = std::move(work); RCTUnsafeExecuteOnMainQueueSync(^{ retainedWork(); }); return; } work(); } ``` But, customizing CallInvoker to introduce a `methodName` parameter doesn't make sense: Native → JavaScript calls don't necessarily have method names. So, this diff forks CallInvoker into NativeMethodCallInvoker. That way, we can customize NativeMethodCallInvoker to introduce a method name (which does make sense) and resolve this problem. For the full solution, see D45924977. NOTE: Now that NativeMethodCallInvoker is different from CallInvoker, it might make sense to re-name CallInvoker back to JSCallInvoker. Changelog: [Category][Breaking] - Introduce NativeMethodCallInvoker to replace the TurboModule system's native CallInvoker Reviewed By: javache Differential Revision: D45891627 fbshipit-source-id: 39c3f450a290ad396b715288a50858d33ce78441
- Loading branch information
1 parent
d470dee
commit b70f186
Showing
29 changed files
with
275 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
.../src/main/java/com/facebook/react/turbomodule/core/NativeMethodCallInvokerHolderImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.turbomodule.core; | ||
|
||
import com.facebook.jni.HybridData; | ||
import com.facebook.react.turbomodule.core.interfaces.NativeMethodCallInvokerHolder; | ||
import com.facebook.soloader.SoLoader; | ||
|
||
/** | ||
* NativeMethodCallInvokerHolder is created at a different time/place (i.e: in CatalystInstance) | ||
* than TurboModuleManager. Therefore, we need to wrap NativeMethodCallInvokerHolder within a hybrid | ||
* class so that we may pass it from CatalystInstance, through Java, to | ||
* TurboModuleManager::initHybrid. | ||
*/ | ||
public class NativeMethodCallInvokerHolderImpl implements NativeMethodCallInvokerHolder { | ||
private static volatile boolean sIsSoLibraryLoaded; | ||
|
||
private final HybridData mHybridData; | ||
|
||
private NativeMethodCallInvokerHolderImpl(HybridData hd) { | ||
maybeLoadSoLibrary(); | ||
mHybridData = hd; | ||
} | ||
|
||
// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context. | ||
private static synchronized void maybeLoadSoLibrary() { | ||
if (!sIsSoLibraryLoaded) { | ||
SoLoader.loadLibrary("turbomodulejsijni"); | ||
sIsSoLibraryLoaded = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...in/java/com/facebook/react/turbomodule/core/interfaces/NativeMethodCallInvokerHolder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
package com.facebook.react.turbomodule.core.interfaces; | ||
|
||
/** | ||
* This interface represents the opaque Java object that contains a pointer to and instance of | ||
* NativeMethodCallInvoker. | ||
*/ | ||
public interface NativeMethodCallInvokerHolder {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/NativeMethodCallInvokerHolder.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) Meta Platforms, Inc. and affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
#include "NativeMethodCallInvokerHolder.h" | ||
|
||
namespace facebook::react { | ||
|
||
NativeMethodCallInvokerHolder::NativeMethodCallInvokerHolder( | ||
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker) | ||
: _nativeMethodCallInvoker(nativeMethodCallInvoker) {} | ||
|
||
std::shared_ptr<NativeMethodCallInvoker> | ||
NativeMethodCallInvokerHolder::getNativeMethodCallInvoker() { | ||
return _nativeMethodCallInvoker; | ||
} | ||
|
||
void NativeMethodCallInvokerHolder::registerNatives() {} | ||
|
||
} // namespace facebook::react |
Oops, something went wrong.