2018-05-31 22:31:03 +00:00
|
|
|
// Copyright (c) 2004-present, Facebook, Inc.
|
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2017-09-21 15:34:44 +00:00
|
|
|
|
|
|
|
#include "RAMBundleRegistry.h"
|
|
|
|
|
2017-11-08 11:50:36 +00:00
|
|
|
#include <folly/Memory.h>
|
2018-04-17 18:54:47 +00:00
|
|
|
#include <folly/String.h>
|
2017-11-08 11:50:36 +00:00
|
|
|
|
2017-10-04 22:03:52 +00:00
|
|
|
#include <libgen.h>
|
|
|
|
|
2017-09-21 15:34:44 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID;
|
|
|
|
|
2018-04-17 18:54:49 +00:00
|
|
|
std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::singleBundleRegistry(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> mainBundle) {
|
|
|
|
return folly::make_unique<RAMBundleRegistry>(std::move(mainBundle));
|
2017-11-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:54:49 +00:00
|
|
|
std::unique_ptr<RAMBundleRegistry> RAMBundleRegistry::multipleBundlesRegistry(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> mainBundle,
|
|
|
|
std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory) {
|
|
|
|
return folly::make_unique<RAMBundleRegistry>(
|
|
|
|
std::move(mainBundle), std::move(factory));
|
2017-11-08 11:50:36 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:54:49 +00:00
|
|
|
RAMBundleRegistry::RAMBundleRegistry(
|
|
|
|
std::unique_ptr<JSModulesUnbundle> mainBundle,
|
|
|
|
std::function<std::unique_ptr<JSModulesUnbundle>(std::string)> factory):
|
|
|
|
m_factory(std::move(factory)) {
|
2017-09-21 15:34:44 +00:00
|
|
|
m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle));
|
|
|
|
}
|
|
|
|
|
2018-04-17 18:54:49 +00:00
|
|
|
void RAMBundleRegistry::registerBundle(
|
|
|
|
uint32_t bundleId, std::string bundlePath) {
|
|
|
|
m_bundlePaths.emplace(bundleId, std::move(bundlePath));
|
2017-11-09 19:55:37 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:54:49 +00:00
|
|
|
JSModulesUnbundle::Module RAMBundleRegistry::getModule(
|
|
|
|
uint32_t bundleId, uint32_t moduleId) {
|
2017-09-22 16:56:50 +00:00
|
|
|
if (m_bundles.find(bundleId) == m_bundles.end()) {
|
2017-11-08 11:50:36 +00:00
|
|
|
if (!m_factory) {
|
2018-04-17 18:54:49 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"You need to register factory function in order to "
|
|
|
|
"support multiple RAM bundles."
|
|
|
|
);
|
2017-11-08 11:50:36 +00:00
|
|
|
}
|
2017-11-09 19:55:37 +00:00
|
|
|
|
|
|
|
auto bundlePath = m_bundlePaths.find(bundleId);
|
|
|
|
if (bundlePath == m_bundlePaths.end()) {
|
2018-04-17 18:54:49 +00:00
|
|
|
throw std::runtime_error(
|
|
|
|
"In order to fetch RAM bundle from the registry, its file "
|
|
|
|
"path needs to be registered first."
|
|
|
|
);
|
2017-11-09 19:55:37 +00:00
|
|
|
}
|
|
|
|
m_bundles.emplace(bundleId, m_factory(bundlePath->second));
|
2017-09-22 16:56:50 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:54:47 +00:00
|
|
|
auto module = getBundle(bundleId)->getModule(moduleId);
|
|
|
|
if (bundleId == MAIN_BUNDLE_ID) {
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
folly::to<std::string>("seg-", bundleId, '_', std::move(module.name)),
|
|
|
|
std::move(module.code),
|
|
|
|
};
|
2017-09-21 15:34:44 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 18:54:49 +00:00
|
|
|
JSModulesUnbundle* RAMBundleRegistry::getBundle(uint32_t bundleId) const {
|
2017-09-21 15:34:44 +00:00
|
|
|
return m_bundles.at(bundleId).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|