Wire compiled bundle API

Differential Revision: D3887878

fbshipit-source-id: 212fc3188f059d7378ecd61bb5e31fc87a857e8a
This commit is contained in:
Michał Gregorczyk 2016-09-29 09:08:28 -07:00 committed by Facebook Github Bot
parent aa36adb116
commit b1fdac47b5
2 changed files with 38 additions and 17 deletions

View File

@ -18,10 +18,12 @@ namespace react {
#define UNPACKED_JS_SOURCE_PATH_SUFFIX "/bundle.js" #define UNPACKED_JS_SOURCE_PATH_SUFFIX "/bundle.js"
#define UNPACKED_META_PATH_SUFFIX "/bundle.meta" #define UNPACKED_META_PATH_SUFFIX "/bundle.meta"
#define UNPACKED_BYTECODE_SUFFIX "/bundle.bytecode"
enum { enum {
UNPACKED_JS_SOURCE = (1 << 0), UNPACKED_JS_SOURCE = (1 << 0),
UNPACKED_BC_CACHE = (1 << 1), UNPACKED_BC_CACHE = (1 << 1),
UNPACKED_BYTECODE = (1 << 2),
}; };
class JSExecutor; class JSExecutor;

View File

@ -9,9 +9,11 @@
#include <string> #include <string>
#include <glog/logging.h> #include <glog/logging.h>
#include <folly/json.h> #include <folly/json.h>
#include <folly/Exception.h>
#include <folly/Memory.h> #include <folly/Memory.h>
#include <folly/String.h> #include <folly/String.h>
#include <folly/Conv.h> #include <folly/Conv.h>
#include <fcntl.h>
#include <sys/time.h> #include <sys/time.h>
#include "FollySupport.h" #include "FollySupport.h"
@ -260,30 +262,47 @@ void JSCExecutor::loadApplicationScript(
SystraceSection s("JSCExecutor::loadApplicationScript", SystraceSection s("JSCExecutor::loadApplicationScript",
"sourceURL", sourceURL); "sourceURL", sourceURL);
if ((flags & UNPACKED_JS_SOURCE) == 0) { folly::throwOnFail<std::runtime_error>(
throw std::runtime_error("Optimized bundle with no unpacked js source"); (flags & UNPACKED_JS_SOURCE) || (flags & UNPACKED_BYTECODE),
} "Optimized bundle with no unpacked source or bytecode");
auto jsScriptBigString = JSBigMmapString::fromOptimizedBundle(bundlePath);
if (jsScriptBigString->encoding() != JSBigMmapString::Encoding::Ascii) {
LOG(WARNING) << "Bundle is not ASCII encoded - falling back to the slow path";
return loadApplicationScript(std::move(jsScriptBigString), sourceURL);
}
ReactMarker::logMarker("RUN_JS_BUNDLE_START");
if (flags & UNPACKED_BC_CACHE) {
configureJSCBCCache(m_context, bundlePath);
}
String jsSourceURL(sourceURL.c_str()); String jsSourceURL(sourceURL.c_str());
JSSourceCodeRef sourceCode = JSCreateSourceCode( JSSourceCodeRef sourceCode = nullptr;
SCOPE_EXIT {
if (sourceCode) {
JSReleaseSourceCode(sourceCode);
}
};
if (flags & UNPACKED_BYTECODE) {
int fd = open((bundlePath + UNPACKED_BYTECODE_SUFFIX).c_str(), O_RDONLY);
folly::checkUnixError(fd, "Couldn't open compiled bundle");
SCOPE_EXIT { close(fd); };
auto length = lseek(fd, 0, SEEK_END);
folly::checkUnixError(length, "Couldn't seek to the end of compiled bundle");
sourceCode = JSCreateCompiledSourceCode(fd, length, jsSourceURL);
} else {
auto jsScriptBigString = JSBigMmapString::fromOptimizedBundle(bundlePath);
if (jsScriptBigString->encoding() != JSBigMmapString::Encoding::Ascii) {
LOG(WARNING) << "Bundle is not ASCII encoded - falling back to the slow path";
return loadApplicationScript(std::move(jsScriptBigString), sourceURL);
}
if (flags & UNPACKED_BC_CACHE) {
configureJSCBCCache(m_context, bundlePath);
}
sourceCode = JSCreateSourceCode(
jsScriptBigString->fd(), jsScriptBigString->fd(),
jsScriptBigString->size(), jsScriptBigString->size(),
jsSourceURL, jsSourceURL,
jsScriptBigString->hash(), jsScriptBigString->hash(),
true); true);
SCOPE_EXIT { JSReleaseSourceCode(sourceCode); }; }
ReactMarker::logMarker("RUN_JS_BUNDLE_START");
evaluateSourceCode(m_context, sourceCode, jsSourceURL); evaluateSourceCode(m_context, sourceCode, jsSourceURL);