forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
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 (facebook#…
…37473) Summary: Pull Request resolved: facebook#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. Differential Revision: D45891627 fbshipit-source-id: 14a93431bc4674bf0a2eef1738bb77998a99ad07
- Loading branch information
1 parent
a41d482
commit 8322be5
Showing
28 changed files
with
269 additions
and
118 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
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 |
32 changes: 32 additions & 0 deletions
32
...e/ReactAndroid/src/main/jni/react/turbomodule/ReactCommon/NativeMethodCallInvokerHolder.h
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,32 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <ReactCommon/CallInvoker.h> | ||
#include <fbjni/fbjni.h> | ||
#include <memory> | ||
|
||
namespace facebook::react { | ||
|
||
class NativeMethodCallInvokerHolder | ||
: public jni::HybridClass<NativeMethodCallInvokerHolder> { | ||
public: | ||
static auto constexpr kJavaDescriptor = | ||
"Lcom/facebook/react/turbomodule/core/NativeMethodCallInvokerHolderImpl;"; | ||
|
||
static void registerNatives(); | ||
std::shared_ptr<NativeMethodCallInvoker> getNativeMethodCallInvoker(); | ||
|
||
private: | ||
friend HybridBase; | ||
NativeMethodCallInvokerHolder( | ||
std::shared_ptr<NativeMethodCallInvoker> nativeMethodCallInvoker); | ||
std::shared_ptr<NativeMethodCallInvoker> _nativeMethodCallInvoker; | ||
}; | ||
|
||
} // namespace facebook::react |
Oops, something went wrong.