Warn whenever CxxNativeModules are used

Summary:
After this diff, when ReactFeatureFlags.warnOnLegacyNativeModuleSystemUse is enabled, the legacy NativeModule infra will log soft exceptions whenever legacy NativeModules are accessed/used.

Changelog: [Internal]

Reviewed By: p-sun

Differential Revision: D30272695

fbshipit-source-id: 7111402c1d8b883a600dcb4559e9ff1d56447070
This commit is contained in:
Ramanpreet Nara
2021-08-13 13:53:44 -07:00
committed by Facebook GitHub Bot
parent b7fd68e611
commit f536f82e12
5 changed files with 54 additions and 0 deletions
+24
View File
@@ -54,6 +54,12 @@ CxxModule::Callback convertCallback(
} // namespace
WarnOnUsageLogger CxxNativeModule::warnOnUsageLogger_ = nullptr;
void CxxNativeModule::setWarnOnUsageLogger(WarnOnUsageLogger logger) {
warnOnUsageLogger_ = logger;
}
std::string CxxNativeModule::getName() {
return name_;
}
@@ -87,6 +93,12 @@ folly::dynamic CxxNativeModule::getConstants() {
return nullptr;
}
if (warnOnUsageLogger_) {
warnOnUsageLogger_(
"Calling getConstants() on Cxx NativeModule (name = \"" + getName() +
"\").");
}
folly::dynamic constants = folly::dynamic::object();
for (auto &pair : module_->getConstants()) {
constants.insert(std::move(pair.first), std::move(pair.second));
@@ -121,6 +133,12 @@ void CxxNativeModule::invoke(
"Method ", method.name, " is synchronous but invoked asynchronously"));
}
if (warnOnUsageLogger_) {
warnOnUsageLogger_(
"Calling " + method.name + "() on Cxx NativeModule (name = \"" +
getName() + "\").");
}
if (params.size() < method.callbacks) {
throw std::invalid_argument(folly::to<std::string>(
"Expected ",
@@ -204,6 +222,12 @@ MethodCallResult CxxNativeModule::callSerializableNativeHook(
"Method ", method.name, " is asynchronous but invoked synchronously"));
}
if (warnOnUsageLogger_) {
warnOnUsageLogger_(
"Calling " + method.name + "() on Cxx NativeModule (name = \"" +
getName() + "\").");
}
return method.syncFunc(std::move(args));
}