2018-09-11 22:27:47 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-31 22:31:03 +00:00
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include "ModuleRegistry.h"
|
|
|
|
|
2017-04-07 16:27:05 +00:00
|
|
|
#include <glog/logging.h>
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "NativeModule.h"
|
2016-05-14 00:15:06 +00:00
|
|
|
#include "SystraceSection.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
std::string normalizeName(std::string name) {
|
|
|
|
// TODO mhorowitz #10487027: This is super ugly. We should just
|
|
|
|
// change iOS to emit normalized names, drop the "RK..." from
|
|
|
|
// names hardcoded in Android, and then delete this and the
|
|
|
|
// similar hacks in js.
|
|
|
|
if (name.compare(0, 3, "RCT") == 0) {
|
|
|
|
return name.substr(3);
|
|
|
|
} else if (name.compare(0, 2, "RK") == 0) {
|
|
|
|
return name.substr(2);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-11 13:16:55 +00:00
|
|
|
ModuleRegistry::ModuleRegistry(std::vector<std::unique_ptr<NativeModule>> modules, ModuleNotFoundCallback callback)
|
|
|
|
: modules_{std::move(modules)}, moduleNotFoundCallback_{callback} {}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-06-21 18:56:08 +00:00
|
|
|
void ModuleRegistry::updateModuleNamesFromIndex(size_t index) {
|
|
|
|
for (; index < modules_.size(); index++ ) {
|
|
|
|
std::string name = normalizeName(modules_[index]->getName());
|
|
|
|
modulesByName_[name] = index;
|
|
|
|
}
|
|
|
|
}
|
2017-04-07 16:27:05 +00:00
|
|
|
|
2017-06-21 18:56:08 +00:00
|
|
|
void ModuleRegistry::registerModules(std::vector<std::unique_ptr<NativeModule>> modules) {
|
2018-07-26 22:18:22 +00:00
|
|
|
SystraceSection s_("ModuleRegistry::registerModules");
|
2017-06-21 18:56:08 +00:00
|
|
|
if (modules_.empty() && unknownModules_.empty()) {
|
2017-04-07 16:27:05 +00:00
|
|
|
modules_ = std::move(modules);
|
|
|
|
} else {
|
2017-06-21 18:56:08 +00:00
|
|
|
size_t modulesSize = modules_.size();
|
|
|
|
size_t addModulesSize = modules.size();
|
|
|
|
bool addToNames = !modulesByName_.empty();
|
|
|
|
modules_.reserve(modulesSize + addModulesSize);
|
2017-04-07 16:27:05 +00:00
|
|
|
std::move(modules.begin(), modules.end(), std::back_inserter(modules_));
|
2017-06-21 18:56:08 +00:00
|
|
|
if (!unknownModules_.empty()) {
|
|
|
|
for (size_t index = modulesSize; index < modulesSize + addModulesSize; index++) {
|
|
|
|
std::string name = normalizeName(modules_[index]->getName());
|
|
|
|
auto it = unknownModules_.find(name);
|
|
|
|
if (it != unknownModules_.end()) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
folly::to<std::string>("module ", name, " was required without being registered and is now being registered."));
|
|
|
|
} else if (addToNames) {
|
|
|
|
modulesByName_[name] = index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (addToNames) {
|
|
|
|
updateModuleNamesFromIndex(modulesSize);
|
|
|
|
}
|
2017-04-07 16:27:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
std::vector<std::string> ModuleRegistry::moduleNames() {
|
2018-07-26 22:18:22 +00:00
|
|
|
SystraceSection s_("ModuleRegistry::moduleNames");
|
2016-05-18 19:46:01 +00:00
|
|
|
std::vector<std::string> names;
|
|
|
|
for (size_t i = 0; i < modules_.size(); i++) {
|
|
|
|
std::string name = normalizeName(modules_[i]->getName());
|
|
|
|
modulesByName_[name] = i;
|
|
|
|
names.push_back(std::move(name));
|
|
|
|
}
|
|
|
|
return names;
|
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
folly::Optional<ModuleConfig> ModuleRegistry::getConfig(const std::string& name) {
|
2017-08-30 10:36:23 +00:00
|
|
|
SystraceSection s("ModuleRegistry::getConfig", "module", name);
|
2017-04-07 16:27:05 +00:00
|
|
|
|
|
|
|
// Initialize modulesByName_
|
|
|
|
if (modulesByName_.empty() && !modules_.empty()) {
|
|
|
|
moduleNames();
|
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
auto it = modulesByName_.find(name);
|
2017-08-11 13:16:55 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
if (it == modulesByName_.end()) {
|
2017-08-11 13:16:55 +00:00
|
|
|
if (unknownModules_.find(name) != unknownModules_.end()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!moduleNotFoundCallback_ ||
|
|
|
|
!moduleNotFoundCallback_(name) ||
|
|
|
|
(it = modulesByName_.find(name)) == modulesByName_.end()) {
|
|
|
|
unknownModules_.insert(name);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
2017-08-11 13:16:55 +00:00
|
|
|
size_t index = it->second;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-08-11 13:16:55 +00:00
|
|
|
CHECK(index < modules_.size());
|
|
|
|
NativeModule *module = modules_[index].get();
|
2016-05-18 19:46:01 +00:00
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
// string name, object constants, array methodNames (methodId is index), [array promiseMethodIds], [array syncMethodIds]
|
2016-05-18 19:46:01 +00:00
|
|
|
folly::dynamic config = folly::dynamic::array(name);
|
|
|
|
|
|
|
|
{
|
2018-07-26 22:18:22 +00:00
|
|
|
SystraceSection s_("ModuleRegistry::getConstants", "module", name);
|
2016-09-23 18:12:53 +00:00
|
|
|
config.push_back(module->getConstants());
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
{
|
2018-07-26 22:18:22 +00:00
|
|
|
SystraceSection s_("ModuleRegistry::getMethods", "module", name);
|
2016-05-18 19:46:01 +00:00
|
|
|
std::vector<MethodDescriptor> methods = module->getMethods();
|
|
|
|
|
|
|
|
folly::dynamic methodNames = folly::dynamic::array;
|
2016-09-08 10:59:32 +00:00
|
|
|
folly::dynamic promiseMethodIds = folly::dynamic::array;
|
|
|
|
folly::dynamic syncMethodIds = folly::dynamic::array;
|
2016-05-18 19:46:01 +00:00
|
|
|
|
|
|
|
for (auto& descriptor : methods) {
|
2016-09-08 10:59:32 +00:00
|
|
|
// TODO: #10487027 compare tags instead of doing string comparison?
|
2016-05-18 19:46:01 +00:00
|
|
|
methodNames.push_back(std::move(descriptor.name));
|
2016-09-08 10:59:32 +00:00
|
|
|
if (descriptor.type == "promise") {
|
|
|
|
promiseMethodIds.push_back(methodNames.size() - 1);
|
|
|
|
} else if (descriptor.type == "sync") {
|
|
|
|
syncMethodIds.push_back(methodNames.size() - 1);
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
if (!methodNames.empty()) {
|
|
|
|
config.push_back(std::move(methodNames));
|
2016-09-08 10:59:32 +00:00
|
|
|
if (!promiseMethodIds.empty() || !syncMethodIds.empty()) {
|
|
|
|
config.push_back(std::move(promiseMethodIds));
|
|
|
|
if (!syncMethodIds.empty()) {
|
|
|
|
config.push_back(std::move(syncMethodIds));
|
|
|
|
}
|
2016-05-18 19:46:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-04-08 22:14:32 +00:00
|
|
|
if (config.size() == 2 && config[1].empty()) {
|
2016-05-18 19:46:01 +00:00
|
|
|
// no constants or methods
|
|
|
|
return nullptr;
|
|
|
|
} else {
|
2017-08-11 13:16:55 +00:00
|
|
|
return ModuleConfig{index, config};
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
void ModuleRegistry::callNativeMethod(unsigned int moduleId, unsigned int methodId, folly::dynamic&& params, int callId) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (moduleId >= modules_.size()) {
|
|
|
|
throw std::runtime_error(
|
2017-04-07 16:27:05 +00:00
|
|
|
folly::to<std::string>("moduleId ", moduleId, " out of range [0..", modules_.size(), ")"));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2017-05-13 00:57:12 +00:00
|
|
|
modules_[moduleId]->invoke(methodId, std::move(params), callId);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
MethodCallResult ModuleRegistry::callSerializableNativeHook(unsigned int moduleId, unsigned int methodId, folly::dynamic&& params) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (moduleId >= modules_.size()) {
|
|
|
|
throw std::runtime_error(
|
2017-04-07 16:27:05 +00:00
|
|
|
folly::to<std::string>("moduleId ", moduleId, "out of range [0..", modules_.size(), ")"));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2017-04-25 12:29:45 +00:00
|
|
|
return modules_[moduleId]->callSerializableNativeHook(methodId, std::move(params));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}}
|