diff --git a/include/v8-array-buffer.h b/include/v8-array-buffer.h index e9047b79ce..bab840f82c 100644 --- a/include/v8-array-buffer.h +++ b/include/v8-array-buffer.h @@ -256,6 +256,12 @@ class V8_EXPORT ArrayBuffer : public Object { */ std::shared_ptr GetBackingStore(); + /** + * More efficient shortcut for GetBackingStore()->Data(). The returned pointer + * is valid as long as the ArrayBuffer is alive. + */ + void* Data() const; + V8_INLINE static ArrayBuffer* Cast(Value* value) { #ifdef V8_ENABLE_CHECKS CheckCast(value); @@ -414,6 +420,12 @@ class V8_EXPORT SharedArrayBuffer : public Object { */ std::shared_ptr GetBackingStore(); + /** + * More efficient shortcut for GetBackingStore()->Data(). The returned pointer + * is valid as long as the ArrayBuffer is alive. + */ + void* Data() const; + V8_INLINE static SharedArrayBuffer* Cast(Value* value) { #ifdef V8_ENABLE_CHECKS CheckCast(value); diff --git a/src/api/api.cc b/src/api/api.cc index 6b4424dea5..53a248d452 100644 --- a/src/api/api.cc +++ b/src/api/api.cc @@ -4109,6 +4109,11 @@ std::shared_ptr v8::ArrayBuffer::GetBackingStore() { return std::static_pointer_cast(bs_base); } +void* v8::ArrayBuffer::Data() const { + i::Handle self = Utils::OpenHandle(this); + return self->backing_store(); +} + std::shared_ptr v8::SharedArrayBuffer::GetBackingStore() { i::Handle self = Utils::OpenHandle(this); std::shared_ptr backing_store = self->GetBackingStore(); @@ -4119,6 +4124,11 @@ std::shared_ptr v8::SharedArrayBuffer::GetBackingStore() { return std::static_pointer_cast(bs_base); } +void* v8::SharedArrayBuffer::Data() const { + i::Handle self = Utils::OpenHandle(this); + return self->backing_store(); +} + void v8::ArrayBuffer::CheckCast(Value* that) { i::Handle obj = Utils::OpenHandle(that); Utils::ApiCheck( diff --git a/test/cctest/test-api-array-buffer.cc b/test/cctest/test-api-array-buffer.cc index d472ebcf32..b087274b31 100644 --- a/test/cctest/test-api-array-buffer.cc +++ b/test/cctest/test-api-array-buffer.cc @@ -366,6 +366,7 @@ THREADED_TEST(SkipArrayBufferBackingStoreDuringGC) { // Should not move the pointer CHECK_EQ(ab->GetBackingStore()->Data(), store_ptr); + CHECK_EQ(ab->Data(), store_ptr); CcTest::array_buffer_allocator()->Free(buffer, 100); } @@ -394,8 +395,8 @@ THREADED_TEST(SkipArrayBufferDuringScavenge) { CcTest::CollectGarbage(i::NEW_SPACE); // in survivor space now CcTest::CollectGarbage(i::NEW_SPACE); // in old gen now - // Use `ab` to silence compiler warning CHECK_EQ(ab->GetBackingStore()->Data(), store_ptr); + CHECK_EQ(ab->Data(), store_ptr); } THREADED_TEST(Regress1006600) { @@ -418,6 +419,7 @@ THREADED_TEST(ArrayBuffer_NewBackingStore) { CHECK(!backing_store->IsShared()); Local ab = v8::ArrayBuffer::New(isolate, backing_store); CHECK_EQ(backing_store.get(), ab->GetBackingStore().get()); + CHECK_EQ(backing_store->Data(), ab->Data()); } THREADED_TEST(SharedArrayBuffer_NewBackingStore) { @@ -430,6 +432,7 @@ THREADED_TEST(SharedArrayBuffer_NewBackingStore) { Local ab = v8::SharedArrayBuffer::New(isolate, backing_store); CHECK_EQ(backing_store.get(), ab->GetBackingStore().get()); + CHECK_EQ(backing_store->Data(), ab->Data()); } static void* backing_store_custom_data = nullptr;