Skip to content

Commit 3012aac

Browse files
javachemeta-codesync[bot]
authored andcommitted
JSBigString implements jsi::Buffer (#54206)
Summary: Pull Request resolved: #54206 We don't need `BigStringBuffer` as a separate abstraction when `JSBigString` implements almost exactly the same interface. Changelog: [General][Changed] BigStringBuffer is deprecated and will be removed in a future release - use JSBigString instead [General][Breaking] JSBigString mutable data accessor has been renamed to mutableData Reviewed By: Abbondanzo Differential Revision: D84458636 fbshipit-source-id: f507fd4ff842301af3047afeccf853f30a2f72df
1 parent f687292 commit 3012aac

6 files changed

Lines changed: 27 additions & 20 deletions

File tree

packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ loadScriptFromAssets(AAssetManager* manager, const std::string& assetName) {
7272
}
7373

7474
auto buf = std::make_unique<JSBigBufferString>(script->size());
75-
memcpy(buf->data(), script->c_str(), script->size());
75+
memcpy(buf->mutableData(), script->c_str(), script->size());
7676
return buf;
7777
}
7878
}

packages/react-native/ReactCommon/cxxreact/JSBigString.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
#include <memory>
1111
#include <string>
1212

13+
#include <jsi/jsi.h>
14+
1315
#ifndef RN_EXPORT
1416
#ifdef _MSC_VER
1517
#define RN_EXPORT
@@ -27,23 +29,30 @@ namespace facebook::react {
2729
// large string needs to be curried into a std::function<>, which must
2830
// by CopyConstructible.
2931

30-
class JSBigString {
32+
class JSBigString : public facebook::jsi::Buffer {
3133
public:
3234
JSBigString() = default;
3335

34-
// Not copyable
36+
// Not copyable or movable
3537
JSBigString(const JSBigString &) = delete;
3638
JSBigString &operator=(const JSBigString &) = delete;
39+
JSBigString(JSBigString &&) = delete;
40+
JSBigString &operator=(JSBigString &&) = delete;
3741

38-
virtual ~JSBigString() = default;
42+
~JSBigString() override = default;
3943

4044
virtual bool isAscii() const = 0;
4145

4246
// This needs to be a \0 terminated string
4347
virtual const char *c_str() const = 0;
4448

4549
// Length of the c_str without the NULL byte.
46-
virtual size_t size() const = 0;
50+
size_t size() const override = 0;
51+
52+
const uint8_t *data() const final
53+
{
54+
return reinterpret_cast<const uint8_t *>(c_str());
55+
}
4756
};
4857

4958
// Concrete JSBigString implementation which holds a std::string
@@ -105,7 +114,7 @@ class RN_EXPORT JSBigBufferString : public JSBigString {
105114
return m_size;
106115
}
107116

108-
char *data()
117+
char *mutableData()
109118
{
110119
return m_data;
111120
}

packages/react-native/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ void JSIndexedRAMBundle::init() {
6060
"header size must exactly match the input file format");
6161

6262
readBundle(reinterpret_cast<char*>(header), sizeof(header));
63-
const size_t numTableEntries = folly::Endian::little(header[1]);
64-
const size_t startupCodeSize = folly::Endian::little(header[2]);
63+
size_t numTableEntries = folly::Endian::little(header[1]);
64+
std::streamsize startupCodeSize = folly::Endian::little(header[2]);
6565

6666
// allocate memory for meta data and lookup table.
6767
m_table = ModuleTable(numTableEntries);
@@ -73,7 +73,7 @@ void JSIndexedRAMBundle::init() {
7373
// read the startup code
7474
m_startupCode = std::make_unique<JSBigBufferString>(startupCodeSize - 1);
7575

76-
readBundle(m_startupCode->data(), startupCodeSize - 1);
76+
readBundle(m_startupCode->mutableData(), startupCodeSize - 1);
7777
}
7878

7979
JSIndexedRAMBundle::Module JSIndexedRAMBundle::getModule(
@@ -109,8 +109,7 @@ std::string JSIndexedRAMBundle::getModuleCode(const uint32_t id) const {
109109
return ret;
110110
}
111111

112-
void JSIndexedRAMBundle::readBundle(char* buffer, const std::streamsize bytes)
113-
const {
112+
void JSIndexedRAMBundle::readBundle(char* buffer, std::streamsize bytes) const {
114113
if (!m_bundle->read(buffer, bytes)) {
115114
if ((m_bundle->rdstate() & std::ios::eofbit) != 0) {
116115
throw std::ios_base::failure("Unexpected end of RAM Bundle file");

packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ void JSIExecutor::loadBundle(
168168
ReactMarker::logTaggedMarker(
169169
ReactMarker::RUN_JS_BUNDLE_START, scriptName.c_str());
170170
}
171-
runtime_->evaluateJavaScript(
172-
std::make_unique<BigStringBuffer>(std::move(script)), sourceURL);
171+
runtime_->evaluateJavaScript(std::move(script), sourceURL);
173172
flush();
174173
if (hasLogger) {
175174
ReactMarker::logTaggedMarker(
@@ -212,7 +211,7 @@ void JSIExecutor::registerBundle(
212211
"Empty bundle registered with ID " + tag + " from " + bundlePath);
213212
}
214213
runtime_->evaluateJavaScript(
215-
std::make_unique<BigStringBuffer>(std::move(script)),
214+
std::move(script),
216215
JSExecutor::getSyntheticBundlePath(bundleId, bundlePath));
217216
}
218217
ReactMarker::logTaggedMarker(

packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ namespace facebook::react {
4949
using JSIScopedTimeoutInvoker =
5050
std::function<void(const std::function<void()> &invokee, std::function<std::string()> errorMessageProducer)>;
5151

52-
class BigStringBuffer : public jsi::Buffer {
52+
class [[deprecated("JSBigString implements jsi::Buffer directly")]] BigStringBuffer : public jsi::Buffer {
5353
public:
5454
BigStringBuffer(std::unique_ptr<const JSBigString> script) : script_(std::move(script)) {}
5555

@@ -60,7 +60,7 @@ class BigStringBuffer : public jsi::Buffer {
6060

6161
const uint8_t *data() const override
6262
{
63-
return reinterpret_cast<const uint8_t *>(script_->c_str());
63+
return script_->data();
6464
}
6565

6666
private:

packages/react-native/ReactCommon/react/runtime/ReactInstance.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ void ReactInstance::loadScript(
221221
const std::string& sourceURL,
222222
std::function<void(jsi::Runtime& runtime)>&& beforeLoad,
223223
std::function<void(jsi::Runtime& runtime)>&& afterLoad) {
224-
auto buffer = std::make_shared<BigStringBuffer>(std::move(script));
224+
std::shared_ptr<const jsi::Buffer> buffer(std::move(script));
225225
std::string scriptName = simpleBasename(sourceURL);
226226

227227
runtimeScheduler_->scheduleWork([this,
@@ -232,7 +232,7 @@ void ReactInstance::loadScript(
232232
std::weak_ptr<BufferedRuntimeExecutor>(
233233
bufferedRuntimeExecutor_),
234234
beforeLoad,
235-
afterLoad](jsi::Runtime& runtime) {
235+
afterLoad](jsi::Runtime& runtime) mutable {
236236
if (beforeLoad) {
237237
beforeLoad(runtime);
238238
}
@@ -357,7 +357,6 @@ void ReactInstance::registerSegment(
357357
throw std::invalid_argument(
358358
"Empty segment registered with ID " + tag + " from " + segmentPath);
359359
}
360-
auto buffer = std::make_shared<BigStringBuffer>(std::move(script));
361360

362361
bool hasLogger(ReactMarker::logTaggedMarkerBridgelessImpl != nullptr);
363362
if (hasLogger) {
@@ -369,7 +368,8 @@ void ReactInstance::registerSegment(
369368
#pragma clang diagnostic push
370369
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
371370
runtime.evaluateJavaScript(
372-
buffer, JSExecutor::getSyntheticBundlePath(segmentId, segmentPath));
371+
std::move(script),
372+
JSExecutor::getSyntheticBundlePath(segmentId, segmentPath));
373373
#pragma clang diagnostic pop
374374
LOG(WARNING) << "Finished evaluating segment " << segmentId
375375
<< " in ReactInstance::registerSegment";

0 commit comments

Comments
 (0)