Expose ArrayBufferView::HasBuffer

This allows the embedder to decide whether it's worthwhile to copy the
contents to avoid materializing a buffer.

BUG=v8:3996
R=dslomov@chromium.org,kbr@chromium.org
LOG=y

Review URL: https://codereview.chromium.org/1084513002

Cr-Commit-Position: refs/heads/master@{#27782}
This commit is contained in:
jochen 2015-04-13 04:28:02 -07:00 committed by Commit bot
parent 021f738127
commit ada32ae636
3 changed files with 17 additions and 13 deletions

View File

@ -3511,6 +3511,12 @@ class V8_EXPORT ArrayBufferView : public Object {
*/
size_t CopyContents(void* dest, size_t byte_length);
/**
* Returns true if ArrayBufferView's backing ArrayBuffer has already been
* allocated.
*/
bool HasBuffer() const;
V8_INLINE static ArrayBufferView* Cast(Value* obj);
static const int kInternalFieldCount =

View File

@ -6471,6 +6471,15 @@ size_t v8::ArrayBufferView::CopyContents(void* dest, size_t byte_length) {
}
bool v8::ArrayBufferView::HasBuffer() const {
i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
if (obj->IsJSDataView()) return true;
DCHECK(obj->IsJSTypedArray());
i::Handle<i::JSTypedArray> typed_array(i::JSTypedArray::cast(*obj));
return !typed_array->buffer()->IsSmi();
}
size_t v8::ArrayBufferView::ByteOffset() {
i::Handle<i::JSArrayBufferView> obj = Utils::OpenHandle(this);
return static_cast<size_t>(obj->byte_offset()->Number());

View File

@ -19,22 +19,11 @@ void TestArrayBufferViewContents(LocalContext& env, bool should_use_buffer) {
CHECK(obj_a->IsArrayBufferView());
v8::Local<v8::ArrayBufferView> array_buffer_view =
v8::Local<v8::ArrayBufferView>::Cast(obj_a);
Handle<JSArrayBufferView> internal_view(
v8::Utils::OpenHandle(*array_buffer_view));
bool has_buffer = true;
if (internal_view->IsJSTypedArray()) {
Handle<JSTypedArray> typed_array(JSTypedArray::cast(*internal_view));
has_buffer = !typed_array->buffer()->IsSmi();
}
CHECK_EQ(has_buffer, should_use_buffer);
CHECK_EQ(array_buffer_view->HasBuffer(), should_use_buffer);
unsigned char contents[4] = {23, 23, 23, 23};
CHECK_EQ(sizeof(contents),
array_buffer_view->CopyContents(contents, sizeof(contents)));
if (!has_buffer) {
CHECK(internal_view->IsJSTypedArray());
Handle<JSTypedArray> typed_array(JSTypedArray::cast(*internal_view));
CHECK(typed_array->buffer()->IsSmi());
}
CHECK_EQ(array_buffer_view->HasBuffer(), should_use_buffer);
for (size_t i = 0; i < sizeof(contents); ++i) {
CHECK_EQ(i, contents[i]);
}