Skip to content

Commit cdabd7b

Browse files
tsaichienfacebook-github-bot
authored andcommitted
Add tryGetMutableBuffer API (#56156)
Summary: Adds `ArrayBuffer::tryGetMutableBuffer` to JSI. This allows the user to retrieve a `MutableBuffer` from the `ArrayBuffer`. The returned buffer will point directly into the same buffer as the runtime object, rather than copying it out. The data referencedby the `MutableBuffer` will be alive during the entirety of the `MutableBuffer` lifespan. Addresses: facebook/hermes#1578 Changelog: [Internal] Reviewed By: tmikov Differential Revision: D91264901
1 parent 107ad55 commit cdabd7b

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

packages/react-native/ReactCommon/jsi/jsi/decorator.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,11 @@ class RuntimeDecorator : public Base, private jsi::Instrumentation {
434434
return plain_.getRuntimeDataImpl(uuid);
435435
}
436436

437+
std::shared_ptr<MutableBuffer> tryGetMutableBuffer(
438+
const jsi::ArrayBuffer& arrayBuffer) override {
439+
return plain_.tryGetMutableBuffer(arrayBuffer);
440+
}
441+
437442
// Private data for managing scopes.
438443
Runtime::ScopeState* pushScope() override {
439444
return plain_.pushScope();
@@ -1000,6 +1005,12 @@ class WithRuntimeDecorator : public RuntimeDecorator<Plain, Base> {
10001005
return RD::callAsConstructor(f, args, count);
10011006
}
10021007

1008+
std::shared_ptr<MutableBuffer> tryGetMutableBuffer(
1009+
const jsi::ArrayBuffer& arrayBuffer) override {
1010+
Around around{with_};
1011+
return RD::tryGetMutableBuffer(arrayBuffer);
1012+
}
1013+
10031014
// Private data for managing scopes.
10041015
Runtime::ScopeState* pushScope() override {
10051016
Around around{with_};

packages/react-native/ReactCommon/jsi/jsi/jsi.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,11 @@ void Runtime::setPropertyValue(
552552
}
553553
}
554554

555+
std::shared_ptr<MutableBuffer> Runtime::tryGetMutableBuffer(
556+
const jsi::ArrayBuffer&) {
557+
return nullptr;
558+
}
559+
555560
Pointer& Pointer::operator=(Pointer&& other) noexcept {
556561
if (ptr_) {
557562
ptr_->invalidate();

packages/react-native/ReactCommon/jsi/jsi/jsi.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,14 @@ class JSI_EXPORT IRuntime : public ICast {
628628
void* ctx,
629629
void (*cb)(void* ctx, bool ascii, const void* data, size_t num)) = 0;
630630

631+
/// If possible, returns the MutableBuffer representing \p arrayBuffer's
632+
/// underlying data, else return a nullptr. Importantly, the returned
633+
/// MutableBuffer directly points to \p arrayBuffer's data instead of copying
634+
/// the data over. The data's lifetime is valid for the lifetime of
635+
/// MutableBuffer, which is orthogonal from \p arrayBuffer.
636+
virtual std::shared_ptr<MutableBuffer> tryGetMutableBuffer(
637+
const jsi::ArrayBuffer& arrayBuffer) = 0;
638+
631639
protected:
632640
virtual ~IRuntime() = default;
633641
};
@@ -713,6 +721,9 @@ class JSI_EXPORT Runtime : public IRuntime {
713721

714722
size_t push(const Array&, const Value*, size_t) override;
715723

724+
std::shared_ptr<MutableBuffer> tryGetMutableBuffer(
725+
const jsi::ArrayBuffer& arrayBuffer) override;
726+
716727
protected:
717728
friend class Pointer;
718729
friend class PropNameID;
@@ -1456,6 +1467,10 @@ class JSI_EXPORT ArrayBuffer : public Object {
14561467
return runtime.data(*this);
14571468
}
14581469

1470+
std::shared_ptr<MutableBuffer> tryGetMutableBuffer(IRuntime& runtime) const {
1471+
return runtime.tryGetMutableBuffer(*this);
1472+
}
1473+
14591474
private:
14601475
friend class Object;
14611476
friend class Value;

0 commit comments

Comments
 (0)