mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-30 18:24:25 +08:00
Introduce non-copyable JSBigString for managing large strings efficiently
Reviewed By: astreet Differential Revision: D3234836 fbshipit-source-id: 2b95b585dc1215988b88cf0d609c778a95b362a1
This commit is contained in:
committed by
Facebook Github Bot 8
parent
f433ed716c
commit
9e9dfd2ac9
@@ -29,14 +29,95 @@ 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])
|
||||
, m_size(size) {}
|
||||
|
||||
~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;
|
||||
};
|
||||
|
||||
class JSExecutor {
|
||||
public:
|
||||
/**
|
||||
* Execute an application script bundle in the JS context.
|
||||
*/
|
||||
virtual void loadApplicationScript(
|
||||
const std::string& script,
|
||||
const std::string& sourceURL) = 0;
|
||||
virtual void loadApplicationScript(std::unique_ptr<const JSBigString> script,
|
||||
std::string sourceURL) = 0;
|
||||
|
||||
/**
|
||||
* Add an application "unbundle" file
|
||||
@@ -58,9 +139,8 @@ public:
|
||||
*/
|
||||
virtual void invokeCallback(const double callbackId, const folly::dynamic& arguments) = 0;
|
||||
|
||||
virtual void setGlobalVariable(
|
||||
const std::string& propName,
|
||||
const std::string& jsonValue) = 0;
|
||||
virtual void setGlobalVariable(std::string propName,
|
||||
std::unique_ptr<const JSBigString> jsonValue) = 0;
|
||||
virtual void* getJavaScriptContext() {
|
||||
return nullptr;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user