From 6ecae73fe5915863c27ac7e407f5b151fd0c5fc3 Mon Sep 17 00:00:00 2001 From: Alex Dvornikov Date: Wed, 8 Nov 2017 03:50:36 -0800 Subject: [PATCH] Remove RAMBundleRegistry subclasses Differential Revision: D6262247 fbshipit-source-id: 708f919e34f1706d9aca19a34ad8ea506e9f8d8b --- React/CxxBridge/RCTCxxBridge.mm | 6 ++--- React/React.xcodeproj/project.pbxproj | 16 ------------ .../src/main/jni/react/jni/Android.mk | 1 - .../jni/react/jni/CatalystInstanceImpl.cpp | 22 ++++++++++------ .../jni/react/jni/JniRAMBundleRegistry.cpp | 24 ------------------ .../main/jni/react/jni/JniRAMBundleRegistry.h | 21 ---------------- ReactCommon/cxxreact/Android.mk | 1 - ReactCommon/cxxreact/BUCK | 1 - ReactCommon/cxxreact/Instance.cpp | 2 +- ReactCommon/cxxreact/JSIndexedRAMBundle.cpp | 9 +++++++ ReactCommon/cxxreact/JSIndexedRAMBundle.h | 2 ++ .../cxxreact/JSIndexedRAMBundleRegistry.cpp | 22 ---------------- .../cxxreact/JSIndexedRAMBundleRegistry.h | 25 ------------------- ReactCommon/cxxreact/RAMBundleRegistry.cpp | 19 ++++++++++++-- ReactCommon/cxxreact/RAMBundleRegistry.h | 14 ++++++----- 15 files changed, 54 insertions(+), 131 deletions(-) delete mode 100644 ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp delete mode 100644 ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h delete mode 100644 ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.cpp delete mode 100644 ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.h diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index aeb75c70ac85b7..88eb814aceb334 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -35,8 +35,8 @@ #import #import #import -#include #import +#import #import #import "NSDataBigString.h" @@ -1152,8 +1152,8 @@ - (void)executeApplicationScript:(NSData *)script ? [[self.delegate jsSegmentsDirectory].path stringByAppendingString:@"/"] : nil; auto registry = jsSegmentsDirectory != nil - ? std::make_unique(std::move(ramBundle), jsSegmentsDirectory.UTF8String) - : std::make_unique(std::move(ramBundle)); + ? RAMBundleRegistry::multipleBundlesRegistry(std::move(ramBundle), JSIndexedRAMBundle::buildFactory(jsSegmentsDirectory.UTF8String)) + : RAMBundleRegistry::singleBundleRegistry(std::move(ramBundle)); self->_reactInstance->loadRAMBundle(std::move(registry), std::move(scriptStr), sourceUrlStr.UTF8String, !async); } diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index 6e3ab54b1e7bac..0c7f8e3c2f2559 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -1023,7 +1023,6 @@ 597633371F4E021D005BE8A4 /* RCTShadowView+Internal.m in Sources */ = {isa = PBXBuildFile; fileRef = 597633341F4E021D005BE8A4 /* RCTShadowView+Internal.m */; }; 597633381F4E021D005BE8A4 /* RCTShadowView+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 597633351F4E021D005BE8A4 /* RCTShadowView+Internal.h */; }; 597633391F4E021D005BE8A4 /* RCTShadowView+Internal.h in Headers */ = {isa = PBXBuildFile; fileRef = 597633351F4E021D005BE8A4 /* RCTShadowView+Internal.h */; }; - 598FD1911F816998006C54CB /* JSIndexedRAMBundleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6D3801E1F71D91600621378 /* JSIndexedRAMBundleRegistry.h */; }; 598FD1921F816A2A006C54CB /* RAMBundleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6D380181F71D75B00621378 /* RAMBundleRegistry.h */; }; 598FD1931F817284006C54CB /* PrivateDataBase.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9936F3351F5F2F480010BF04 /* PrivateDataBase.cpp */; }; 598FD1941F8172A9006C54CB /* PrivateDataBase.h in Headers */ = {isa = PBXBuildFile; fileRef = 9936F3361F5F2F480010BF04 /* PrivateDataBase.h */; }; @@ -1158,7 +1157,6 @@ C654505E1F3BD9280090799B /* RCTManagedPointer.h in Headers */ = {isa = PBXBuildFile; fileRef = C654505D1F3BD9280090799B /* RCTManagedPointer.h */; }; C654505F1F3BD9280090799B /* RCTManagedPointer.h in Headers */ = {isa = PBXBuildFile; fileRef = C654505D1F3BD9280090799B /* RCTManagedPointer.h */; }; C669D8981F72E3DE006748EB /* RAMBundleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6D380181F71D75B00621378 /* RAMBundleRegistry.h */; }; - C669D8B91F72E4B7006748EB /* JSIndexedRAMBundleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6D3801E1F71D91600621378 /* JSIndexedRAMBundleRegistry.h */; }; C6827DF61EF17CCC00D66BEF /* RCTJSEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; }; C6827DF71EF17CCC00D66BEF /* RCTJSEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; }; C6827DFB1EF1800E00D66BEF /* RCTJSEnvironment.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; }; @@ -1167,10 +1165,6 @@ C6D3801B1F71D76200621378 /* RAMBundleRegistry.h in Headers */ = {isa = PBXBuildFile; fileRef = C6D380181F71D75B00621378 /* RAMBundleRegistry.h */; }; C6D3801C1F71D76700621378 /* RAMBundleRegistry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6D380191F71D75B00621378 /* RAMBundleRegistry.cpp */; }; C6D3801D1F71D76800621378 /* RAMBundleRegistry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6D380191F71D75B00621378 /* RAMBundleRegistry.cpp */; }; - C6D380201F71D91D00621378 /* JSIndexedRAMBundleRegistry.h in Headers */ = {isa = PBXBuildFile; fileRef = C6D3801E1F71D91600621378 /* JSIndexedRAMBundleRegistry.h */; }; - C6D380211F71D91E00621378 /* JSIndexedRAMBundleRegistry.h in Headers */ = {isa = PBXBuildFile; fileRef = C6D3801E1F71D91600621378 /* JSIndexedRAMBundleRegistry.h */; }; - C6D380221F71D92300621378 /* JSIndexedRAMBundleRegistry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6D3801F1F71D91600621378 /* JSIndexedRAMBundleRegistry.cpp */; }; - C6D380231F71D92400621378 /* JSIndexedRAMBundleRegistry.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C6D3801F1F71D91600621378 /* JSIndexedRAMBundleRegistry.cpp */; }; CF2731C01E7B8DE40044CA4F /* RCTDeviceInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = CF2731BE1E7B8DE40044CA4F /* RCTDeviceInfo.h */; }; CF2731C11E7B8DE40044CA4F /* RCTDeviceInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = CF2731BF1E7B8DE40044CA4F /* RCTDeviceInfo.m */; }; CF2731C21E7B8DEF0044CA4F /* RCTDeviceInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = CF2731BE1E7B8DE40044CA4F /* RCTDeviceInfo.h */; }; @@ -1455,7 +1449,6 @@ dstSubfolderSpec = 16; files = ( 598FD1921F816A2A006C54CB /* RAMBundleRegistry.h in Copy Headers */, - 598FD1911F816998006C54CB /* JSIndexedRAMBundleRegistry.h in Copy Headers */, 3DA9823B1E5B1053004F2374 /* CxxModule.h in Copy Headers */, 3DA9823C1E5B1053004F2374 /* CxxNativeModule.h in Copy Headers */, 3DA9823D1E5B1053004F2374 /* JSExecutor.h in Copy Headers */, @@ -1674,7 +1667,6 @@ dstPath = include/cxxreact; dstSubfolderSpec = 16; files = ( - C669D8B91F72E4B7006748EB /* JSIndexedRAMBundleRegistry.h in Copy Headers */, C669D8981F72E3DE006748EB /* RAMBundleRegistry.h in Copy Headers */, 3DA981A01E5B0E34004F2374 /* CxxModule.h in Copy Headers */, 3DA981A11E5B0E34004F2374 /* CxxNativeModule.h in Copy Headers */, @@ -2194,8 +2186,6 @@ C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTJSEnvironment.h; sourceTree = ""; }; C6D380181F71D75B00621378 /* RAMBundleRegistry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RAMBundleRegistry.h; sourceTree = ""; }; C6D380191F71D75B00621378 /* RAMBundleRegistry.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = RAMBundleRegistry.cpp; sourceTree = ""; }; - C6D3801E1F71D91600621378 /* JSIndexedRAMBundleRegistry.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = JSIndexedRAMBundleRegistry.h; sourceTree = ""; }; - C6D3801F1F71D91600621378 /* JSIndexedRAMBundleRegistry.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = JSIndexedRAMBundleRegistry.cpp; sourceTree = ""; }; CF2731BE1E7B8DE40044CA4F /* RCTDeviceInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTDeviceInfo.h; sourceTree = ""; }; CF2731BF1E7B8DE40044CA4F /* RCTDeviceInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTDeviceInfo.m; sourceTree = ""; }; E3BBC8EB1ADE6F47001BBD81 /* RCTTextDecorationLineType.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTTextDecorationLineType.h; sourceTree = ""; }; @@ -2905,8 +2895,6 @@ 3D92B0AB1E03699D0018521A /* JSExecutor.h */, 3D92B0C61E03699D0018521A /* JSIndexedRAMBundle.cpp */, 3D92B0C71E03699D0018521A /* JSIndexedRAMBundle.h */, - C6D3801F1F71D91600621378 /* JSIndexedRAMBundleRegistry.cpp */, - C6D3801E1F71D91600621378 /* JSIndexedRAMBundleRegistry.h */, 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */, 3D92B0C91E03699D0018521A /* MessageQueueThread.h */, 3D92B0CA1E03699D0018521A /* MethodCall.cpp */, @@ -3154,7 +3142,6 @@ 27595AC61E575C7800CCE2B1 /* JsArgumentHelpers.h in Headers */, 27595AD71E575C7800CCE2B1 /* SampleCxxModule.h in Headers */, 27595AD21E575C7800CCE2B1 /* MethodCall.h in Headers */, - C6D380211F71D91E00621378 /* JSIndexedRAMBundleRegistry.h in Headers */, 3D3030221DF8294C00D6DDAE /* JSBundleType.h in Headers */, 27595ACA1E575C7800CCE2B1 /* JSCMemory.h in Headers */, 3D74547D1E54758900E74ADD /* JSBigString.h in Headers */, @@ -3244,7 +3231,6 @@ 27595AAB1E575C7800CCE2B1 /* JsArgumentHelpers.h in Headers */, 27595ABC1E575C7800CCE2B1 /* SampleCxxModule.h in Headers */, 27595AB71E575C7800CCE2B1 /* MethodCall.h in Headers */, - C6D380201F71D91D00621378 /* JSIndexedRAMBundleRegistry.h in Headers */, 3D3CD9471DE5FC7800167DC4 /* oss-compat-util.h in Headers */, 27595AAF1E575C7800CCE2B1 /* JSCMemory.h in Headers */, 3D74547C1E54758900E74ADD /* JSBigString.h in Headers */, @@ -4133,7 +4119,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C6D380221F71D92300621378 /* JSIndexedRAMBundleRegistry.cpp in Sources */, 3DC159E51E83E1E9007B1282 /* JSBigString.cpp in Sources */, 13F8877B1E29726200C3C7A1 /* JSIndexedRAMBundle.cpp in Sources */, 13F8877D1E29726200C3C7A1 /* ModuleRegistry.cpp in Sources */, @@ -4159,7 +4144,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - C6D380231F71D92400621378 /* JSIndexedRAMBundleRegistry.cpp in Sources */, 3DC159E61E83E1FA007B1282 /* JSBigString.cpp in Sources */, 13F8878E1E29726300C3C7A1 /* JSIndexedRAMBundle.cpp in Sources */, 13F887901E29726300C3C7A1 /* ModuleRegistry.cpp in Sources */, diff --git a/ReactAndroid/src/main/jni/react/jni/Android.mk b/ReactAndroid/src/main/jni/react/jni/Android.mk index cc4cfbb5d446bd..8d8b992d52432e 100644 --- a/ReactAndroid/src/main/jni/react/jni/Android.mk +++ b/ReactAndroid/src/main/jni/react/jni/Android.mk @@ -14,7 +14,6 @@ LOCAL_SRC_FILES := \ JSLoader.cpp \ JSLogging.cpp \ JniJSModulesUnbundle.cpp \ - JniRAMBundleRegistry.cpp \ MethodInvoker.cpp \ ModuleRegistryBuilder.cpp \ NativeArray.cpp \ diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp index 5183a252145dc5..3bcc53aa8318e9 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp @@ -10,10 +10,10 @@ #include #include #include -#include #include -#include #include +#include +#include #include #include #include @@ -22,9 +22,8 @@ #include "CxxModuleWrapper.h" #include "JavaScriptExecutorHolder.h" -#include "JniJSModulesUnbundle.h" -#include "JniRAMBundleRegistry.h" #include "JNativeRunnable.h" +#include "JniJSModulesUnbundle.h" #include "NativeArray.h" using namespace facebook::jni; @@ -193,9 +192,7 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets( auto script = loadScriptFromAssets(manager, sourceURL); if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) { auto bundle = JniJSModulesUnbundle::fromEntryFile(manager, sourceURL); - auto registry = jsSegmentsDirectory_.empty() - ? folly::make_unique(std::move(bundle)) - : folly::make_unique(std::move(bundle), manager, sourceURL); + auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle)); instance_->loadRAMBundle( std::move(registry), std::move(script), @@ -211,7 +208,16 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously) { if (Instance::isIndexedRAMBundle(fileName.c_str())) { - instance_->loadRAMBundleFromFile(fileName, sourceURL, loadSynchronously); + auto bundle = folly::make_unique(fileName.c_str()); + auto script = bundle->getStartupCode(); + auto registry = jsSegmentsDirectory_.empty() + ? RAMBundleRegistry::singleBundleRegistry(std::move(bundle)) + : RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory(jsSegmentsDirectory_)); + instance_->loadRAMBundle( + std::move(registry), + std::move(script), + sourceURL, + loadSynchronously); } else { std::unique_ptr script; RecoverableError::runRethrowingAsRecoverable( diff --git a/ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp b/ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp deleted file mode 100644 index 3f2219a20f50e3..00000000000000 --- a/ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.cpp +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2004-present Facebook. All Rights Reserved. - -#include "JniRAMBundleRegistry.h" - -#include -#include - -#include "JniJSModulesUnbundle.h" - -namespace facebook { -namespace react { - -JniRAMBundleRegistry::JniRAMBundleRegistry(std::unique_ptr mainBundle, AAssetManager *assetManager, const std::string& baseDirectoryPath) : - RAMBundleRegistry(std::move(mainBundle)), - m_assetManager(assetManager), - m_baseDirectoryPath(baseDirectoryPath) {} - -std::unique_ptr JniRAMBundleRegistry::bundleById(uint32_t index) const { - std::string bundlePathById = m_baseDirectoryPath + folly::to(index) + "/js-modules/"; - return folly::make_unique(m_assetManager, bundlePathById); -} - -} -} diff --git a/ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h b/ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h deleted file mode 100644 index 255fa89e2edecc..00000000000000 --- a/ReactAndroid/src/main/jni/react/jni/JniRAMBundleRegistry.h +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2004-present Facebook. All Rights Reserved. - -#include -#include - -namespace facebook { -namespace react { - -class JniRAMBundleRegistry : public RAMBundleRegistry { -public: - JniRAMBundleRegistry(std::unique_ptr mainBundle, AAssetManager *assetManager, const std::string& baseDirectoryPath); - -protected: - virtual std::unique_ptr bundleById(uint32_t index) const override; -private: - AAssetManager *m_assetManager = nullptr; - std::string m_baseDirectoryPath; -}; - -} -} diff --git a/ReactCommon/cxxreact/Android.mk b/ReactCommon/cxxreact/Android.mk index 7e64ce3f91f1d3..fae8dbaabba4e0 100644 --- a/ReactCommon/cxxreact/Android.mk +++ b/ReactCommon/cxxreact/Android.mk @@ -18,7 +18,6 @@ LOCAL_SRC_FILES := \ JSCTracing.cpp \ JSCUtils.cpp \ JSIndexedRAMBundle.cpp \ - JSIndexedRAMBundleRegistry.cpp \ MethodCall.cpp \ ModuleRegistry.cpp \ NativeToJsBridge.cpp \ diff --git a/ReactCommon/cxxreact/BUCK b/ReactCommon/cxxreact/BUCK index 4012972acd58cc..6b567cca03b7ec 100644 --- a/ReactCommon/cxxreact/BUCK +++ b/ReactCommon/cxxreact/BUCK @@ -80,7 +80,6 @@ CXXREACT_PUBLIC_HEADERS = [ "JSCExecutor.h", "JSCNativeModules.h", "JSIndexedRAMBundle.h", - "JSIndexedRAMBundleRegistry.h", "JSModulesUnbundle.h", "MessageQueueThread.h", "MethodCall.h", diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index 0226801518dc6f..271dc85815d291 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -111,7 +111,7 @@ void Instance::loadRAMBundleFromFile(const std::string& sourcePath, bool loadSynchronously) { auto bundle = folly::make_unique(sourcePath.c_str()); auto startupScript = bundle->getStartupCode(); - auto registry = folly::make_unique(std::move(bundle)); + auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle)); loadRAMBundle( std::move(registry), std::move(startupScript), diff --git a/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp b/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp index 1fb50627e1636c..51672048570e99 100644 --- a/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp +++ b/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp @@ -2,11 +2,20 @@ #include "JSIndexedRAMBundle.h" +#include + #include "oss-compat-util.h" namespace facebook { namespace react { +std::function(uint32_t)> JSIndexedRAMBundle::buildFactory(const std::string& baseDirectoryPath) { + return [baseDirectoryPath](uint32_t index){ + std::string bundlePathById = baseDirectoryPath + toString(index) + ".jsbundle"; + return folly::make_unique(bundlePathById.c_str()); + }; +} + JSIndexedRAMBundle::JSIndexedRAMBundle(const char *sourcePath) : m_bundle (sourcePath, std::ios_base::in) { if (!m_bundle) { diff --git a/ReactCommon/cxxreact/JSIndexedRAMBundle.h b/ReactCommon/cxxreact/JSIndexedRAMBundle.h index 79e2587f30eeb8..3d4341b4a40a1b 100644 --- a/ReactCommon/cxxreact/JSIndexedRAMBundle.h +++ b/ReactCommon/cxxreact/JSIndexedRAMBundle.h @@ -17,6 +17,8 @@ namespace react { class RN_EXPORT JSIndexedRAMBundle : public JSModulesUnbundle { public: + static std::function(uint32_t)> buildFactory(const std::string& baseDirectoryPath); + // Throws std::runtime_error on failure. JSIndexedRAMBundle(const char *sourceURL); diff --git a/ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.cpp b/ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.cpp deleted file mode 100644 index 44a58e36181137..00000000000000 --- a/ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.cpp +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2004-present Facebook. All Rights Reserved. - -#include "JSIndexedRAMBundleRegistry.h" - -#include -#include - -#include "oss-compat-util.h" - -namespace facebook { -namespace react { - -JSIndexedRAMBundleRegistry::JSIndexedRAMBundleRegistry(std::unique_ptr mainBundle, const std::string& baseDirectoryPath): -RAMBundleRegistry(std::move(mainBundle)), m_baseDirectoryPath(baseDirectoryPath) {} - -std::unique_ptr JSIndexedRAMBundleRegistry::bundleById(uint32_t index) const { - std::string bundlePathById = m_baseDirectoryPath + toString(index) + ".jsbundle"; - return folly::make_unique(bundlePathById.c_str()); -} - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.h b/ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.h deleted file mode 100644 index 199f26772a54ce..00000000000000 --- a/ReactCommon/cxxreact/JSIndexedRAMBundleRegistry.h +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2004-present Facebook. All Rights Reserved. - -#pragma once - -#include - -#ifndef RN_EXPORT -#define RN_EXPORT __attribute__((visibility("default"))) -#endif - -namespace facebook { -namespace react { - -class RN_EXPORT JSIndexedRAMBundleRegistry: public RAMBundleRegistry { -public: - JSIndexedRAMBundleRegistry(std::unique_ptr mainBundle, const std::string& baseDirectoryPath); - -protected: - virtual std::unique_ptr bundleById(uint32_t index) const override; -private: - std::string m_baseDirectoryPath; -}; - -} // namespace react -} // namespace facebook diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.cpp b/ReactCommon/cxxreact/RAMBundleRegistry.cpp index 5159475bb41c6c..896eeb5f649c15 100644 --- a/ReactCommon/cxxreact/RAMBundleRegistry.cpp +++ b/ReactCommon/cxxreact/RAMBundleRegistry.cpp @@ -2,6 +2,8 @@ #include "RAMBundleRegistry.h" +#include + #include namespace facebook { @@ -9,13 +11,26 @@ namespace react { constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID; -RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr mainBundle) { +std::unique_ptr RAMBundleRegistry::singleBundleRegistry(std::unique_ptr mainBundle) { + RAMBundleRegistry *registry = new RAMBundleRegistry(std::move(mainBundle)); + return std::unique_ptr(registry); +} + +std::unique_ptr RAMBundleRegistry::multipleBundlesRegistry(std::unique_ptr mainBundle, std::function(uint32_t)> factory) { + RAMBundleRegistry *registry = new RAMBundleRegistry(std::move(mainBundle), std::move(factory)); + return std::unique_ptr(registry); +} + +RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr mainBundle, std::function(uint32_t)> factory): m_factory(factory) { m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle)); } JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32_t moduleId) { if (m_bundles.find(bundleId) == m_bundles.end()) { - m_bundles.emplace(bundleId, this->bundleById(bundleId)); + if (!m_factory) { + throw std::runtime_error("You need to register factory function in order to support multiple RAM bundles."); + } + m_bundles.emplace(bundleId, m_factory(bundleId)); } return getBundle(bundleId)->getModule(moduleId); diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.h b/ReactCommon/cxxreact/RAMBundleRegistry.h index 950379b83f4ee2..cb845a4751e29f 100644 --- a/ReactCommon/cxxreact/RAMBundleRegistry.h +++ b/ReactCommon/cxxreact/RAMBundleRegistry.h @@ -3,6 +3,7 @@ #pragma once #include +#include #include #include #include @@ -19,22 +20,23 @@ namespace react { class RN_EXPORT RAMBundleRegistry : noncopyable { public: + using unique_ram_bundle = std::unique_ptr; constexpr static uint32_t MAIN_BUNDLE_ID = 0; - explicit RAMBundleRegistry(std::unique_ptr mainBundle); + static std::unique_ptr singleBundleRegistry(unique_ram_bundle mainBundle); + static std::unique_ptr multipleBundlesRegistry(unique_ram_bundle mainBundle, std::function factory); + RAMBundleRegistry(RAMBundleRegistry&&) = default; RAMBundleRegistry& operator=(RAMBundleRegistry&&) = default; JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId); virtual ~RAMBundleRegistry() {}; -protected: - virtual std::unique_ptr bundleById(uint32_t index) const { - throw std::runtime_error("Please, override this method in a subclass to support multiple RAM bundles."); - } private: + explicit RAMBundleRegistry(unique_ram_bundle mainBundle, std::function factory = {}); JSModulesUnbundle *getBundle(uint32_t bundleId) const; - std::unordered_map> m_bundles; + std::function m_factory; + std::unordered_map m_bundles; }; } // namespace react