Support ModuleHolder-based lazy init of C++ modules with C++ bridge on android

Reviewed By: AaaChiuuu

Differential Revision: D4614479

fbshipit-source-id: 109ac34b8688f0113675e4a4479d1ddcc6169ed4
This commit is contained in:
Marc Horowitz
2017-03-14 15:28:53 -07:00
committed by Facebook Github Bot
parent 6410e256c5
commit e622d51e20
20 changed files with 174 additions and 52 deletions

View File

@@ -47,18 +47,19 @@ CxxModule::Callback convertCallback(
}
CxxNativeModule::CxxNativeModule(std::weak_ptr<Instance> instance,
std::unique_ptr<CxxModule> module)
std::string name,
CxxModule::Provider provider)
: instance_(instance)
, module_(std::move(module))
, methods_(module_->getMethods()) {
module_->setInstance(instance);
}
, name_(std::move(name))
, provider_(provider) {}
std::string CxxNativeModule::getName() {
return module_->getName();
return name_;
}
std::vector<MethodDescriptor> CxxNativeModule::getMethods() {
lazyInit();
std::vector<MethodDescriptor> descs;
for (auto& method : methods_) {
assert(method.func || method.syncFunc);
@@ -68,6 +69,8 @@ std::vector<MethodDescriptor> CxxNativeModule::getMethods() {
}
folly::dynamic CxxNativeModule::getConstants() {
lazyInit();
folly::dynamic constants = folly::dynamic::object();
for (auto& pair : module_->getConstants()) {
constants.insert(std::move(pair.first), std::move(pair.second));
@@ -168,5 +171,15 @@ MethodCallResult CxxNativeModule::callSerializableNativeHook(
return method.syncFunc(std::move(args));
}
void CxxNativeModule::lazyInit() {
if (module_) {
return;
}
module_ = provider_();
methods_ = module_->getMethods();
module_->setInstance(instance_);
}
}
}