From 5870dc9059e5daf98f4cfb61a7288cb8e79e8d70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Trys=C5=82a?= Date: Tue, 4 Jun 2019 12:48:53 +0200 Subject: [PATCH] remove RAMBundleRegistry and JSModulesUnbundle --- .../jni/react/jni/CatalystInstanceImpl.cpp | 1 - ReactCommon/cxxreact/Instance.h | 1 - ReactCommon/cxxreact/JSDeltaBundleClient.cpp | 165 +++++++++--------- ReactCommon/cxxreact/JSDeltaBundleClient.h | 56 +++--- ReactCommon/cxxreact/JSExecutor.cpp | 25 --- ReactCommon/cxxreact/JSExecutor.h | 5 - ReactCommon/cxxreact/JSModulesUnbundle.h | 45 ----- ReactCommon/cxxreact/NativeToJsBridge.cpp | 1 - ReactCommon/cxxreact/NativeToJsBridge.h | 1 - ReactCommon/cxxreact/RAMBundleRegistry.cpp | 75 -------- ReactCommon/cxxreact/RAMBundleRegistry.h | 53 ------ .../tests/JSDeltaBundleClientTest.cpp | 14 +- .../jsiexecutor/jsireact/JSIExecutor.h | 1 - 13 files changed, 118 insertions(+), 325 deletions(-) delete mode 100644 ReactCommon/cxxreact/JSExecutor.cpp delete mode 100644 ReactCommon/cxxreact/JSModulesUnbundle.h delete mode 100644 ReactCommon/cxxreact/RAMBundleRegistry.cpp delete mode 100644 ReactCommon/cxxreact/RAMBundleRegistry.h diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp index 56c22ca37afe..d1be649806e6 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp @@ -17,7 +17,6 @@ #include #include #include -// #include #include #include #include diff --git a/ReactCommon/cxxreact/Instance.h b/ReactCommon/cxxreact/Instance.h index 0f5e46abfaae..bda26dcdc157 100644 --- a/ReactCommon/cxxreact/Instance.h +++ b/ReactCommon/cxxreact/Instance.h @@ -26,7 +26,6 @@ class JSBigString; class JSExecutorFactory; class MessageQueueThread; class ModuleRegistry; -// class RAMBundleRegistry; struct InstanceCallback { virtual ~InstanceCallback() {} diff --git a/ReactCommon/cxxreact/JSDeltaBundleClient.cpp b/ReactCommon/cxxreact/JSDeltaBundleClient.cpp index 0c48785d0371..2957241444fd 100644 --- a/ReactCommon/cxxreact/JSDeltaBundleClient.cpp +++ b/ReactCommon/cxxreact/JSDeltaBundleClient.cpp @@ -12,88 +12,89 @@ namespace facebook { namespace react { -namespace { - std::string startupCode(const folly::dynamic *pre, const folly::dynamic *post) { - std::ostringstream startupCode; - - for (auto section : {pre, post}) { - if (section != nullptr) { - startupCode << section->getString() << '\n'; - } - } - - return startupCode.str(); - } -} // namespace - -void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) { - for (const folly::dynamic pair : *modules) { - auto id = pair[0].getInt(); - auto module = pair[1]; - modules_[id] = std::move(module.getString()); - } -} - -void JSDeltaBundleClient::patch(const folly::dynamic& delta) { - auto const base = delta.get_ptr("base"); - - if (base != nullptr && base->asBool()) { - clear(); - - auto const pre = delta.get_ptr("pre"); - auto const post = delta.get_ptr("post"); - - startupCode_ = startupCode(pre, post); - - const folly::dynamic *modules = delta.get_ptr("modules"); - if (modules != nullptr) { - patchModules(modules); - } - } else { - const folly::dynamic *deleted = delta.get_ptr("deleted"); - if (deleted != nullptr) { - for (const folly::dynamic id : *deleted) { - modules_.erase(id.getInt()); - } - } - - // TODO T37123645 This is deprecated but necessary in order to support older - // versions of the Metro server. - const folly::dynamic *modules = delta.get_ptr("modules"); - if (modules != nullptr) { - patchModules(modules); - } - - const folly::dynamic *added = delta.get_ptr("added"); - if (added != nullptr) { - patchModules(added); - } - - const folly::dynamic *modified = delta.get_ptr("modified"); - if (modified != nullptr) { - patchModules(modified); - } - } - -} - -JSModulesUnbundle::Module JSDeltaBundleClient::getModule(uint32_t moduleId) const { - auto search = modules_.find(moduleId); - if (search != modules_.end()) { - return {folly::to(search->first, ".js"), search->second}; - } - - throw JSModulesUnbundle::ModuleNotFound(moduleId); -} - -std::unique_ptr JSDeltaBundleClient::getStartupCode() const { - return folly::make_unique(startupCode_); -} - -void JSDeltaBundleClient::clear() { - modules_.clear(); - startupCode_.clear(); -} +// TODO: refactor +// namespace { +// std::string startupCode(const folly::dynamic *pre, const folly::dynamic *post) { +// std::ostringstream startupCode; + +// for (auto section : {pre, post}) { +// if (section != nullptr) { +// startupCode << section->getString() << '\n'; +// } +// } + +// return startupCode.str(); +// } +// } // namespace + +// void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) { +// for (const folly::dynamic pair : *modules) { +// auto id = pair[0].getInt(); +// auto module = pair[1]; +// modules_[id] = std::move(module.getString()); +// } +// } + +// void JSDeltaBundleClient::patch(const folly::dynamic& delta) { +// auto const base = delta.get_ptr("base"); + +// if (base != nullptr && base->asBool()) { +// clear(); + +// auto const pre = delta.get_ptr("pre"); +// auto const post = delta.get_ptr("post"); + +// startupCode_ = startupCode(pre, post); + +// const folly::dynamic *modules = delta.get_ptr("modules"); +// if (modules != nullptr) { +// patchModules(modules); +// } +// } else { +// const folly::dynamic *deleted = delta.get_ptr("deleted"); +// if (deleted != nullptr) { +// for (const folly::dynamic id : *deleted) { +// modules_.erase(id.getInt()); +// } +// } + +// // TODO T37123645 This is deprecated but necessary in order to support older +// // versions of the Metro server. +// const folly::dynamic *modules = delta.get_ptr("modules"); +// if (modules != nullptr) { +// patchModules(modules); +// } + +// const folly::dynamic *added = delta.get_ptr("added"); +// if (added != nullptr) { +// patchModules(added); +// } + +// const folly::dynamic *modified = delta.get_ptr("modified"); +// if (modified != nullptr) { +// patchModules(modified); +// } +// } + +// } + +// RAMBundle::Module JSDeltaBundleClient::getModule(uint32_t moduleId) const { +// auto search = modules_.find(moduleId); +// if (search != modules_.end()) { +// return {folly::to(search->first, ".js"), search->second}; +// } + +// throw RAMBundle::ModuleNotFound(moduleId); +// } + +// std::unique_ptr JSDeltaBundleClient::getStartupCode() const { +// return folly::make_unique(startupCode_); +// } + +// void JSDeltaBundleClient::clear() { +// modules_.clear(); +// startupCode_.clear(); +// } } // namespace react } // namespace facebook diff --git a/ReactCommon/cxxreact/JSDeltaBundleClient.h b/ReactCommon/cxxreact/JSDeltaBundleClient.h index 5ee2aba9ca7f..14214e50cdb2 100644 --- a/ReactCommon/cxxreact/JSDeltaBundleClient.h +++ b/ReactCommon/cxxreact/JSDeltaBundleClient.h @@ -9,39 +9,39 @@ #include #include #include - -#include -#include #include +#include "JSBigString.h" +#include "RAMBundle.h" namespace facebook { namespace react { -class JSDeltaBundleClient { -public: - void patch(const folly::dynamic& delta); - JSModulesUnbundle::Module getModule(uint32_t moduleId) const; - std::unique_ptr getStartupCode() const; - void clear(); - -private: - std::unordered_map modules_; - std::string startupCode_; - - void patchModules(const folly::dynamic *delta); -}; - -class JSDeltaBundleClientRAMBundle : public JSModulesUnbundle { -public: - JSDeltaBundleClientRAMBundle( - std::shared_ptr client) : client_(client) {} - - Module getModule(uint32_t moduleId) const override { - return client_->getModule(moduleId); - } -private: - const std::shared_ptr client_; -}; +// TODO: refactor +// class JSDeltaBundleClient { +// public: +// void patch(const folly::dynamic& delta); +// RAMBundle::Module getModule(uint32_t moduleId) const; +// std::unique_ptr getStartupCode() const; +// void clear(); + +// private: +// std::unordered_map modules_; +// std::string startupCode_; + +// void patchModules(const folly::dynamic *delta); +// }; + +// class JSDeltaBundleClientRAMBundle : public JSModulesUnbundle { +// public: +// JSDeltaBundleClientRAMBundle( +// std::shared_ptr client) : client_(client) {} + +// RAMBundle::Module getModule(uint32_t moduleId) const override { +// return client_->getModule(moduleId); +// } +// private: +// const std::shared_ptr client_; +// }; } // namespace react } // namespace facebook diff --git a/ReactCommon/cxxreact/JSExecutor.cpp b/ReactCommon/cxxreact/JSExecutor.cpp deleted file mode 100644 index ca88df2ec312..000000000000 --- a/ReactCommon/cxxreact/JSExecutor.cpp +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright (c) Facebook, Inc. and its affiliates. - -// This source code is licensed under the MIT license found in the -// LICENSE file in the root directory of this source tree. - -#include "JSExecutor.h" - -#include "RAMBundleRegistry.h" - -#include - -namespace facebook { -namespace react { - -std::string JSExecutor::getSyntheticBundlePath( - uint32_t bundleId, - const std::string& bundlePath) { - if (bundleId == RAMBundleRegistry::MAIN_BUNDLE_ID) { - return bundlePath; - } - return folly::to("seg-", bundleId, ".js"); -} - -} -} diff --git a/ReactCommon/cxxreact/JSExecutor.h b/ReactCommon/cxxreact/JSExecutor.h index 2cee55016716..514d48fc8828 100644 --- a/ReactCommon/cxxreact/JSExecutor.h +++ b/ReactCommon/cxxreact/JSExecutor.h @@ -23,7 +23,6 @@ class JSBigString; class JSExecutor; class MessageQueueThread; class ModuleRegistry; -class RAMBundleRegistry; // This interface describes the delegate interface required by // Executor implementations to call from JS into native code. @@ -105,10 +104,6 @@ class RN_EXPORT JSExecutor { virtual void destroy() {} virtual ~JSExecutor() {} - - static std::string getSyntheticBundlePath( - uint32_t bundleId, - const std::string& bundlePath); }; } } diff --git a/ReactCommon/cxxreact/JSModulesUnbundle.h b/ReactCommon/cxxreact/JSModulesUnbundle.h deleted file mode 100644 index c926a5982c8e..000000000000 --- a/ReactCommon/cxxreact/JSModulesUnbundle.h +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright (c) Facebook, Inc. and its affiliates. - -// This source code is licensed under the MIT license found in the -// LICENSE file in the root directory of this source tree. - -#pragma once - -#include -#include -#include - -#include - -namespace facebook { -namespace react { - -class JSModulesUnbundle { - /** - * Represents the set of JavaScript modules that the application consists of. - * The source code of each module can be retrieved by module ID. - * - * The class is non-copyable because copying instances might involve copying - * several megabytes of memory. - */ -public: - class ModuleNotFound : public std::out_of_range { - public: - using std::out_of_range::out_of_range; - ModuleNotFound(uint32_t moduleId) : std::out_of_range::out_of_range( - folly::to("Module not found: ", moduleId)) {} - }; - struct Module { - std::string name; - std::string code; - }; - JSModulesUnbundle() {} - virtual ~JSModulesUnbundle() {} - virtual Module getModule(uint32_t moduleId) const = 0; - -private: - JSModulesUnbundle(const JSModulesUnbundle&) = delete; -}; - -} -} diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index 17973e910c6a..1deac434cf98 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -16,7 +16,6 @@ #include "MethodCall.h" #include "MessageQueueThread.h" #include "ModuleRegistry.h" -#include "RAMBundleRegistry.h" #ifdef WITH_FBSYSTRACE #include diff --git a/ReactCommon/cxxreact/NativeToJsBridge.h b/ReactCommon/cxxreact/NativeToJsBridge.h index dee37e1988dc..0cad47233043 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.h +++ b/ReactCommon/cxxreact/NativeToJsBridge.h @@ -25,7 +25,6 @@ struct InstanceCallback; class JsToNativeBridge; class MessageQueueThread; class ModuleRegistry; -class RAMBundleRegistry; // This class manages calls from native code to JS. It also manages // executors and their threads. All functions here can be called from diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.cpp b/ReactCommon/cxxreact/RAMBundleRegistry.cpp deleted file mode 100644 index f53370d3c4ac..000000000000 --- a/ReactCommon/cxxreact/RAMBundleRegistry.cpp +++ /dev/null @@ -1,75 +0,0 @@ -// Copyright (c) Facebook, Inc. and its affiliates. - -// This source code is licensed under the MIT license found in the -// LICENSE file in the root directory of this source tree. - -#include "RAMBundleRegistry.h" - -#include -#include - -namespace facebook { -namespace react { - -constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID; - -std::unique_ptr RAMBundleRegistry::singleBundleRegistry( - std::unique_ptr mainBundle) { - return folly::make_unique(std::move(mainBundle)); -} - -std::unique_ptr RAMBundleRegistry::multipleBundlesRegistry( - std::unique_ptr mainBundle, - std::function(std::string)> factory) { - return folly::make_unique( - std::move(mainBundle), std::move(factory)); -} - -RAMBundleRegistry::RAMBundleRegistry( - std::unique_ptr mainBundle, - std::function(std::string)> factory): - m_factory(std::move(factory)) { - m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle)); -} - -void RAMBundleRegistry::registerBundle( - uint32_t bundleId, std::string bundlePath) { - m_bundlePaths.emplace(bundleId, std::move(bundlePath)); -} - -JSModulesUnbundle::Module RAMBundleRegistry::getModule( - uint32_t bundleId, uint32_t moduleId) { - if (m_bundles.find(bundleId) == m_bundles.end()) { - if (!m_factory) { - throw std::runtime_error( - "You need to register factory function in order to " - "support multiple RAM bundles." - ); - } - - auto bundlePath = m_bundlePaths.find(bundleId); - if (bundlePath == m_bundlePaths.end()) { - throw std::runtime_error( - "In order to fetch RAM bundle from the registry, its file " - "path needs to be registered first." - ); - } - m_bundles.emplace(bundleId, m_factory(bundlePath->second)); - } - - auto module = getBundle(bundleId)->getModule(moduleId); - if (bundleId == MAIN_BUNDLE_ID) { - return module; - } - return { - folly::to("seg-", bundleId, '_', std::move(module.name)), - std::move(module.code), - }; -} - -JSModulesUnbundle* RAMBundleRegistry::getBundle(uint32_t bundleId) const { - return m_bundles.at(bundleId).get(); -} - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.h b/ReactCommon/cxxreact/RAMBundleRegistry.h deleted file mode 100644 index 224e1541b356..000000000000 --- a/ReactCommon/cxxreact/RAMBundleRegistry.h +++ /dev/null @@ -1,53 +0,0 @@ -// Copyright (c) Facebook, Inc. and its affiliates. - -// This source code is licensed under the MIT license found in the -// LICENSE file in the root directory of this source tree. - -#pragma once - -#include -#include -#include -#include -#include - -#include - -#ifndef RN_EXPORT -#define RN_EXPORT __attribute__((visibility("default"))) -#endif - -namespace facebook { -namespace react { - -class RN_EXPORT RAMBundleRegistry { -public: - constexpr static uint32_t MAIN_BUNDLE_ID = 0; - - static std::unique_ptr singleBundleRegistry( - std::unique_ptr mainBundle); - static std::unique_ptr multipleBundlesRegistry( - std::unique_ptr mainBundle, - std::function(std::string)> factory); - - explicit RAMBundleRegistry( - std::unique_ptr mainBundle, - std::function< - std::unique_ptr(std::string)> factory = nullptr); - - RAMBundleRegistry(RAMBundleRegistry&&) = default; - RAMBundleRegistry& operator=(RAMBundleRegistry&&) = default; - - void registerBundle(uint32_t bundleId, std::string bundlePath); - JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId); - virtual ~RAMBundleRegistry() {}; -private: - JSModulesUnbundle* getBundle(uint32_t bundleId) const; - - std::function(std::string)> m_factory; - std::unordered_map m_bundlePaths; - std::unordered_map> m_bundles; -}; - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp b/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp index c3a70ab221b7..138435bbeb23 100644 --- a/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp +++ b/ReactCommon/cxxreact/tests/JSDeltaBundleClientTest.cpp @@ -65,7 +65,7 @@ TEST(JSDeltaBundleClient, PatchModule) { EXPECT_EQ(client.getModule(0).code, "0"); EXPECT_EQ(client.getModule(1).code, "1"); - ASSERT_THROW(client.getModule(2), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(2), RAMBundle::ModuleNotFound); folly::dynamic delta2 = folly::parseJson(R"({ "base": false, @@ -83,7 +83,7 @@ TEST(JSDeltaBundleClient, PatchModule) { EXPECT_EQ(client.getModule(0).code, "0.1"); EXPECT_EQ(client.getModule(2).code, "2"); - ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(1), RAMBundle::ModuleNotFound); folly::dynamic delta3 = folly::parseJson(R"({ "base": true, @@ -98,9 +98,9 @@ TEST(JSDeltaBundleClient, PatchModule) { client.patch(delta3); - ASSERT_THROW(client.getModule(0), JSModulesUnbundle::ModuleNotFound); - ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound); - ASSERT_THROW(client.getModule(2), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(0), RAMBundle::ModuleNotFound); + ASSERT_THROW(client.getModule(1), RAMBundle::ModuleNotFound); + ASSERT_THROW(client.getModule(2), RAMBundle::ModuleNotFound); EXPECT_EQ(client.getModule(3).code, "3"); EXPECT_EQ(client.getModule(4).code, "4"); @@ -124,8 +124,8 @@ TEST(JSDeltaBundleClient, Clear) { client.clear(); - ASSERT_THROW(client.getModule(0), JSModulesUnbundle::ModuleNotFound); - ASSERT_THROW(client.getModule(1), JSModulesUnbundle::ModuleNotFound); + ASSERT_THROW(client.getModule(0), RAMBundle::ModuleNotFound); + ASSERT_THROW(client.getModule(1), RAMBundle::ModuleNotFound); EXPECT_STREQ(client.getStartupCode()->c_str(), ""); } diff --git a/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h b/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h index 947740f3c71e..cadd2004a8e5 100644 --- a/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h +++ b/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h @@ -9,7 +9,6 @@ #include #include -#include #include #include #include