From b1fdac47b597594c6d3d1bfdd16eb8b8b1451029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Gregorczyk?= Date: Thu, 29 Sep 2016 09:08:28 -0700 Subject: [PATCH] Wire compiled bundle API Differential Revision: D3887878 fbshipit-source-id: 212fc3188f059d7378ecd61bb5e31fc87a857e8a --- ReactCommon/cxxreact/Executor.h | 2 ++ ReactCommon/cxxreact/JSCExecutor.cpp | 49 +++++++++++++++++++--------- 2 files changed, 36 insertions(+), 15 deletions(-) diff --git a/ReactCommon/cxxreact/Executor.h b/ReactCommon/cxxreact/Executor.h index b69923b754f7ee..017ff748bce3f4 100644 --- a/ReactCommon/cxxreact/Executor.h +++ b/ReactCommon/cxxreact/Executor.h @@ -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; diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index 9f2b4c1c8653f5..b074a2339e0401 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -9,9 +9,11 @@ #include #include #include +#include #include #include #include +#include #include #include "FollySupport.h" @@ -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( + (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);