From f3dfd616f4eb9465585aeec1526592dcdad9871d Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Sat, 8 Apr 2017 15:14:32 -0700 Subject: [PATCH] Allow lazy Cxx modules to not initialize Reviewed By: mhorowitz Differential Revision: D4851596 fbshipit-source-id: cf5f5c51b9aaa0da96f7ab6fba1277b72c988400 --- React/CxxModule/RCTCxxUtils.mm | 6 ------ ReactCommon/cxxreact/CxxNativeModule.cpp | 14 +++++++++++--- ReactCommon/cxxreact/ModuleRegistry.cpp | 2 +- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/React/CxxModule/RCTCxxUtils.mm b/React/CxxModule/RCTCxxUtils.mm index 331e0bfaf5..73951be812 100644 --- a/React/CxxModule/RCTCxxUtils.mm +++ b/React/CxxModule/RCTCxxUtils.mm @@ -46,12 +46,6 @@ std::vector> createNativeModules(NSArray> nativeModules; for (RCTModuleData *moduleData in modules) { 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( instance, [moduleData.name UTF8String], diff --git a/ReactCommon/cxxreact/CxxNativeModule.cpp b/ReactCommon/cxxreact/CxxNativeModule.cpp index 40d37f1a0e..52a1469fe9 100644 --- a/ReactCommon/cxxreact/CxxNativeModule.cpp +++ b/ReactCommon/cxxreact/CxxNativeModule.cpp @@ -64,6 +64,10 @@ std::vector CxxNativeModule::getMethods() { folly::dynamic CxxNativeModule::getConstants() { lazyInit(); + if (!module_) { + return nullptr; + } + folly::dynamic constants = folly::dynamic::object(); for (auto& pair : module_->getConstants()) { constants.insert(std::move(pair.first), std::move(pair.second)); @@ -161,13 +165,17 @@ MethodCallResult CxxNativeModule::callSerializableNativeHook( } void CxxNativeModule::lazyInit() { - if (module_) { + if (module_ || !provider_) { return; } + // TODO 17216751: providers should never return null modules module_ = provider_(); - methods_ = module_->getMethods(); - module_->setInstance(instance_); + provider_ = nullptr; + if (module_) { + methods_ = module_->getMethods(); + module_->setInstance(instance_); + } } } diff --git a/ReactCommon/cxxreact/ModuleRegistry.cpp b/ReactCommon/cxxreact/ModuleRegistry.cpp index 4a7dcb8014..7fda3c75cc 100644 --- a/ReactCommon/cxxreact/ModuleRegistry.cpp +++ b/ReactCommon/cxxreact/ModuleRegistry.cpp @@ -105,7 +105,7 @@ folly::Optional ModuleRegistry::getConfig(const std::string& name) } } - if (config.size() == 1) { + if (config.size() == 2 && config[1].empty()) { // no constants or methods return nullptr; } else {