Skip to content

Commit

Permalink
Wire compiled bundle API
Browse files Browse the repository at this point in the history
Differential Revision: D3887878

fbshipit-source-id: 212fc3188f059d7378ecd61bb5e31fc87a857e8a
  • Loading branch information
Michał Gregorczyk authored and Facebook Github Bot committed Sep 29, 2016
1 parent aa36adb commit b1fdac4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 15 deletions.
2 changes: 2 additions & 0 deletions ReactCommon/cxxreact/Executor.h
Expand Up @@ -18,10 +18,12 @@ namespace react {

#define UNPACKED_JS_SOURCE_PATH_SUFFIX "/bundle.js"
#define UNPACKED_META_PATH_SUFFIX "/bundle.meta"
#define UNPACKED_BYTECODE_SUFFIX "/bundle.bytecode"

enum {
UNPACKED_JS_SOURCE = (1 << 0),
UNPACKED_BC_CACHE = (1 << 1),
UNPACKED_BYTECODE = (1 << 2),
};

class JSExecutor;
Expand Down
49 changes: 34 additions & 15 deletions ReactCommon/cxxreact/JSCExecutor.cpp
Expand Up @@ -9,9 +9,11 @@
#include <string>
#include <glog/logging.h>
#include <folly/json.h>
#include <folly/Exception.h>
#include <folly/Memory.h>
#include <folly/String.h>
#include <folly/Conv.h>
#include <fcntl.h>
#include <sys/time.h>

#include "FollySupport.h"
Expand Down Expand Up @@ -260,30 +262,47 @@ void JSCExecutor::loadApplicationScript(
SystraceSection s("JSCExecutor::loadApplicationScript",
"sourceURL", sourceURL);

if ((flags & UNPACKED_JS_SOURCE) == 0) {
throw std::runtime_error("Optimized bundle with no unpacked js source");
}
folly::throwOnFail<std::runtime_error>(
(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);
}
String jsSourceURL(sourceURL.c_str());
JSSourceCodeRef sourceCode = nullptr;
SCOPE_EXIT {
if (sourceCode) {
JSReleaseSourceCode(sourceCode);
}
};

ReactMarker::logMarker("RUN_JS_BUNDLE_START");
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); };

if (flags & UNPACKED_BC_CACHE) {
configureJSCBCCache(m_context, bundlePath);
}
auto length = lseek(fd, 0, SEEK_END);
folly::checkUnixError(length, "Couldn't seek to the end of compiled bundle");

String jsSourceURL(sourceURL.c_str());
JSSourceCodeRef sourceCode = JSCreateSourceCode(
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->size(),
jsSourceURL,
jsScriptBigString->hash(),
true);
SCOPE_EXIT { JSReleaseSourceCode(sourceCode); };
}

ReactMarker::logMarker("RUN_JS_BUNDLE_START");

evaluateSourceCode(m_context, sourceCode, jsSourceURL);

Expand Down

0 comments on commit b1fdac4

Please sign in to comment.