Deleting Optimized Bundle!

Reviewed By: javache

Differential Revision: D4566164

fbshipit-source-id: 1fbd3dd04f24399e93e3c6ec58956e6e18f1683f
This commit is contained in:
Ashok Menon 2017-02-16 13:24:44 -08:00 committed by Facebook Github Bot
parent 7c97008fa8
commit 14dc219810
13 changed files with 27 additions and 271 deletions

View File

@ -208,15 +208,9 @@ public class CatalystInstanceImpl implements CatalystInstance {
jniLoadScriptFromFile(fileName, sourceURL);
}
/* package */ void loadScriptFromOptimizedBundle(String path, String sourceURL, int flags) {
mSourceURL = sourceURL;
jniLoadScriptFromOptimizedBundle(path, sourceURL, flags);
}
private native void jniSetSourceURL(String sourceURL);
private native void jniLoadScriptFromAssets(AssetManager assetManager, String assetURL);
private native void jniLoadScriptFromFile(String fileName, String sourceURL);
private native void jniLoadScriptFromOptimizedBundle(String path, String sourceURL, int flags);
@Override
public void runJSBundle() {

View File

@ -105,8 +105,6 @@ void CatalystInstanceImpl::registerNatives() {
makeNativeMethod("jniSetSourceURL", CatalystInstanceImpl::jniSetSourceURL),
makeNativeMethod("jniLoadScriptFromAssets", CatalystInstanceImpl::jniLoadScriptFromAssets),
makeNativeMethod("jniLoadScriptFromFile", CatalystInstanceImpl::jniLoadScriptFromFile),
makeNativeMethod("jniLoadScriptFromOptimizedBundle",
CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle),
makeNativeMethod("jniCallJSFunction", CatalystInstanceImpl::jniCallJSFunction),
makeNativeMethod("jniCallJSCallback", CatalystInstanceImpl::jniCallJSCallback),
makeNativeMethod("getMainExecutorToken", CatalystInstanceImpl::getMainExecutorToken),
@ -217,14 +215,6 @@ void CatalystInstanceImpl::jniLoadScriptFromFile(const std::string& fileName,
}
}
void CatalystInstanceImpl::jniLoadScriptFromOptimizedBundle(const std::string& bundlePath,
const std::string& sourceURL,
jint flags) {
return instance_->loadScriptFromOptimizedBundle(std::move(bundlePath),
std::move(sourceURL),
flags);
}
void CatalystInstanceImpl::jniCallJSFunction(
JExecutorToken* token, std::string module, std::string method, NativeArray* arguments) {
// We want to share the C++ code, and on iOS, modules pass module/method

View File

@ -57,7 +57,6 @@ class CatalystInstanceImpl : public jni::HybridClass<CatalystInstanceImpl> {
void jniLoadScriptFromAssets(jni::alias_ref<JAssetManager::javaobject> assetManager, const std::string& assetURL);
void jniLoadScriptFromFile(const std::string& fileName, const std::string& sourceURL);
void jniLoadScriptFromOptimizedBundle(const std::string& bundlePath, const std::string& sourceURL, jint flags);
void jniCallJSFunction(JExecutorToken* token, std::string module, std::string method, NativeArray* arguments);
void jniCallJSCallback(JExecutorToken* token, jint callbackId, NativeArray* arguments);
local_ref<JExecutorToken::JavaPart> getMainExecutorToken();

View File

@ -1,85 +0,0 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#include "Executor.h"
#include <errno.h>
#include <fcntl.h>
#include <fstream>
#include <stdio.h>
#include <sys/stat.h>
#include <folly/Memory.h>
#include <folly/ScopeGuard.h>
namespace facebook {
namespace react {
void JSExecutor::loadApplicationScript(std::string bundlePath, std::string sourceURL, int flags) {
if ((flags & UNPACKED_JS_SOURCE) == 0) {
throw std::runtime_error("No unpacked js source file");
}
return loadApplicationScript(
JSBigOptimizedBundleString::fromOptimizedBundle(bundlePath),
std::move(sourceURL));
}
std::unique_ptr<const JSBigFileString> JSBigFileString::fromPath(const std::string& sourceURL) {
int fd = ::open(sourceURL.c_str(), O_RDONLY);
folly::checkUnixError(fd, "Could not open file", sourceURL);
SCOPE_EXIT { CHECK(::close(fd) == 0); };
struct stat fileInfo;
folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed.");
return folly::make_unique<const JSBigFileString>(fd, fileInfo.st_size);
}
static JSBigOptimizedBundleString::Encoding encodingFromByte(uint8_t byte) {
switch (byte) {
case 0:
return JSBigOptimizedBundleString::Encoding::Unknown;
case 1:
return JSBigOptimizedBundleString::Encoding::Ascii;
case 2:
return JSBigOptimizedBundleString::Encoding::Utf8;
case 3:
return JSBigOptimizedBundleString::Encoding::Utf16;
default:
throw std::invalid_argument("Unknown bundle encoding");
}
}
std::unique_ptr<const JSBigOptimizedBundleString> JSBigOptimizedBundleString::fromOptimizedBundle(
const std::string& bundlePath) {
uint8_t sha1[20];
uint8_t encoding;
struct stat fileInfo;
int fd = -1;
SCOPE_EXIT { CHECK(fd == -1 || ::close(fd) == 0); };
{
auto metaPath = bundlePath + UNPACKED_META_PATH_SUFFIX;
std::ifstream metaFile;
metaFile.exceptions(std::ifstream::eofbit | std::ifstream::failbit | std::ifstream::badbit);
metaFile.open(metaPath, std::ifstream::in | std::ifstream::binary);
metaFile.read(reinterpret_cast<char*>(sha1), sizeof(sha1));
metaFile.read(reinterpret_cast<char*>(&encoding), sizeof(encoding));
}
{
auto sourcePath = bundlePath + UNPACKED_JS_SOURCE_PATH_SUFFIX;
fd = ::open(sourcePath.c_str(), O_RDONLY);
folly::checkUnixError(fd, "could not open js bundle file.");
}
folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on js bundle failed.");
return folly::make_unique<const JSBigOptimizedBundleString>(
fd,
fileInfo.st_size,
sha1,
encodingFromByte(encoding));
}
} // namespace react
} // namespace facebook

View File

@ -66,11 +66,6 @@ public:
virtual void loadApplicationScript(std::unique_ptr<const JSBigString> script,
std::string sourceURL) = 0;
/**
* Execute an application script optimized bundle in the JS context.
*/
virtual void loadApplicationScript(std::string bundlePath, std::string source, int flags);
/**
* Add an application "unbundle" file
*/

View File

@ -90,16 +90,6 @@ void Instance::loadScriptFromFile(const std::string& filename,
nativeToJsBridge_->loadApplication(nullptr, std::move(script), sourceURL);
}
void Instance::loadScriptFromOptimizedBundle(std::string bundlePath,
std::string sourceURL,
int flags) {
SystraceSection s("reactbridge_xplat_loadScriptFromOptimizedBundle",
"bundlePath", bundlePath);
nativeToJsBridge_->loadOptimizedApplicationScript(std::move(bundlePath),
std::move(sourceURL),
flags);
}
void Instance::loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL) {

View File

@ -39,7 +39,6 @@ class Instance {
void loadScriptFromString(std::unique_ptr<const JSBigString> string, std::string sourceURL);
void loadScriptFromStringSync(std::unique_ptr<const JSBigString> string, std::string sourceURL);
void loadScriptFromFile(const std::string& filename, const std::string& sourceURL);
void loadScriptFromOptimizedBundle(std::string bundlePath, std::string sourceURL, int flags);
void loadUnbundle(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,

View File

@ -0,0 +1,27 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#include "JSBigString.h"
#include <fcntl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <folly/Memory.h>
#include <folly/ScopeGuard.h>
namespace facebook {
namespace react {
std::unique_ptr<const JSBigFileString> JSBigFileString::fromPath(const std::string& sourceURL) {
int fd = ::open(sourceURL.c_str(), O_RDONLY);
folly::checkUnixError(fd, "Could not open file", sourceURL);
SCOPE_EXIT { CHECK(::close(fd) == 0); };
struct stat fileInfo;
folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed.");
return folly::make_unique<const JSBigFileString>(fd, fileInfo.st_size);
}
} // namespace react
} // namespace facebook

View File

@ -165,71 +165,4 @@ private:
mutable const char *m_data; // Pointer to the mmaped region.
};
class JSBigOptimizedBundleString : public JSBigString {
public:
enum class Encoding {
Unknown,
Ascii,
Utf8,
Utf16,
};
JSBigOptimizedBundleString(int fd, size_t size, const uint8_t sha1[20], Encoding encoding) :
m_fd(-1),
m_size(size),
m_encoding(encoding),
m_str(nullptr)
{
folly::checkUnixError(
m_fd = dup(fd),
"Could not duplicate file descriptor");
memcpy(m_hash, sha1, 20);
}
~JSBigOptimizedBundleString() {
if (m_str) {
CHECK(munmap((void *)m_str, m_size) != -1);
}
close(m_fd);
}
bool isAscii() const override {
return m_encoding == Encoding::Ascii;
}
const char* c_str() const override {
if (!m_str) {
m_str = (const char *)mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, 0);
CHECK(m_str != MAP_FAILED);
}
return m_str;
}
size_t size() const override {
return m_size;
}
int fd() const {
return m_fd;
}
const uint8_t* hash() const {
return m_hash;
}
Encoding encoding() const {
return m_encoding;
}
static std::unique_ptr<const JSBigOptimizedBundleString> fromOptimizedBundle(const std::string& bundlePath);
private:
int m_fd;
size_t m_size;
uint8_t m_hash[20];
Encoding m_encoding;
mutable const char *m_str;
};
} }

View File

@ -343,65 +343,6 @@ static const char* explainLoadSourceStatus(JSLoadSourceStatus status) {
}
#endif
#ifdef WITH_FBJSCEXTENSIONS
void JSCExecutor::loadApplicationScript(
std::string bundlePath,
std::string sourceURL,
int flags) {
SystraceSection s("JSCExecutor::loadApplicationScript",
"sourceURL", sourceURL);
if (!(flags & (UNPACKED_JS_SOURCE | UNPACKED_BYTECODE))) {
throw RecoverableError("Optimized bundle with no unpacked source or bytecode");
}
String jsSourceURL(m_context, sourceURL.c_str());
JSSourceCodeRef sourceCode = nullptr;
SCOPE_EXIT {
if (sourceCode) {
JSReleaseSourceCode(sourceCode);
}
};
if (flags & UNPACKED_BYTECODE) {
int fd = open((bundlePath + UNPACKED_BYTECODE_SUFFIX).c_str(), O_RDONLY);
RecoverableError::runRethrowingAsRecoverable<std::system_error>([fd]() {
folly::checkUnixError(fd, "Couldn't open compiled bundle");
});
SCOPE_EXIT { close(fd); };
JSLoadSourceStatus jsStatus;
sourceCode = JSCreateCompiledSourceCode(fd, jsSourceURL, &jsStatus);
if (!sourceCode) {
throw RecoverableError(explainLoadSourceStatus(jsStatus));
}
} else {
auto jsScriptBigString = JSBigOptimizedBundleString::fromOptimizedBundle(bundlePath);
if (!jsScriptBigString->isAscii()) {
LOG(WARNING) << "Bundle is not ASCII encoded - falling back to the slow path";
return loadApplicationScript(std::move(jsScriptBigString), sourceURL);
}
sourceCode = JSCreateSourceCode(
jsScriptBigString->fd(),
jsSourceURL,
jsScriptBigString->hash(),
true);
}
ReactMarker::logMarker("RUN_JS_BUNDLE_START");
evaluateSourceCode(m_context, sourceCode, jsSourceURL);
bindBridge();
flush();
ReactMarker::logMarker("CREATE_REACT_CONTEXT_END");
ReactMarker::logMarker("RUN_JS_BUNDLE_END");
}
#endif
void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> script, std::string sourceURL) {
SystraceSection s("JSCExecutor::loadApplicationScript",
"sourceURL", sourceURL);

View File

@ -62,13 +62,6 @@ public:
std::unique_ptr<const JSBigString> script,
std::string sourceURL) override;
#ifdef WITH_FBJSCEXTENSIONS
virtual void loadApplicationScript(
std::string bundlePath,
std::string sourceURL,
int flags) override;
#endif
virtual void setJSModulesUnbundle(
std::unique_ptr<JSModulesUnbundle> unbundle) override;

View File

@ -111,20 +111,6 @@ NativeToJsBridge::~NativeToJsBridge() {
"NativeToJsBridge::destroy() must be called before deallocating the NativeToJsBridge!";
}
void NativeToJsBridge::loadOptimizedApplicationScript(
std::string bundlePath,
std::string sourceURL,
int flags) {
runOnExecutorQueue(
m_mainExecutorToken,
[bundlePath=std::move(bundlePath),
sourceURL=std::move(sourceURL),
flags=flags]
(JSExecutor* executor) {
executor->loadApplicationScript(std::move(bundlePath), std::move(sourceURL), flags);
});
}
void NativeToJsBridge::loadApplication(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,

View File

@ -123,12 +123,6 @@ public:
std::unique_ptr<const JSBigString> startupCode,
std::string sourceURL);
/**
* Similar to loading a "bundle", but instead of passing js source this method accepts
* path to a directory containing files prepared for particular JSExecutor.
*/
void loadOptimizedApplicationScript(std::string bundlePath, std::string sourceURL, int flags);
void setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue);
void* getJavaScriptContext();
bool supportsProfiling();