Moving some RAM logic to Instance.

Reviewed By: michalgr

Differential Revision: D6208977

fbshipit-source-id: 28339d07c58f1acc816be02f2707ef75529fa069
This commit is contained in:
Dan Caspi 2017-11-03 05:17:23 -07:00 committed by Facebook Github Bot
parent 59a5dbc0b0
commit 19de7fcb22
4 changed files with 34 additions and 25 deletions

View File

@ -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<char *>(&header), sizeof(header));
bundle_stream.close();
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
}
void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName, void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
const std::string& sourceURL, const std::string& sourceURL,
bool loadSynchronously) { bool loadSynchronously) {
auto zFileName = fileName.c_str(); if (Instance::isIndexedRAMBundle(fileName.c_str())) {
if (isIndexedRAMBundle(zFileName)) { instance_->loadRAMBundleFromFile(fileName, sourceURL, loadSynchronously);
auto bundle = folly::make_unique<JSIndexedRAMBundle>(zFileName);
auto startupScript = bundle->getStartupCode();
auto registry = jsBundlesDirectory_.empty()
? folly::make_unique<RAMBundleRegistry>(std::move(bundle))
: folly::make_unique<JSIndexedRAMBundleRegistry>(std::move(bundle), jsBundlesDirectory_);
instance_->loadRAMBundle(
std::move(registry),
std::move(startupScript),
sourceURL,
loadSynchronously);
} else { } else {
std::unique_ptr<const JSBigFileString> script; std::unique_ptr<const JSBigFileString> script;
RecoverableError::runRethrowingAsRecoverable<std::system_error>( RecoverableError::runRethrowingAsRecoverable<std::system_error>(

View File

@ -40,8 +40,6 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
CatalystInstanceImpl(); CatalystInstanceImpl();
static bool isIndexedRAMBundle(const char *sourcePath);
void initializeBridge( void initializeBridge(
jni::alias_ref<ReactCallback::javaobject> callback, jni::alias_ref<ReactCallback::javaobject> callback,
// This executor is actually a factory holder. // This executor is actually a factory holder.

View File

@ -3,6 +3,7 @@
#include "Instance.h" #include "Instance.h"
#include "JSBigString.h" #include "JSBigString.h"
#include "JSBundleType.h"
#include "JSExecutor.h" #include "JSExecutor.h"
#include "MessageQueueThread.h" #include "MessageQueueThread.h"
#include "MethodCall.h" #include "MethodCall.h"
@ -11,6 +12,7 @@
#include "RecoverableError.h" #include "RecoverableError.h"
#include "SystraceSection.h" #include "SystraceSection.h"
#include <cxxreact/JSIndexedRAMBundle.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/MoveWrapper.h> #include <folly/MoveWrapper.h>
#include <folly/json.h> #include <folly/json.h>
@ -18,6 +20,7 @@
#include <glog/logging.h> #include <glog/logging.h>
#include <condition_variable> #include <condition_variable>
#include <fstream>
#include <mutex> #include <mutex>
#include <string> #include <string>
@ -91,6 +94,31 @@ void Instance::loadScriptFromString(std::unique_ptr<const JSBigString> 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<char *>(&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<JSIndexedRAMBundle>(sourcePath.c_str());
auto startupScript = bundle->getStartupCode();
auto registry = folly::make_unique<RAMBundleRegistry>(std::move(bundle));
loadRAMBundle(
std::move(registry),
std::move(startupScript),
sourceURL,
loadSynchronously);
}
void Instance::loadRAMBundle(std::unique_ptr<RAMBundleRegistry> bundleRegistry, void Instance::loadRAMBundle(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript, std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL, std::string startupScriptSourceURL,

View File

@ -44,6 +44,10 @@ public:
void loadScriptFromString(std::unique_ptr<const JSBigString> string, void loadScriptFromString(std::unique_ptr<const JSBigString> string,
std::string sourceURL, bool loadSynchronously); 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<RAMBundleRegistry> bundleRegistry, void loadRAMBundle(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
std::unique_ptr<const JSBigString> startupScript, std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL, bool loadSynchronously); std::string startupScriptSourceURL, bool loadSynchronously);