Reviewers: Dmitry Lomov (chromium), Ken Russell,

Description:
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
[email protected],[email protected]
LOG=y

Please review this at https://codereview.chromium.org/1084513002/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+20, -13 lines):
  M include/v8.h
  M src/api.cc
  M test/cctest/test-typedarrays.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index d94e9b32b854826ba221abb3948f057fb74a9a26..908c0e19e36c206915cc596bbdc5daa691c32c17 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -3511,6 +3511,15 @@ class V8_EXPORT ArrayBufferView : public Object {
    */
   size_t CopyContents(void* dest, size_t byte_length);

+  /**
+ * Returns true if ArrayBufferView::Buffer() is a no-op. If it returns false, + * getting the buffer might incur allocating a backing store and materializing
+   * the contents. In that case, CopyContents can be used to access the
+   * underlying contents of this ArrayBufferView without materializing the
+   * buffer first.
+   */
+  bool HasBuffer() const;
+
   V8_INLINE static ArrayBufferView* Cast(Value* obj);

   static const int kInternalFieldCount =
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 733214ec076712b31538853fe24fcc03162f80b9..9d4356c5140777598b0c67ebc3d293fc3bcacc37 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -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());
Index: test/cctest/test-typedarrays.cc
diff --git a/test/cctest/test-typedarrays.cc b/test/cctest/test-typedarrays.cc index dc1bca2e0a241b4e4d02e92f2a783e4793b92591..966edb720ee5b7c03b97e3d2e919209505558f0d 100644
--- a/test/cctest/test-typedarrays.cc
+++ b/test/cctest/test-typedarrays.cc
@@ -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]);
   }


--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to