Skip to content

Commit

Permalink
Patch REACT_MACRO_NOREG to 0.71 (#11865)
Browse files Browse the repository at this point in the history
* Add REACT_MODULE_NOREG which is REACT_MODULE but without auto registration (#11849)

* #define REACT_MODULE_NOREG

* yarn format

* Add unit test

* ...

* ...

* Change files

* Delete react-native-windows-ccec3086-548e-40b6-a95d-41c1ca26caff.json

* Change files
  • Loading branch information
ZihanChen-MSFT committed Jul 10, 2023
1 parent df0b218 commit 6e285ab
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "patch",
"comment": "Add REACT_MODULE_NOREG which is REACT_MODULE but without auto registration (#11849)",
"packageName": "react-native-windows",
"email": "53799235+ZihanChen-MSFT@users.noreply.github.com",
"dependentChangeType": "patch"
}
12 changes: 12 additions & 0 deletions vnext/Microsoft.ReactNative.Cxx.UnitTests/NativeModuleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,18 @@ TEST_CLASS (NativeModuleTest) {
m_module = React::ReactNonAbiValue<SimpleNativeModule>::GetPtrUnsafe(m_moduleObject);
}

TEST_METHOD(EnsureReactModuleAutoRegistered) {
bool registered = false;
auto current = React::ModuleRegistration::Head();
while (current) {
if (wcscmp(current->ModuleName(), L"SimpleNativeModule") == 0) {
registered = true;
}
current = current->Next();
}
TestCheck(registered);
}

TEST_METHOD(TestMethodCall_Add) {
m_builderMock.Call1(L"Add", std::function<void(int)>([](int result) noexcept { TestCheck(result == 8); }), 3, 5);
TestCheck(m_builderMock.IsResolveCallbackCalled());
Expand Down
22 changes: 22 additions & 0 deletions vnext/Microsoft.ReactNative.Cxx/ModuleRegistration.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,28 @@
INTERNAL_REACT_RECOMPOSER_4( \
(__VA_ARGS__, INTERNAL_REACT_MODULE_3_ARGS, INTERNAL_REACT_MODULE_2_ARGS, INTERNAL_REACT_MODULE_1_ARG, ))

// Another version of REACT_MODULE but does not do auto registration
#define INTERNAL_REACT_MODULE_NOREG_3_ARGS(moduleStruct, moduleName, eventEmitterName) \
struct moduleStruct; \
template <class TRegistry> \
constexpr void GetReactModuleInfo(moduleStruct *, TRegistry &registry) noexcept { \
registry.RegisterModule( \
moduleName, eventEmitterName, winrt::Microsoft::ReactNative::ReactAttributeId<__COUNTER__>{}); \
}

#define INTERNAL_REACT_MODULE_NOREG_2_ARGS(moduleStruct, moduleName) \
INTERNAL_REACT_MODULE_NOREG_3_ARGS(moduleStruct, moduleName, L"")

#define INTERNAL_REACT_MODULE_NOREG_1_ARG(moduleStruct) \
INTERNAL_REACT_MODULE_NOREG_2_ARGS(moduleStruct, L## #moduleStruct)

#define INTERNAL_REACT_MODULE_NOREG(...) \
INTERNAL_REACT_RECOMPOSER_4( \
(__VA_ARGS__, \
INTERNAL_REACT_MODULE_NOREG_3_ARGS, \
INTERNAL_REACT_MODULE_NOREG_2_ARGS, \
INTERNAL_REACT_MODULE_NOREG_1_ARG, ))

// Provide meta data information about struct member.
// For each member with a 'custom attribute' macro we create a static method to provide meta data.
// The member Id is generated as a ReactMemberId<__COUNTER__> type.
Expand Down
5 changes: 5 additions & 0 deletions vnext/Microsoft.ReactNative.Cxx/NativeModules.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
#define REACT_MODULE(/* moduleStruct, [opt] moduleName, [opt] eventEmitterName */...) \
INTERNAL_REACT_MODULE(__VA_ARGS__)(__VA_ARGS__)

// REACT_MODULE_NOREG is REACT_MODULE without auto registration
// they have the same arguments
#define REACT_MODULE_NOREG(/* moduleStruct, [opt] moduleName, [opt] eventEmitterName */...) \
INTERNAL_REACT_MODULE_NOREG(__VA_ARGS__)(__VA_ARGS__)

// REACT_INIT(method)
// Arguments:
// - method (required) - the method name the macro is attached to.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ struct CppTurboModuleSpec : TurboModuleSpec {
}
};

REACT_MODULE(CppTurboModule)
REACT_MODULE_NOREG(CppTurboModule)
struct CppTurboModule {
using ModuleSpec = CppTurboModuleSpec;

Expand Down Expand Up @@ -558,6 +558,18 @@ struct CppTurboModulePackageProvider : winrt::implements<CppTurboModulePackagePr
} // namespace

TEST_CLASS (TurboModuleTests) {
TEST_METHOD(EnsureReactModuleNotAutoRegistered) {
bool registered = false;
auto current = ModuleRegistration::Head();
while (current) {
if (wcscmp(current->ModuleName(), L"CppTurboModule") == 0) {
registered = true;
}
current = current->Next();
}
TestCheck(!registered);
}

TEST_METHOD(ExecuteSampleTurboModule) {
TestEventService::Initialize();

Expand Down

0 comments on commit 6e285ab

Please sign in to comment.