From 19de7fcb22f97d2c3cef5600aa3046d16d807340 Mon Sep 17 00:00:00 2001 From: Dan Caspi Date: Fri, 3 Nov 2017 05:17:23 -0700 Subject: [PATCH] Moving some RAM logic to Instance. Reviewed By: michalgr Differential Revision: D6208977 fbshipit-source-id: 28339d07c58f1acc816be02f2707ef75529fa069 --- .../jni/react/jni/CatalystInstanceImpl.cpp | 25 ++--------------- .../main/jni/react/jni/CatalystInstanceImpl.h | 2 -- ReactCommon/cxxreact/Instance.cpp | 28 +++++++++++++++++++ ReactCommon/cxxreact/Instance.h | 4 +++ 4 files changed, 34 insertions(+), 25 deletions(-) diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp index 4ce97ac87..1706e2d3e 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp @@ -207,32 +207,11 @@ void CatalystInstanceImpl::jniLoadScriptFromAssets( } } -bool CatalystInstanceImpl::isIndexedRAMBundle(const char *sourcePath) { - std::ifstream bundle_stream(sourcePath, std::ios_base::in); - if (!bundle_stream) { - return false; - } - BundleHeader header; - bundle_stream.read(reinterpret_cast(&header), sizeof(header)); - bundle_stream.close(); - return parseTypeFromHeader(header) == ScriptTag::RAMBundle; -} - void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL, bool loadSynchronously) { - auto zFileName = fileName.c_str(); - if (isIndexedRAMBundle(zFileName)) { - auto bundle = folly::make_unique(zFileName); - auto startupScript = bundle->getStartupCode(); - auto registry = jsBundlesDirectory_.empty() - ? folly::make_unique(std::move(bundle)) - : folly::make_unique(std::move(bundle), jsBundlesDirectory_); - instance_->loadRAMBundle( - std::move(registry), - std::move(startupScript), - sourceURL, - loadSynchronously); + if (Instance::isIndexedRAMBundle(fileName.c_str())) { + instance_->loadRAMBundleFromFile(fileName, sourceURL, loadSynchronously); } else { std::unique_ptr script; RecoverableError::runRethrowingAsRecoverable( diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h index 1c481a835..f1bd47e8e 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h @@ -40,8 +40,6 @@ class CatalystInstanceImpl : public jni::HybridClass { CatalystInstanceImpl(); - static bool isIndexedRAMBundle(const char *sourcePath); - void initializeBridge( jni::alias_ref callback, // This executor is actually a factory holder. diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index 04e86cc82..022680151 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -3,6 +3,7 @@ #include "Instance.h" #include "JSBigString.h" +#include "JSBundleType.h" #include "JSExecutor.h" #include "MessageQueueThread.h" #include "MethodCall.h" @@ -11,6 +12,7 @@ #include "RecoverableError.h" #include "SystraceSection.h" +#include #include #include #include @@ -18,6 +20,7 @@ #include #include +#include #include #include @@ -91,6 +94,31 @@ void Instance::loadScriptFromString(std::unique_ptr string, } } +bool Instance::isIndexedRAMBundle(const char *sourcePath) { + std::ifstream bundle_stream(sourcePath, std::ios_base::in); + BundleHeader header; + + if (!bundle_stream || + !bundle_stream.read(reinterpret_cast(&header), sizeof(header))) { + return false; + } + + return parseTypeFromHeader(header) == ScriptTag::RAMBundle; +} + +void Instance::loadRAMBundleFromFile(const std::string& sourcePath, + const std::string& sourceURL, + bool loadSynchronously) { + auto bundle = folly::make_unique(sourcePath.c_str()); + auto startupScript = bundle->getStartupCode(); + auto registry = folly::make_unique(std::move(bundle)); + loadRAMBundle( + std::move(registry), + std::move(startupScript), + sourceURL, + loadSynchronously); +} + void Instance::loadRAMBundle(std::unique_ptr bundleRegistry, std::unique_ptr startupScript, std::string startupScriptSourceURL, diff --git a/ReactCommon/cxxreact/Instance.h b/ReactCommon/cxxreact/Instance.h index 45ab52842..6999fb60d 100644 --- a/ReactCommon/cxxreact/Instance.h +++ b/ReactCommon/cxxreact/Instance.h @@ -44,6 +44,10 @@ public: void loadScriptFromString(std::unique_ptr string, std::string sourceURL, bool loadSynchronously); + static bool isIndexedRAMBundle(const char *sourcePath); + void loadRAMBundleFromFile(const std::string& sourcePath, + const std::string& sourceURL, + bool loadSynchronously); void loadRAMBundle(std::unique_ptr bundleRegistry, std::unique_ptr startupScript, std::string startupScriptSourceURL, bool loadSynchronously);