diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index e7e8723ed..60211f047 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -1105,6 +1105,10 @@ C6827DF71EF17CCC00D66BEF /* RCTJSEnvironment.h in Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; }; C6827DFB1EF1800E00D66BEF /* RCTJSEnvironment.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; }; C6827DFC1EF1801B00D66BEF /* RCTJSEnvironment.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = C6827DF51EF17CCC00D66BEF /* RCTJSEnvironment.h */; }; + C6D3801A1F71D76100621378 /* RAMBundleRegistry.h in Headers */ = {isa = PBXBuildFile; fileRef = C6D380181F71D75B00621378 /* RAMBundleRegistry.h */; }; + 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 */; }; 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 */; }; @@ -2095,6 +2099,8 @@ C6194AAB1EF156280034D062 /* RCTPackagerConnectionConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTPackagerConnectionConfig.h; sourceTree = ""; }; C654505D1F3BD9280090799B /* RCTManagedPointer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTManagedPointer.h; sourceTree = ""; }; 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 = ""; }; 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 = ""; }; @@ -2775,6 +2781,8 @@ AC70D2EE1DE48AC5002E6351 /* oss-compat-util.h */, 3D92B0D11E03699D0018521A /* Platform.cpp */, 3D92B0D21E03699D0018521A /* Platform.h */, + C6D380191F71D75B00621378 /* RAMBundleRegistry.cpp */, + C6D380181F71D75B00621378 /* RAMBundleRegistry.h */, 3D7454791E54757500E74ADD /* RecoverableError.h */, 3D92B0D31E03699D0018521A /* SampleCxxModule.cpp */, 3D92B0D41E03699D0018521A /* SampleCxxModule.h */, @@ -2979,6 +2987,7 @@ buildActionMask = 2147483647; files = ( 3D74547E1E54759A00E74ADD /* JSModulesUnbundle.h in Headers */, + C6D3801B1F71D76200621378 /* RAMBundleRegistry.h in Headers */, 27595AD51E575C7800CCE2B1 /* NativeToJsBridge.h in Headers */, 27595AC41E575C7800CCE2B1 /* Instance.h in Headers */, 27595AD11E575C7800CCE2B1 /* MessageQueueThread.h in Headers */, @@ -3067,6 +3076,7 @@ buildActionMask = 2147483647; files = ( 3D74547F1E54759E00E74ADD /* JSModulesUnbundle.h in Headers */, + C6D3801A1F71D76100621378 /* RAMBundleRegistry.h in Headers */, 27595ABA1E575C7800CCE2B1 /* NativeToJsBridge.h in Headers */, 27595AA91E575C7800CCE2B1 /* Instance.h in Headers */, 27595AB61E575C7800CCE2B1 /* MessageQueueThread.h in Headers */, @@ -3944,6 +3954,7 @@ 3DC159E51E83E1E9007B1282 /* JSBigString.cpp in Sources */, 13F8877B1E29726200C3C7A1 /* JSIndexedRAMBundle.cpp in Sources */, 13F8877D1E29726200C3C7A1 /* ModuleRegistry.cpp in Sources */, + C6D3801C1F71D76700621378 /* RAMBundleRegistry.cpp in Sources */, 13F8876E1E29726200C3C7A1 /* CxxNativeModule.cpp in Sources */, 13F887721E29726200C3C7A1 /* JSCExecutor.cpp in Sources */, 13F887741E29726200C3C7A1 /* JSCLegacyTracing.cpp in Sources */, @@ -3968,6 +3979,7 @@ 3DC159E61E83E1FA007B1282 /* JSBigString.cpp in Sources */, 13F8878E1E29726300C3C7A1 /* JSIndexedRAMBundle.cpp in Sources */, 13F887901E29726300C3C7A1 /* ModuleRegistry.cpp in Sources */, + C6D3801D1F71D76800621378 /* RAMBundleRegistry.cpp in Sources */, 13F887851E29726300C3C7A1 /* JSCExecutor.cpp in Sources */, 13F887871E29726300C3C7A1 /* JSCLegacyTracing.cpp in Sources */, 13F8878A1E29726300C3C7A1 /* JSCPerfStats.cpp in Sources */, diff --git a/ReactCommon/cxxreact/Android.mk b/ReactCommon/cxxreact/Android.mk index c29e7d1e3..fae8dbaab 100644 --- a/ReactCommon/cxxreact/Android.mk +++ b/ReactCommon/cxxreact/Android.mk @@ -22,6 +22,7 @@ LOCAL_SRC_FILES := \ ModuleRegistry.cpp \ NativeToJsBridge.cpp \ Platform.cpp \ + RAMBundleRegistry.cpp \ LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES) diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index 4a63f5cd8..a85aa6ef2 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -32,6 +32,7 @@ #include "JSModulesUnbundle.h" #include "ModuleRegistry.h" #include "Platform.h" +#include "RAMBundleRegistry.h" #include "RecoverableError.h" #include "SystraceSection.h" @@ -354,10 +355,10 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr scrip } void JSCExecutor::setJSModulesUnbundle(std::unique_ptr unbundle) { - if (!m_unbundle) { + if (!m_bundleRegistry) { installNativeHook<&JSCExecutor::nativeRequire>("nativeRequire"); } - m_unbundle = std::move(unbundle); + m_bundleRegistry = folly::make_unique(std::move(unbundle)); } void JSCExecutor::bindBridge() throw(JSException) { @@ -550,8 +551,8 @@ void JSCExecutor::flushQueueImmediate(Value&& queue) { m_delegate->callNativeModules(*this, folly::parseJson(queueStr), false); } -void JSCExecutor::loadModule(uint32_t moduleId) { - auto module = m_unbundle->getModule(moduleId); +void JSCExecutor::loadModule(uint32_t bundleId, uint32_t moduleId) { + auto module = m_bundleRegistry->getModule(bundleId, moduleId); auto sourceUrl = String::createExpectingAscii(m_context, module.name); auto source = String::createExpectingAscii(m_context, module.code); evaluateScript(m_context, source, sourceUrl); @@ -574,9 +575,10 @@ JSValueRef JSCExecutor::getNativeModule(JSObjectRef object, JSStringRef property JSValueRef JSCExecutor::nativeRequire( size_t argumentCount, const JSValueRef arguments[]) { - uint32_t moduleId = parseNativeRequireParameters(m_context, arguments, argumentCount).second; + uint32_t bundleId, moduleId; + std::tie(bundleId, moduleId) = parseNativeRequireParameters(m_context, arguments, argumentCount); ReactMarker::logMarker(ReactMarker::NATIVE_REQUIRE_START); - loadModule(moduleId); + loadModule(bundleId, moduleId); ReactMarker::logMarker(ReactMarker::NATIVE_REQUIRE_STOP); return Value::makeUndefined(m_context); } diff --git a/ReactCommon/cxxreact/JSCExecutor.h b/ReactCommon/cxxreact/JSCExecutor.h index 82e2b30f8..c027c7d12 100644 --- a/ReactCommon/cxxreact/JSCExecutor.h +++ b/ReactCommon/cxxreact/JSCExecutor.h @@ -5,7 +5,6 @@ #include #include #include -#include #include #include @@ -24,6 +23,7 @@ namespace facebook { namespace react { class MessageQueueThread; +class RAMBundleRegistry; class RN_EXPORT JSCExecutorFactory : public JSExecutorFactory { public: @@ -106,7 +106,7 @@ private: std::shared_ptr m_delegate; std::shared_ptr m_isDestroyed = std::shared_ptr(new bool(false)); std::shared_ptr m_messageQueueThread; - std::unique_ptr m_unbundle; + std::unique_ptr m_bundleRegistry; JSCNativeModules m_nativeModules; folly::dynamic m_jscConfig; std::once_flag m_bindFlag; @@ -125,7 +125,7 @@ private: void callNativeModules(Value&&); void flush(); void flushQueueImmediate(Value&&); - void loadModule(uint32_t moduleId); + void loadModule(uint32_t bundleId, uint32_t moduleId); String adoptString(std::unique_ptr); diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.cpp b/ReactCommon/cxxreact/RAMBundleRegistry.cpp new file mode 100644 index 000000000..2e4db7b11 --- /dev/null +++ b/ReactCommon/cxxreact/RAMBundleRegistry.cpp @@ -0,0 +1,23 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#include "RAMBundleRegistry.h" + +namespace facebook { +namespace react { + +constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID; + +RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr mainBundle) { + m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle)); +} + +JSModulesUnbundle::Module RAMBundleRegistry::getModule(uint32_t bundleId, uint32_t moduleId) { + return getBundle(bundleId)->getModule(moduleId); +} + +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 new file mode 100644 index 000000000..f25856929 --- /dev/null +++ b/ReactCommon/cxxreact/RAMBundleRegistry.h @@ -0,0 +1,28 @@ +// Copyright 2004-present Facebook. All Rights Reserved. + +#pragma once + +#include +#include +#include +#include + +#include + +namespace facebook { +namespace react { + +class RAMBundleRegistry { +public: + constexpr static uint32_t MAIN_BUNDLE_ID = 0; + + explicit RAMBundleRegistry(std::unique_ptr mainBundle); + JSModulesUnbundle::Module getModule(uint32_t bundleId, uint32_t moduleId); +private: + JSModulesUnbundle *getBundle(uint32_t bundleId) const; + + std::unordered_map> m_bundles; +}; + +} // namespace react +} // namespace facebook