Getting rid of File descriptor based loading APIs

Reviewed By: javache

Differential Revision: D4333725

fbshipit-source-id: ba8591c7380e8eb90660a912f6fc86fc3d2d4774
This commit is contained in:
Ashok Menon 2017-01-10 07:04:49 -08:00 committed by Facebook Github Bot
parent b5f382c0e8
commit 8819bef56b
5 changed files with 71 additions and 72 deletions

View File

@ -40,7 +40,23 @@ if THIS_IS_FBANDROID:
'-DWITH_FB_MEMORY_PROFILING=1', '-DWITH_FB_MEMORY_PROFILING=1',
'-DWITH_INSPECTOR=1', '-DWITH_INSPECTOR=1',
], ],
deps = JSC_DEPS deps = JSC_DEPS,
visibility = [
# TL;DR: If you depend on this target directly, you're gonna have a
# Bad Time(TM).
#
# `facebook::react::JSCExecutor::initOnJSVMThread` (in `:bridge`) does
# some platform-dependant setup. Exactly what setup to do is
# determined by some static functors, defined in `Platform.h`, which
# are initially `nullptr`. On Android, these functors are properly
# assigned as part of `xreact`'s `JNI_OnLoad`. By depending directly
# on the bridge, we can mess up the SO initialisation order, causing
# `initOnJSVMThread` to be called before the platform-specific hooks
# have been properly initialised. Bad Times(TM).
# -- @ashokmenon (2017/01/03)
react_native_target('jni/xreact/jni:jni'),
react_native_xplat_target('cxxreact/...'),
],
) )
) )
@ -61,7 +77,8 @@ elif THIS_IS_FBOBJC:
preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS, preprocessor_flags = DEBUG_PREPROCESSOR_FLAGS,
deps = [ deps = [
'//xplat/folly:molly', '//xplat/folly:molly',
] ],
visibility = [ 'PUBLIC' ],
) )
) )
@ -131,7 +148,6 @@ CXXREACT_PUBLIC_HEADERS = [
] ]
react_library( react_library(
soname = 'libreactnativefb.$(ext)',
header_namespace = 'cxxreact', header_namespace = 'cxxreact',
force_static = True, force_static = True,
srcs = glob(['*.cpp'], excludes=['SampleCxxModule.cpp']), srcs = glob(['*.cpp'], excludes=['SampleCxxModule.cpp']),
@ -155,5 +171,4 @@ react_library(
react_native_xplat_target('jschelpers:jschelpers'), react_native_xplat_target('jschelpers:jschelpers'),
react_native_xplat_target('microprofiler:microprofiler'), react_native_xplat_target('microprofiler:microprofiler'),
], ],
visibility = [ 'PUBLIC' ],
) )

View File

@ -23,12 +23,15 @@ void JSExecutor::loadApplicationScript(std::string bundlePath, std::string sourc
std::move(sourceURL)); std::move(sourceURL));
} }
void JSExecutor::loadApplicationScript(int fd, std::string 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; struct stat fileInfo;
folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed."); folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed.");
auto bundle = folly::make_unique<JSBigFileString>(fd, fileInfo.st_size); return folly::make_unique<const JSBigFileString>(fd, fileInfo.st_size);
return loadApplicationScript(std::move(bundle), std::move(sourceURL));
} }
static JSBigOptimizedBundleString::Encoding encodingFromByte(uint8_t byte) { static JSBigOptimizedBundleString::Encoding encodingFromByte(uint8_t byte) {

View File

@ -15,6 +15,8 @@
#include "JSModulesUnbundle.h" #include "JSModulesUnbundle.h"
#define RN_EXPORT __attribute__((visibility("default")))
namespace facebook { namespace facebook {
namespace react { namespace react {
@ -150,7 +152,7 @@ private:
}; };
// JSBigString interface implemented by a file-backed mmap region. // JSBigString interface implemented by a file-backed mmap region.
class JSBigFileString : public JSBigString { class RN_EXPORT JSBigFileString : public JSBigString {
public: public:
JSBigFileString(int fd, size_t size, off_t offset = 0) JSBigFileString(int fd, size_t size, off_t offset = 0)
@ -204,6 +206,8 @@ public:
return m_fd; return m_fd;
} }
static std::unique_ptr<const JSBigFileString> fromPath(const std::string& sourceURL);
private: private:
int m_fd; // The file descriptor being mmaped int m_fd; // The file descriptor being mmaped
size_t m_size; // The size of the mmaped region size_t m_size; // The size of the mmaped region
@ -292,14 +296,6 @@ public:
*/ */
virtual void loadApplicationScript(std::string bundlePath, std::string source, int flags); virtual void loadApplicationScript(std::string bundlePath, std::string source, int flags);
/**
* @experimental
*
* Read an app bundle from a file descriptor, determine how it should be
* loaded, load and execute it in the JS context.
*/
virtual void loadApplicationScript(int fd, std::string source);
/** /**
* Add an application "unbundle" file * Add an application "unbundle" file
*/ */

View File

@ -397,61 +397,55 @@ void JSCExecutor::loadApplicationScript(
} }
#endif #endif
#ifdef WITH_FBJSCEXTENSIONS
void JSCExecutor::loadApplicationScript(
int fd,
std::string sourceURL)
{
String jsSourceURL(m_context, sourceURL.c_str());
JSLoadSourceError jsError;
auto bcSourceCode = JSCreateCompiledSourceCode(fd, jsSourceURL, &jsError);
switch (jsError) {
case JSLoadSourceErrorOnRead:
case JSLoadSourceErrorNotCompiled:
// Not bytecode, fall through.
return JSExecutor::loadApplicationScript(fd, sourceURL);
case JSLoadSourceErrorNone:
if (!bcSourceCode) {
throw std::runtime_error("Unexpected error opening compiled bundle");
}
break;
case JSLoadSourceErrorVersionMismatch:
case JSLoadSourceErrorUnknown:
throw RecoverableError(explainLoadSourceError(jsError));
}
ReactMarker::logMarker("RUN_JS_BUNDLE_START");
evaluateSourceCode(m_context, bcSourceCode, jsSourceURL);
// TODO(luk): t13903306 Remove this check once we make native modules
// working for java2js
if (m_delegate) {
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) throw(JSException) { void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> script, std::string sourceURL) throw(JSException) {
SystraceSection s("JSCExecutor::loadApplicationScript", SystraceSection s("JSCExecutor::loadApplicationScript",
"sourceURL", sourceURL); "sourceURL", sourceURL);
ReactMarker::logMarker("RUN_JS_BUNDLE_START");
String jsSourceURL(m_context, sourceURL.c_str());
#ifdef WITH_FBJSCEXTENSIONS
if (auto fileStr = dynamic_cast<const JSBigFileString *>(script.get())) {
JSLoadSourceError jsError;
auto bcSourceCode = JSCreateCompiledSourceCode(fileStr->fd(), jsSourceURL, &jsError);
switch (jsError) {
case JSLoadSourceErrorNone:
if (!bcSourceCode) {
throw std::runtime_error("Unexpected error opening compiled bundle");
}
evaluateSourceCode(m_context, bcSourceCode, jsSourceURL);
// TODO(luk): t13903306 Remove this check once we make native modules
// working for java2js
if (m_delegate) {
bindBridge();
flush();
}
ReactMarker::logMarker("CREATE_REACT_CONTEXT_END");
ReactMarker::logMarker("RUN_JS_BUNDLE_END");
return;
case JSLoadSourceErrorVersionMismatch:
case JSLoadSourceErrorUnknown:
throw RecoverableError(explainLoadSourceError(jsError));
case JSLoadSourceErrorOnRead:
case JSLoadSourceErrorNotCompiled:
// Not bytecode, fall through.
break;
}
}
#endif
#ifdef WITH_FBSYSTRACE #ifdef WITH_FBSYSTRACE
fbsystrace_begin_section( fbsystrace_begin_section(
TRACE_TAG_REACT_CXX_BRIDGE, TRACE_TAG_REACT_CXX_BRIDGE,
"JSCExecutor::loadApplicationScript-createExpectingAscii"); "JSCExecutor::loadApplicationScript-createExpectingAscii");
#endif #endif
ReactMarker::logMarker("RUN_JS_BUNDLE_START");
ReactMarker::logMarker("loadApplicationScript_startStringConvert"); ReactMarker::logMarker("loadApplicationScript_startStringConvert");
String jsScript = jsStringFromBigString(m_context, *script); String jsScript = jsStringFromBigString(m_context, *script);
ReactMarker::logMarker("loadApplicationScript_endStringConvert"); ReactMarker::logMarker("loadApplicationScript_endStringConvert");
@ -460,7 +454,6 @@ void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> scrip
fbsystrace_end_section(TRACE_TAG_REACT_CXX_BRIDGE); fbsystrace_end_section(TRACE_TAG_REACT_CXX_BRIDGE);
#endif #endif
String jsSourceURL(m_context, sourceURL.c_str());
evaluateScript(m_context, jsScript, jsSourceURL); evaluateScript(m_context, jsScript, jsSourceURL);
// TODO(luk): t13903306 Remove this check once we make native modules working for java2js // TODO(luk): t13903306 Remove this check once we make native modules working for java2js

View File

@ -22,9 +22,7 @@ namespace react {
class MessageQueueThread; class MessageQueueThread;
#define RN_JSC_EXECUTOR_EXPORT __attribute__((visibility("default"))) class RN_EXPORT JSCExecutorFactory : public JSExecutorFactory {
class RN_JSC_EXECUTOR_EXPORT JSCExecutorFactory : public JSExecutorFactory {
public: public:
JSCExecutorFactory(const std::string& cacheDir, const folly::dynamic& jscConfig) : JSCExecutorFactory(const std::string& cacheDir, const folly::dynamic& jscConfig) :
m_cacheDir(cacheDir), m_cacheDir(cacheDir),
@ -51,7 +49,7 @@ public:
template <typename T> template <typename T>
struct ValueEncoder; struct ValueEncoder;
class RN_JSC_EXECUTOR_EXPORT JSCExecutor : public JSExecutor { class RN_EXPORT JSCExecutor : public JSExecutor {
public: public:
/** /**
* Must be invoked from thread this Executor will run on. * Must be invoked from thread this Executor will run on.
@ -73,12 +71,6 @@ public:
int flags) override; int flags) override;
#endif #endif
#ifdef WITH_FBJSCEXTENSIONS
virtual void loadApplicationScript(
int fd,
std::string sourceURL) override;
#endif
virtual void setJSModulesUnbundle( virtual void setJSModulesUnbundle(
std::unique_ptr<JSModulesUnbundle> unbundle) override; std::unique_ptr<JSModulesUnbundle> unbundle) override;