From 79821917fabda1aa97489ea21b66550a2b93a491 Mon Sep 17 00:00:00 2001 From: Alex Dvornikov Date: Thu, 21 Sep 2017 08:34:46 -0700 Subject: [PATCH] Configure JSExector with BundleRegistry instead of JSModulesUnbundle. Differential Revision: D5850968 fbshipit-source-id: e5e7ad92c2347c2641551fcf820f061ffde5fed6 --- React/CxxBridge/RCTCxxBridge.mm | 6 +++-- React/CxxBridge/RCTObjcExecutor.mm | 4 ++-- React/React.xcodeproj/project.pbxproj | 2 ++ .../jni/react/jni/CatalystInstanceImpl.cpp | 12 ++++++---- .../src/main/jni/react/jni/ProxyExecutor.cpp | 4 ++-- .../src/main/jni/react/jni/ProxyExecutor.h | 4 ++-- ReactCommon/cxxreact/BUCK | 1 + ReactCommon/cxxreact/Instance.cpp | 22 +++++++++---------- ReactCommon/cxxreact/Instance.h | 12 +++++----- ReactCommon/cxxreact/JSCExecutor.cpp | 4 ++-- ReactCommon/cxxreact/JSCExecutor.h | 3 +-- ReactCommon/cxxreact/JSExecutor.h | 5 +++-- ReactCommon/cxxreact/NativeToJsBridge.cpp | 18 +++++++-------- ReactCommon/cxxreact/NativeToJsBridge.h | 8 +++---- ReactCommon/cxxreact/RAMBundleRegistry.cpp | 4 ++++ ReactCommon/cxxreact/RAMBundleRegistry.h | 11 +++++++++- 16 files changed, 71 insertions(+), 49 deletions(-) diff --git a/React/CxxBridge/RCTCxxBridge.mm b/React/CxxBridge/RCTCxxBridge.mm index bc66cf910..535f22c1b 100644 --- a/React/CxxBridge/RCTCxxBridge.mm +++ b/React/CxxBridge/RCTCxxBridge.mm @@ -36,6 +36,7 @@ #import #import #import +#import #import #import "NSDataBigString.h" @@ -1184,8 +1185,9 @@ RCT_NOT_IMPLEMENTED(- (instancetype)initWithBundleURL:(__unused NSURL *)bundleUR [self->_performanceLogger markStopForTag:RCTPLRAMBundleLoad]; [self->_performanceLogger setValue:scriptStr->size() forTag:RCTPLRAMStartupCodeSize]; if (self->_reactInstance) { - self->_reactInstance->loadUnbundle(std::move(ramBundle), std::move(scriptStr), - sourceUrlStr.UTF8String, !async); + auto registry = std::make_unique(std::move(ramBundle)); + self->_reactInstance->loadRAMBundle(std::move(registry), std::move(scriptStr), + sourceUrlStr.UTF8String, !async); } } else if (self->_reactInstance) { self->_reactInstance->loadScriptFromString(std::make_unique(script), diff --git a/React/CxxBridge/RCTObjcExecutor.mm b/React/CxxBridge/RCTObjcExecutor.mm index a8428dd72..53c32368d 100644 --- a/React/CxxBridge/RCTObjcExecutor.mm +++ b/React/CxxBridge/RCTObjcExecutor.mm @@ -91,8 +91,8 @@ public: }]; } - void setJSModulesUnbundle(std::unique_ptr) override { - RCTAssert(NO, @"Unbundle is not supported in RCTObjcExecutor"); + void setBundleRegistry(std::unique_ptr) override { + RCTAssert(NO, @"RAM bundles are not supported in RCTObjcExecutor"); } void callFunction(const std::string &module, const std::string &method, diff --git a/React/React.xcodeproj/project.pbxproj b/React/React.xcodeproj/project.pbxproj index 60211f047..b1b2b2b6f 100644 --- a/React/React.xcodeproj/project.pbxproj +++ b/React/React.xcodeproj/project.pbxproj @@ -1101,6 +1101,7 @@ C6194AB11EF156280034D062 /* RCTPackagerConnectionConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = C6194AAB1EF156280034D062 /* RCTPackagerConnectionConfig.h */; }; 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 */; }; 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 */; }; @@ -1608,6 +1609,7 @@ dstPath = include/cxxreact; dstSubfolderSpec = 16; files = ( + C669D8981F72E3DE006748EB /* RAMBundleRegistry.h in Copy Headers */, 3DA981A01E5B0E34004F2374 /* CxxModule.h in Copy Headers */, 3DA981A11E5B0E34004F2374 /* CxxNativeModule.h in Copy Headers */, 3DA981A21E5B0E34004F2374 /* JSExecutor.h in Copy Headers */, diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp index da393c836..d5f9e0971 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -185,8 +186,10 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets( auto manager = extractAssetManager(assetManager); auto script = loadScriptFromAssets(manager, sourceURL); if (JniJSModulesUnbundle::isUnbundle(manager, sourceURL)) { - instance_->loadUnbundle( - folly::make_unique(manager, sourceURL), + auto bundle = folly::make_unique(manager, sourceURL); + auto registry = folly::make_unique(std::move(bundle)); + instance_->loadRAMBundle( + std::move(registry), std::move(script), sourceURL, loadSynchronously); @@ -214,8 +217,9 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName, if (isIndexedRAMBundle(zFileName)) { auto bundle = folly::make_unique(zFileName); auto startupScript = bundle->getStartupCode(); - instance_->loadUnbundle( - std::move(bundle), + auto registry = folly::make_unique(std::move(bundle)); + instance_->loadRAMBundle( + std::move(registry), std::move(startupScript), sourceURL, loadSynchronously); diff --git a/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp b/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp index 5e495ec7a..d79f4f96d 100644 --- a/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp @@ -84,10 +84,10 @@ void ProxyExecutor::loadApplicationScript( // we launch the application. } -void ProxyExecutor::setJSModulesUnbundle(std::unique_ptr) { +void ProxyExecutor::setBundleRegistry(std::unique_ptr) { jni::throwNewJavaException( "java/lang/UnsupportedOperationException", - "Loading application unbundles is not supported for proxy executors"); + "Loading application RAM bundles is not supported for proxy executors"); } void ProxyExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) { diff --git a/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.h b/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.h index e4d88a2b3..30758073a 100644 --- a/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.h +++ b/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.h @@ -35,8 +35,8 @@ public: virtual void loadApplicationScript( std::unique_ptr script, std::string sourceURL) override; - virtual void setJSModulesUnbundle( - std::unique_ptr bundle) override; + virtual void setBundleRegistry( + std::unique_ptr bundle) override; virtual void callFunction( const std::string& moduleId, const std::string& methodId, diff --git a/ReactCommon/cxxreact/BUCK b/ReactCommon/cxxreact/BUCK index 053a23930..6b567cca0 100644 --- a/ReactCommon/cxxreact/BUCK +++ b/ReactCommon/cxxreact/BUCK @@ -87,6 +87,7 @@ CXXREACT_PUBLIC_HEADERS = [ "NativeModule.h", "NativeToJsBridge.h", "Platform.h", + "RAMBundleRegistry.h", "RecoverableError.h", "SharedProxyCxxModule.h", "SystraceSection.h", diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index f3329d27c..04e86cc82 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -4,10 +4,10 @@ #include "JSBigString.h" #include "JSExecutor.h" -#include "JSModulesUnbundle.h" #include "MessageQueueThread.h" #include "MethodCall.h" #include "NativeToJsBridge.h" +#include "RAMBundleRegistry.h" #include "RecoverableError.h" #include "SystraceSection.h" @@ -50,17 +50,17 @@ void Instance::initializeBridge( CHECK(nativeToJsBridge_); } -void Instance::loadApplication(std::unique_ptr unbundle, +void Instance::loadApplication(std::unique_ptr bundleRegistry, std::unique_ptr string, std::string sourceURL) { callback_->incrementPendingJSCalls(); SystraceSection s("Instance::loadApplication", "sourceURL", sourceURL); - nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(string), + nativeToJsBridge_->loadApplication(std::move(bundleRegistry), std::move(string), std::move(sourceURL)); } -void Instance::loadApplicationSync(std::unique_ptr unbundle, +void Instance::loadApplicationSync(std::unique_ptr bundleRegistry, std::unique_ptr string, std::string sourceURL) { std::unique_lock lock(m_syncMutex); @@ -68,7 +68,7 @@ void Instance::loadApplicationSync(std::unique_ptr unbundle, SystraceSection s("Instance::loadApplicationSync", "sourceURL", sourceURL); - nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(string), + nativeToJsBridge_->loadApplicationSync(std::move(bundleRegistry), std::move(string), std::move(sourceURL)); } @@ -91,15 +91,15 @@ void Instance::loadScriptFromString(std::unique_ptr string, } } -void Instance::loadUnbundle(std::unique_ptr unbundle, - std::unique_ptr startupScript, - std::string startupScriptSourceURL, - bool loadSynchronously) { +void Instance::loadRAMBundle(std::unique_ptr bundleRegistry, + std::unique_ptr startupScript, + std::string startupScriptSourceURL, + bool loadSynchronously) { if (loadSynchronously) { - loadApplicationSync(std::move(unbundle), std::move(startupScript), + loadApplicationSync(std::move(bundleRegistry), std::move(startupScript), std::move(startupScriptSourceURL)); } else { - loadApplication(std::move(unbundle), std::move(startupScript), + loadApplication(std::move(bundleRegistry), std::move(startupScript), std::move(startupScriptSourceURL)); } } diff --git a/ReactCommon/cxxreact/Instance.h b/ReactCommon/cxxreact/Instance.h index 5089830b4..45ab52842 100644 --- a/ReactCommon/cxxreact/Instance.h +++ b/ReactCommon/cxxreact/Instance.h @@ -21,9 +21,9 @@ namespace react { class JSBigString; class JSExecutorFactory; -class JSModulesUnbundle; class MessageQueueThread; class ModuleRegistry; +class RAMBundleRegistry; struct InstanceCallback { virtual ~InstanceCallback() {} @@ -44,9 +44,9 @@ public: void loadScriptFromString(std::unique_ptr string, std::string sourceURL, bool loadSynchronously); - void loadUnbundle(std::unique_ptr unbundle, - std::unique_ptr startupScript, - std::string startupScriptSourceURL, bool loadSynchronously); + void loadRAMBundle(std::unique_ptr bundleRegistry, + std::unique_ptr startupScript, + std::string startupScriptSourceURL, bool loadSynchronously); bool supportsProfiling(); void setGlobalVariable(std::string propName, std::unique_ptr jsonValue); @@ -73,10 +73,10 @@ public: private: void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch); - void loadApplication(std::unique_ptr unbundle, + void loadApplication(std::unique_ptr bundleRegistry, std::unique_ptr startupScript, std::string startupScriptSourceURL); - void loadApplicationSync(std::unique_ptr unbundle, + void loadApplicationSync(std::unique_ptr bundleRegistry, std::unique_ptr startupScript, std::string startupScriptSourceURL); diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index a85aa6ef2..f1162fe37 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -354,11 +354,11 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr scrip ReactMarker::logTaggedMarker(ReactMarker::RUN_JS_BUNDLE_STOP, scriptName.c_str()); } -void JSCExecutor::setJSModulesUnbundle(std::unique_ptr unbundle) { +void JSCExecutor::setBundleRegistry(std::unique_ptr bundleRegistry) { if (!m_bundleRegistry) { installNativeHook<&JSCExecutor::nativeRequire>("nativeRequire"); } - m_bundleRegistry = folly::make_unique(std::move(unbundle)); + m_bundleRegistry = std::move(bundleRegistry); } void JSCExecutor::bindBridge() throw(JSException) { diff --git a/ReactCommon/cxxreact/JSCExecutor.h b/ReactCommon/cxxreact/JSCExecutor.h index c027c7d12..25bd64c87 100644 --- a/ReactCommon/cxxreact/JSCExecutor.h +++ b/ReactCommon/cxxreact/JSCExecutor.h @@ -65,8 +65,7 @@ public: std::unique_ptr script, std::string sourceURL) override; - virtual void setJSModulesUnbundle( - std::unique_ptr unbundle) override; + virtual void setBundleRegistry(std::unique_ptr bundleRegistry) override; virtual void callFunction( const std::string& moduleId, diff --git a/ReactCommon/cxxreact/JSExecutor.h b/ReactCommon/cxxreact/JSExecutor.h index 7d9deb716..b8cc3d3f1 100644 --- a/ReactCommon/cxxreact/JSExecutor.h +++ b/ReactCommon/cxxreact/JSExecutor.h @@ -16,6 +16,7 @@ class JSExecutor; class JSModulesUnbundle; class MessageQueueThread; class ModuleRegistry; +class RAMBundleRegistry; // This interface describes the delegate interface required by // Executor implementations to call from JS into native code. @@ -48,9 +49,9 @@ public: std::string sourceURL) = 0; /** - * Add an application "unbundle" file + * Add an application "RAM" bundle registry */ - virtual void setJSModulesUnbundle(std::unique_ptr bundle) = 0; + virtual void setBundleRegistry(std::unique_ptr bundleRegistry) = 0; /** * Executes BatchedBridge.callFunctionReturnFlushedQueue with the module ID, diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index 39329c377..3f1f78c83 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -10,8 +10,8 @@ #include "JSBigString.h" #include "SystraceSection.h" #include "MethodCall.h" -#include "JSModulesUnbundle.h" #include "MessageQueueThread.h" +#include "RAMBundleRegistry.h" #ifdef WITH_FBSYSTRACE #include @@ -91,17 +91,17 @@ NativeToJsBridge::~NativeToJsBridge() { } void NativeToJsBridge::loadApplication( - std::unique_ptr unbundle, + std::unique_ptr bundleRegistry, std::unique_ptr startupScript, std::string startupScriptSourceURL) { runOnExecutorQueue( - [unbundleWrap=folly::makeMoveWrapper(std::move(unbundle)), + [bundleRegistryWrap=folly::makeMoveWrapper(std::move(bundleRegistry)), startupScript=folly::makeMoveWrapper(std::move(startupScript)), startupScriptSourceURL=std::move(startupScriptSourceURL)] (JSExecutor* executor) mutable { - auto unbundle = unbundleWrap.move(); - if (unbundle) { - executor->setJSModulesUnbundle(std::move(unbundle)); + auto bundleRegistry = bundleRegistryWrap.move(); + if (bundleRegistry) { + executor->setBundleRegistry(std::move(bundleRegistry)); } executor->loadApplicationScript(std::move(*startupScript), std::move(startupScriptSourceURL)); @@ -109,11 +109,11 @@ void NativeToJsBridge::loadApplication( } void NativeToJsBridge::loadApplicationSync( - std::unique_ptr unbundle, + std::unique_ptr bundleRegistry, std::unique_ptr startupScript, std::string startupScriptSourceURL) { - if (unbundle) { - m_executor->setJSModulesUnbundle(std::move(unbundle)); + if (bundleRegistry) { + m_executor->setBundleRegistry(std::move(bundleRegistry)); } m_executor->loadApplicationScript(std::move(startupScript), std::move(startupScriptSourceURL)); diff --git a/ReactCommon/cxxreact/NativeToJsBridge.h b/ReactCommon/cxxreact/NativeToJsBridge.h index fd581c743..dee12b6e3 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.h +++ b/ReactCommon/cxxreact/NativeToJsBridge.h @@ -18,10 +18,10 @@ namespace facebook { namespace react { struct InstanceCallback; -class JSModulesUnbundle; 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 @@ -85,16 +85,16 @@ public: } /** - * Starts the JS application. If unbundle is non-null, then it is + * Starts the JS application. If bundleRegistry is non-null, then it is * used to fetch JavaScript modules as individual scripts. * Otherwise, the script is assumed to include all the modules. */ void loadApplication( - std::unique_ptr unbundle, + std::unique_ptr bundleRegistry, std::unique_ptr startupCode, std::string sourceURL); void loadApplicationSync( - std::unique_ptr unbundle, + std::unique_ptr bundleRegistry, std::unique_ptr startupCode, std::string sourceURL); diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.cpp b/ReactCommon/cxxreact/RAMBundleRegistry.cpp index 2e4db7b11..f291f71f1 100644 --- a/ReactCommon/cxxreact/RAMBundleRegistry.cpp +++ b/ReactCommon/cxxreact/RAMBundleRegistry.cpp @@ -12,6 +12,10 @@ RAMBundleRegistry::RAMBundleRegistry(std::unique_ptr mainBund } 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)); + } + return getBundle(bundleId)->getModule(moduleId); } diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.h b/ReactCommon/cxxreact/RAMBundleRegistry.h index f25856929..1d45c1e5e 100644 --- a/ReactCommon/cxxreact/RAMBundleRegistry.h +++ b/ReactCommon/cxxreact/RAMBundleRegistry.h @@ -8,16 +8,25 @@ #include #include +#include namespace facebook { namespace react { -class RAMBundleRegistry { +class RAMBundleRegistry : noncopyable { public: constexpr static uint32_t MAIN_BUNDLE_ID = 0; explicit RAMBundleRegistry(std::unique_ptr mainBundle); + 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: JSModulesUnbundle *getBundle(uint32_t bundleId) const;