Move JSBigString to its own header

Reviewed By: mhorowitz

Differential Revision: D4551162

fbshipit-source-id: 2374e05e60942211e50af86ed42eb194e5e82bbe
This commit is contained in:
Pieter De Baets 2017-02-16 06:45:41 -08:00 committed by Facebook Github Bot
parent 5d4ff693b4
commit 53b387638e
5 changed files with 324 additions and 230 deletions

View File

@ -170,7 +170,6 @@
13EBC63B1E284D8700880AC5 /* JSCUtils.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C31E03699D0018521A /* JSCUtils.h */; };
13EBC63C1E284D8700880AC5 /* JSCWebWorker.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C51E03699D0018521A /* JSCWebWorker.h */; };
13EBC63D1E284D8700880AC5 /* JSIndexedRAMBundle.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C71E03699D0018521A /* JSIndexedRAMBundle.h */; };
13EBC63E1E284D8700880AC5 /* JSModulesUnbundle.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */; };
13EBC63F1E284D8700880AC5 /* MessageQueueThread.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C91E03699D0018521A /* MessageQueueThread.h */; };
13EBC6401E284D8700880AC5 /* MethodCall.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CB1E03699D0018521A /* MethodCall.h */; };
13EBC6411E284D8700880AC5 /* ModuleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CD1E03699D0018521A /* ModuleRegistry.h */; };
@ -486,6 +485,46 @@
3D3CD9441DE5FC6500167DC4 /* libjschelpers.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 3D3CD9181DE5FBD800167DC4 /* libjschelpers.a */; };
3D3CD9451DE5FC7100167DC4 /* JSBundleType.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D3CD8F51DE5FB2300167DC4 /* JSBundleType.h */; };
3D3CD9471DE5FC7800167DC4 /* oss-compat-util.h in Headers */ = {isa = PBXBuildFile; fileRef = AC70D2EE1DE48AC5002E6351 /* oss-compat-util.h */; };
3D74547C1E54758900E74ADD /* JSBigString.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7454781E54757500E74ADD /* JSBigString.h */; };
3D74547D1E54758900E74ADD /* JSBigString.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7454781E54757500E74ADD /* JSBigString.h */; };
3D74547E1E54759A00E74ADD /* JSModulesUnbundle.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */; };
3D74547F1E54759E00E74ADD /* JSModulesUnbundle.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */; };
3D7454801E5475AF00E74ADD /* RecoverableError.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7454791E54757500E74ADD /* RecoverableError.h */; };
3D7454811E5475AF00E74ADD /* RecoverableError.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D7454791E54757500E74ADD /* RecoverableError.h */; };
3D7454821E5475DA00E74ADD /* JSBigString.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D7454781E54757500E74ADD /* JSBigString.h */; };
3D7454831E5475DA00E74ADD /* JSModulesUnbundle.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */; };
3D7454841E5475DA00E74ADD /* RecoverableError.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D7454791E54757500E74ADD /* RecoverableError.h */; };
3D7454851E5475E800E74ADD /* CxxMessageQueue.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0A61E03699D0018521A /* CxxMessageQueue.h */; };
3D7454861E5475E800E74ADD /* CxxModule.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0A71E03699D0018521A /* CxxModule.h */; };
3D7454871E5475E800E74ADD /* CxxNativeModule.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0A91E03699D0018521A /* CxxNativeModule.h */; };
3D7454881E5475E800E74ADD /* Executor.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0AB1E03699D0018521A /* Executor.h */; };
3D7454891E5475E800E74ADD /* ExecutorToken.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0AC1E03699D0018521A /* ExecutorToken.h */; };
3D74548A1E5475E800E74ADD /* ExecutorTokenFactory.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0AD1E03699D0018521A /* ExecutorTokenFactory.h */; };
3D74548B1E5475E800E74ADD /* Instance.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0AF1E03699D0018521A /* Instance.h */; };
3D74548C1E5475E800E74ADD /* JsArgumentHelpers-inl.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0B01E03699D0018521A /* JsArgumentHelpers-inl.h */; };
3D74548D1E5475E800E74ADD /* JsArgumentHelpers.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0B11E03699D0018521A /* JsArgumentHelpers.h */; };
3D74548E1E5475E800E74ADD /* JSBigString.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D7454781E54757500E74ADD /* JSBigString.h */; };
3D74548F1E5475E800E74ADD /* JSCExecutor.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0B31E03699D0018521A /* JSCExecutor.h */; };
3D7454901E5475E800E74ADD /* JSCLegacyProfiler.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0B51E03699D0018521A /* JSCLegacyProfiler.h */; };
3D7454911E5475E800E74ADD /* JSCLegacyTracing.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0B71E03699D0018521A /* JSCLegacyTracing.h */; };
3D7454921E5475E800E74ADD /* JSCMemory.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0B91E03699D0018521A /* JSCMemory.h */; };
3D7454931E5475E800E74ADD /* JSCNativeModules.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0BB1E03699D0018521A /* JSCNativeModules.h */; };
3D7454941E5475E800E74ADD /* JSCPerfStats.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0BD1E03699D0018521A /* JSCPerfStats.h */; };
3D7454951E5475E800E74ADD /* JSCSamplingProfiler.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0BF1E03699D0018521A /* JSCSamplingProfiler.h */; };
3D7454961E5475E800E74ADD /* JSCUtils.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C31E03699D0018521A /* JSCUtils.h */; };
3D7454971E5475E800E74ADD /* JSCWebWorker.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C51E03699D0018521A /* JSCWebWorker.h */; };
3D7454981E5475E800E74ADD /* JSIndexedRAMBundle.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C71E03699D0018521A /* JSIndexedRAMBundle.h */; };
3D7454991E5475E800E74ADD /* JSModulesUnbundle.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */; };
3D74549A1E5475E800E74ADD /* MessageQueueThread.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C91E03699D0018521A /* MessageQueueThread.h */; };
3D74549B1E5475E800E74ADD /* MethodCall.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CB1E03699D0018521A /* MethodCall.h */; };
3D74549C1E5475E800E74ADD /* ModuleRegistry.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CD1E03699D0018521A /* ModuleRegistry.h */; };
3D74549D1E5475E800E74ADD /* NativeModule.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CE1E03699D0018521A /* NativeModule.h */; };
3D74549E1E5475E800E74ADD /* NativeToJsBridge.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0D01E03699D0018521A /* NativeToJsBridge.h */; };
3D74549F1E5475E800E74ADD /* oss-compat-util.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = AC70D2EE1DE48AC5002E6351 /* oss-compat-util.h */; };
3D7454A01E5475E800E74ADD /* Platform.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0D21E03699D0018521A /* Platform.h */; };
3D7454A11E5475E800E74ADD /* RecoverableError.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D7454791E54757500E74ADD /* RecoverableError.h */; };
3D7454A21E5475E800E74ADD /* SampleCxxModule.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0D41E03699D0018521A /* SampleCxxModule.h */; };
3D7454A31E5475E800E74ADD /* SystraceSection.h in Copy Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0D51E03699D0018521A /* SystraceSection.h */; };
3D7749441DC1065C007EC8D8 /* RCTPlatform.m in Sources */ = {isa = PBXBuildFile; fileRef = 3D7749431DC1065C007EC8D8 /* RCTPlatform.m */; };
3D7A27E21DE325B7002E3F95 /* RCTJSCErrorHandling.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3D7A27E11DE325B7002E3F95 /* RCTJSCErrorHandling.mm */; };
3D7A27E31DE325DA002E3F95 /* RCTJSCErrorHandling.mm in Sources */ = {isa = PBXBuildFile; fileRef = 3D7A27E11DE325B7002E3F95 /* RCTJSCErrorHandling.mm */; };
@ -756,7 +795,6 @@
3D92B0F41E03699D0018521A /* JSCUtils.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C31E03699D0018521A /* JSCUtils.h */; };
3D92B0F61E03699D0018521A /* JSCWebWorker.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C51E03699D0018521A /* JSCWebWorker.h */; };
3D92B0F81E03699D0018521A /* JSIndexedRAMBundle.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C71E03699D0018521A /* JSIndexedRAMBundle.h */; };
3D92B0F91E03699D0018521A /* JSModulesUnbundle.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C81E03699D0018521A /* JSModulesUnbundle.h */; };
3D92B0FA1E03699D0018521A /* MessageQueueThread.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0C91E03699D0018521A /* MessageQueueThread.h */; };
3D92B0FC1E03699D0018521A /* MethodCall.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CB1E03699D0018521A /* MethodCall.h */; };
3D92B0FE1E03699D0018521A /* ModuleRegistry.h in Headers */ = {isa = PBXBuildFile; fileRef = 3D92B0CD1E03699D0018521A /* ModuleRegistry.h */; };
@ -920,6 +958,37 @@
dstPath = include/cxxreact;
dstSubfolderSpec = 16;
files = (
3D7454851E5475E800E74ADD /* CxxMessageQueue.h in Copy Headers */,
3D7454861E5475E800E74ADD /* CxxModule.h in Copy Headers */,
3D7454871E5475E800E74ADD /* CxxNativeModule.h in Copy Headers */,
3D7454881E5475E800E74ADD /* Executor.h in Copy Headers */,
3D7454891E5475E800E74ADD /* ExecutorToken.h in Copy Headers */,
3D74548A1E5475E800E74ADD /* ExecutorTokenFactory.h in Copy Headers */,
3D74548B1E5475E800E74ADD /* Instance.h in Copy Headers */,
3D74548C1E5475E800E74ADD /* JsArgumentHelpers-inl.h in Copy Headers */,
3D74548D1E5475E800E74ADD /* JsArgumentHelpers.h in Copy Headers */,
3D74548E1E5475E800E74ADD /* JSBigString.h in Copy Headers */,
3D74548F1E5475E800E74ADD /* JSCExecutor.h in Copy Headers */,
3D7454901E5475E800E74ADD /* JSCLegacyProfiler.h in Copy Headers */,
3D7454911E5475E800E74ADD /* JSCLegacyTracing.h in Copy Headers */,
3D7454921E5475E800E74ADD /* JSCMemory.h in Copy Headers */,
3D7454931E5475E800E74ADD /* JSCNativeModules.h in Copy Headers */,
3D7454941E5475E800E74ADD /* JSCPerfStats.h in Copy Headers */,
3D7454951E5475E800E74ADD /* JSCSamplingProfiler.h in Copy Headers */,
3D7454961E5475E800E74ADD /* JSCUtils.h in Copy Headers */,
3D7454971E5475E800E74ADD /* JSCWebWorker.h in Copy Headers */,
3D7454981E5475E800E74ADD /* JSIndexedRAMBundle.h in Copy Headers */,
3D7454991E5475E800E74ADD /* JSModulesUnbundle.h in Copy Headers */,
3D74549A1E5475E800E74ADD /* MessageQueueThread.h in Copy Headers */,
3D74549B1E5475E800E74ADD /* MethodCall.h in Copy Headers */,
3D74549C1E5475E800E74ADD /* ModuleRegistry.h in Copy Headers */,
3D74549D1E5475E800E74ADD /* NativeModule.h in Copy Headers */,
3D74549E1E5475E800E74ADD /* NativeToJsBridge.h in Copy Headers */,
3D74549F1E5475E800E74ADD /* oss-compat-util.h in Copy Headers */,
3D7454A01E5475E800E74ADD /* Platform.h in Copy Headers */,
3D7454A11E5475E800E74ADD /* RecoverableError.h in Copy Headers */,
3D7454A21E5475E800E74ADD /* SampleCxxModule.h in Copy Headers */,
3D7454A31E5475E800E74ADD /* SystraceSection.h in Copy Headers */,
3D302F1C1DF8264000D6DDAE /* JSBundleType.h in Copy Headers */,
);
name = "Copy Headers";
@ -1089,6 +1158,9 @@
dstPath = include/cxxreact;
dstSubfolderSpec = 16;
files = (
3D7454821E5475DA00E74ADD /* JSBigString.h in Copy Headers */,
3D7454831E5475DA00E74ADD /* JSModulesUnbundle.h in Copy Headers */,
3D7454841E5475DA00E74ADD /* RecoverableError.h in Copy Headers */,
13EBC62A1E284D8700880AC5 /* CxxMessageQueue.h in Copy Headers */,
13EBC62B1E284D8700880AC5 /* CxxModule.h in Copy Headers */,
13EBC62C1E284D8700880AC5 /* CxxNativeModule.h in Copy Headers */,
@ -1108,7 +1180,6 @@
13EBC63B1E284D8700880AC5 /* JSCUtils.h in Copy Headers */,
13EBC63C1E284D8700880AC5 /* JSCWebWorker.h in Copy Headers */,
13EBC63D1E284D8700880AC5 /* JSIndexedRAMBundle.h in Copy Headers */,
13EBC63E1E284D8700880AC5 /* JSModulesUnbundle.h in Copy Headers */,
13EBC63F1E284D8700880AC5 /* MessageQueueThread.h in Copy Headers */,
13EBC6401E284D8700880AC5 /* MethodCall.h in Copy Headers */,
13EBC6411E284D8700880AC5 /* ModuleRegistry.h in Copy Headers */,
@ -1399,6 +1470,8 @@
3D3CD9181DE5FBD800167DC4 /* libjschelpers.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libjschelpers.a; sourceTree = BUILT_PRODUCTS_DIR; };
3D3CD9251DE5FBEC00167DC4 /* libcxxreact.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libcxxreact.a; sourceTree = BUILT_PRODUCTS_DIR; };
3D3CD9321DE5FBEE00167DC4 /* libcxxreact.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libcxxreact.a; sourceTree = BUILT_PRODUCTS_DIR; };
3D7454781E54757500E74ADD /* JSBigString.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JSBigString.h; sourceTree = "<group>"; };
3D7454791E54757500E74ADD /* RecoverableError.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RecoverableError.h; sourceTree = "<group>"; };
3D7749421DC1065C007EC8D8 /* RCTPlatform.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RCTPlatform.h; sourceTree = "<group>"; };
3D7749431DC1065C007EC8D8 /* RCTPlatform.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RCTPlatform.m; sourceTree = "<group>"; };
3D7A27DC1DE32541002E3F95 /* JavaScriptCore.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JavaScriptCore.h; sourceTree = "<group>"; };
@ -2122,6 +2195,7 @@
3D92B0AF1E03699D0018521A /* Instance.h */,
3D92B0B01E03699D0018521A /* JsArgumentHelpers-inl.h */,
3D92B0B11E03699D0018521A /* JsArgumentHelpers.h */,
3D7454781E54757500E74ADD /* JSBigString.h */,
AC70D2EB1DE48A22002E6351 /* JSBundleType.cpp */,
3D3CD8F51DE5FB2300167DC4 /* JSBundleType.h */,
3D92B0B21E03699D0018521A /* JSCExecutor.cpp */,
@ -2156,6 +2230,7 @@
AC70D2EE1DE48AC5002E6351 /* oss-compat-util.h */,
3D92B0D11E03699D0018521A /* Platform.cpp */,
3D92B0D21E03699D0018521A /* Platform.h */,
3D7454791E54757500E74ADD /* RecoverableError.h */,
3D92B0D31E03699D0018521A /* SampleCxxModule.cpp */,
3D92B0D41E03699D0018521A /* SampleCxxModule.h */,
3D92B0D51E03699D0018521A /* SystraceSection.h */,
@ -2336,7 +2411,10 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
3D74547E1E54759A00E74ADD /* JSModulesUnbundle.h in Headers */,
3D7454811E5475AF00E74ADD /* RecoverableError.h in Headers */,
3D3030221DF8294C00D6DDAE /* JSBundleType.h in Headers */,
3D74547D1E54758900E74ADD /* JSBigString.h in Headers */,
3D3030231DF8294C00D6DDAE /* oss-compat-util.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -2378,7 +2456,10 @@
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
3D74547F1E54759E00E74ADD /* JSModulesUnbundle.h in Headers */,
3D7454801E5475AF00E74ADD /* RecoverableError.h in Headers */,
3D3CD9471DE5FC7800167DC4 /* oss-compat-util.h in Headers */,
3D74547C1E54758900E74ADD /* JSBigString.h in Headers */,
3D3CD9451DE5FC7100167DC4 /* JSBundleType.h in Headers */,
);
runOnlyForDeploymentPostprocessing = 0;
@ -2495,7 +2576,6 @@
3D80DA601DF820620028D040 /* RCTActivityIndicatorViewManager.h in Headers */,
3D80DA611DF820620028D040 /* RCTAnimationType.h in Headers */,
3D80DA621DF820620028D040 /* RCTAutoInsetsProtocol.h in Headers */,
3D92B0F91E03699D0018521A /* JSModulesUnbundle.h in Headers */,
3D80DA631DF820620028D040 /* RCTBorderDrawing.h in Headers */,
3D80DA641DF820620028D040 /* RCTBorderStyle.h in Headers */,
130443A11E3FEAA900D93A67 /* RCTFollyConvert.h in Headers */,

View File

@ -134,6 +134,7 @@ CXXREACT_PUBLIC_HEADERS = [
'JSCExecutor.h',
'JSCNativeModules.h',
'JSCWebWorker.h',
'JSBigString.h',
'JSBundleType.h',
'JSIndexedRAMBundle.h',
'JSModulesUnbundle.h',

View File

@ -2,19 +2,16 @@
#pragma once
#include <fcntl.h>
#include <functional>
#include <memory>
#include <string>
#include <sys/mman.h>
#include <vector>
#include <cxxreact/JSModulesUnbundle.h>
#include <folly/Exception.h>
#include <cxxreact/JSBigString.h>
#include <folly/Optional.h>
#include <folly/dynamic.h>
#ifndef RN_EXPORT
#define RN_EXPORT __attribute__((visibility("default")))
#endif
namespace facebook {
namespace react {
@ -61,224 +58,6 @@ public:
virtual ~JSExecutorFactory() {}
};
// JSExecutor functions sometimes take large strings, on the order of
// megabytes. Copying these can be expensive. Introducing a
// move-only, non-CopyConstructible type will let the compiler ensure
// that no copies occur. folly::MoveWrapper should be used when a
// large string needs to be curried into a std::function<>, which must
// by CopyConstructible.
class JSBigString {
public:
JSBigString() = default;
// Not copyable
JSBigString(const JSBigString&) = delete;
JSBigString& operator=(const JSBigString&) = delete;
virtual ~JSBigString() {}
virtual bool isAscii() const = 0;
virtual const char* c_str() const = 0;
virtual size_t size() const = 0;
};
// Concrete JSBigString implementation which holds a std::string
// instance.
class JSBigStdString : public JSBigString {
public:
JSBigStdString(std::string str, bool isAscii=false)
: m_isAscii(isAscii)
, m_str(std::move(str)) {}
bool isAscii() const override {
return m_isAscii;
}
const char* c_str() const override {
return m_str.c_str();
}
size_t size() const override {
return m_str.size();
}
private:
bool m_isAscii;
std::string m_str;
};
// Concrete JSBigString implementation which holds a heap-allocated
// buffer, and provides an accessor for writing to it. This can be
// used to construct a JSBigString in place, such as by reading from a
// file.
class JSBigBufferString : public facebook::react::JSBigString {
public:
JSBigBufferString(size_t size)
: m_data(new char[size + 1])
, m_size(size) {
// Guarantee nul-termination. The caller is responsible for
// filling in the rest of m_data.
m_data[m_size] = '\0';
}
~JSBigBufferString() {
delete[] m_data;
}
bool isAscii() const override {
return true;
}
const char* c_str() const override {
return m_data;
}
size_t size() const override {
return m_size;
}
char* data() {
return m_data;
}
private:
char* m_data;
size_t m_size;
};
// JSBigString interface implemented by a file-backed mmap region.
class RN_EXPORT JSBigFileString : public JSBigString {
public:
JSBigFileString(int fd, size_t size, off_t offset = 0)
: m_fd {-1}
, m_data {nullptr}
{
folly::checkUnixError(
m_fd = dup(fd),
"Could not duplicate file descriptor");
// Offsets given to mmap must be page aligend. We abstract away that
// restriction by sending a page aligned offset to mmap, and keeping track
// of the offset within the page that we must alter the mmap pointer by to
// get the final desired offset.
auto ps = getpagesize();
auto d = lldiv(offset, ps);
m_mapOff = d.quot;
m_pageOff = d.rem;
m_size = size + m_pageOff;
}
~JSBigFileString() {
if (m_data) {
munmap((void *)m_data, m_size);
}
close(m_fd);
}
bool isAscii() const override {
return true;
}
const char *c_str() const override {
if (!m_data) {
m_data = (const char *)mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, m_mapOff);
CHECK(m_data != MAP_FAILED)
<< " fd: " << m_fd
<< " size: " << m_size
<< " offset: " << m_mapOff
<< " error: " << std::strerror(errno);
}
return m_data + m_pageOff;
}
size_t size() const override {
return m_size - m_pageOff;
}
int fd() const {
return m_fd;
}
static std::unique_ptr<const JSBigFileString> fromPath(const std::string& sourceURL);
private:
int m_fd; // The file descriptor being mmaped
size_t m_size; // The size of the mmaped region
size_t m_pageOff; // The offset in the mmaped region to the data.
off_t m_mapOff; // The offset in the file to the mmaped region.
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;
};
class JSExecutor {
public:
/**

View File

@ -0,0 +1,235 @@
// Copyright 2004-present Facebook. All Rights Reserved.
#pragma once
#include <fcntl.h>
#include <sys/mman.h>
#include <folly/Exception.h>
#ifndef RN_EXPORT
#define RN_EXPORT __attribute__((visibility("default")))
#endif
namespace facebook {
namespace react {
// JSExecutor functions sometimes take large strings, on the order of
// megabytes. Copying these can be expensive. Introducing a
// move-only, non-CopyConstructible type will let the compiler ensure
// that no copies occur. folly::MoveWrapper should be used when a
// large string needs to be curried into a std::function<>, which must
// by CopyConstructible.
class JSBigString {
public:
JSBigString() = default;
// Not copyable
JSBigString(const JSBigString&) = delete;
JSBigString& operator=(const JSBigString&) = delete;
virtual ~JSBigString() {}
virtual bool isAscii() const = 0;
virtual const char* c_str() const = 0;
virtual size_t size() const = 0;
};
// Concrete JSBigString implementation which holds a std::string
// instance.
class JSBigStdString : public JSBigString {
public:
JSBigStdString(std::string str, bool isAscii=false)
: m_isAscii(isAscii)
, m_str(std::move(str)) {}
bool isAscii() const override {
return m_isAscii;
}
const char* c_str() const override {
return m_str.c_str();
}
size_t size() const override {
return m_str.size();
}
private:
bool m_isAscii;
std::string m_str;
};
// Concrete JSBigString implementation which holds a heap-allocated
// buffer, and provides an accessor for writing to it. This can be
// used to construct a JSBigString in place, such as by reading from a
// file.
class JSBigBufferString : public JSBigString {
public:
JSBigBufferString(size_t size)
: m_data(new char[size + 1])
, m_size(size) {
// Guarantee nul-termination. The caller is responsible for
// filling in the rest of m_data.
m_data[m_size] = '\0';
}
~JSBigBufferString() {
delete[] m_data;
}
bool isAscii() const override {
return true;
}
const char* c_str() const override {
return m_data;
}
size_t size() const override {
return m_size;
}
char* data() {
return m_data;
}
private:
char* m_data;
size_t m_size;
};
// JSBigString interface implemented by a file-backed mmap region.
class RN_EXPORT JSBigFileString : public JSBigString {
public:
JSBigFileString(int fd, size_t size, off_t offset = 0)
: m_fd {-1}
, m_data {nullptr}
{
folly::checkUnixError(
m_fd = dup(fd),
"Could not duplicate file descriptor");
// Offsets given to mmap must be page aligend. We abstract away that
// restriction by sending a page aligned offset to mmap, and keeping track
// of the offset within the page that we must alter the mmap pointer by to
// get the final desired offset.
auto ps = getpagesize();
auto d = lldiv(offset, ps);
m_mapOff = d.quot;
m_pageOff = d.rem;
m_size = size + m_pageOff;
}
~JSBigFileString() {
if (m_data) {
munmap((void *)m_data, m_size);
}
close(m_fd);
}
bool isAscii() const override {
return true;
}
const char *c_str() const override {
if (!m_data) {
m_data = (const char *)mmap(0, m_size, PROT_READ, MAP_SHARED, m_fd, m_mapOff);
CHECK(m_data != MAP_FAILED)
<< " fd: " << m_fd
<< " size: " << m_size
<< " offset: " << m_mapOff
<< " error: " << std::strerror(errno);
}
return m_data + m_pageOff;
}
size_t size() const override {
return m_size - m_pageOff;
}
int fd() const {
return m_fd;
}
static std::unique_ptr<const JSBigFileString> fromPath(const std::string& sourceURL);
private:
int m_fd; // The file descriptor being mmaped
size_t m_size; // The size of the mmaped region
size_t m_pageOff; // The offset in the mmaped region to the data.
off_t m_mapOff; // The offset in the file to the mmaped region.
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

@ -7,14 +7,13 @@
#include <cxxreact/Executor.h>
#include <cxxreact/JSBundleType.h>
#include <cxxreact/JSModulesUnbundle.h>
namespace facebook {
namespace react {
class JSBigString;
#include <cxxreact/JSModulesUnbundle.h>
class JSIndexedRAMBundle : public facebook::react::JSModulesUnbundle {
public:
// Throws std::runtime_error on failure.