Allow lazy Cxx modules to not initialize

Reviewed By: mhorowitz

Differential Revision: D4851596

fbshipit-source-id: cf5f5c51b9aaa0da96f7ab6fba1277b72c988400
This commit is contained in:
Pieter De Baets 2017-04-08 15:14:32 -07:00 committed by Facebook Github Bot
parent e67b8fe761
commit f3dfd616f4
3 changed files with 12 additions and 10 deletions

View File

@ -46,12 +46,6 @@ std::vector<std::unique_ptr<NativeModule>> createNativeModules(NSArray<RCTModule
std::vector<std::unique_ptr<NativeModule>> nativeModules; std::vector<std::unique_ptr<NativeModule>> nativeModules;
for (RCTModuleData *moduleData in modules) { for (RCTModuleData *moduleData in modules) {
if ([moduleData.moduleClass isSubclassOfClass:[RCTCxxModule class]]) { if ([moduleData.moduleClass isSubclassOfClass:[RCTCxxModule class]]) {
// If a module does not support automatic instantiation, and
// wasn't provided as an extra module, it may not have an
// instance. If so, skip it.
if (![moduleData hasInstance]) {
continue;
}
nativeModules.emplace_back(std::make_unique<CxxNativeModule>( nativeModules.emplace_back(std::make_unique<CxxNativeModule>(
instance, instance,
[moduleData.name UTF8String], [moduleData.name UTF8String],

View File

@ -64,6 +64,10 @@ std::vector<MethodDescriptor> CxxNativeModule::getMethods() {
folly::dynamic CxxNativeModule::getConstants() { folly::dynamic CxxNativeModule::getConstants() {
lazyInit(); lazyInit();
if (!module_) {
return nullptr;
}
folly::dynamic constants = folly::dynamic::object(); folly::dynamic constants = folly::dynamic::object();
for (auto& pair : module_->getConstants()) { for (auto& pair : module_->getConstants()) {
constants.insert(std::move(pair.first), std::move(pair.second)); constants.insert(std::move(pair.first), std::move(pair.second));
@ -161,13 +165,17 @@ MethodCallResult CxxNativeModule::callSerializableNativeHook(
} }
void CxxNativeModule::lazyInit() { void CxxNativeModule::lazyInit() {
if (module_) { if (module_ || !provider_) {
return; return;
} }
// TODO 17216751: providers should never return null modules
module_ = provider_(); module_ = provider_();
methods_ = module_->getMethods(); provider_ = nullptr;
module_->setInstance(instance_); if (module_) {
methods_ = module_->getMethods();
module_->setInstance(instance_);
}
} }
} }

View File

@ -105,7 +105,7 @@ folly::Optional<ModuleConfig> ModuleRegistry::getConfig(const std::string& name)
} }
} }
if (config.size() == 1) { if (config.size() == 2 && config[1].empty()) {
// no constants or methods // no constants or methods
return nullptr; return nullptr;
} else { } else {