Title: [97893] trunk
Revision
97893
Author
[email protected]
Date
2011-10-19 16:31:12 -0700 (Wed, 19 Oct 2011)

Log Message

ArrayBuffer should have slice method.
https://bugs.webkit.org/show_bug.cgi?id=66646

Patch by Shinya Kawanaka <[email protected]> on 2011-10-19
Reviewed by Kenneth Russell.

Source/WebCore:

* html/canvas/ArrayBuffer.cpp:
(WebCore::clampValue):
(WebCore::ArrayBuffer::slice):
(WebCore::ArrayBuffer::sliceImpl):
(WebCore::ArrayBuffer::clampIndex):
* html/canvas/ArrayBuffer.h: Added declaration.
* html/canvas/ArrayBuffer.idl: Added interface.

LayoutTests:

* fast/canvas/webgl/array-unit-tests.html: Aded.

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (97892 => 97893)


--- trunk/LayoutTests/ChangeLog	2011-10-19 23:27:47 UTC (rev 97892)
+++ trunk/LayoutTests/ChangeLog	2011-10-19 23:31:12 UTC (rev 97893)
@@ -1,3 +1,12 @@
+2011-10-19  Shinya Kawanaka  <[email protected]>
+
+        ArrayBuffer should have slice method.
+        https://bugs.webkit.org/show_bug.cgi?id=66646
+
+        Reviewed by Kenneth Russell.
+
+        * fast/canvas/webgl/array-unit-tests.html: Aded.
+
 2011-10-19  Dirk Pranke  <[email protected]>
 
         Unreviewed, rolling out r97850.

Modified: trunk/LayoutTests/fast/canvas/webgl/array-unit-tests.html (97892 => 97893)


--- trunk/LayoutTests/fast/canvas/webgl/array-unit-tests.html	2011-10-19 23:27:47 UTC (rev 97892)
+++ trunk/LayoutTests/fast/canvas/webgl/array-unit-tests.html	2011-10-19 23:31:12 UTC (rev 97893)
@@ -57,6 +57,56 @@
   }
 }
 
+var byteLength;
+var subBuffer;
+function testSlice() {
+  function test(subBuf, starts, size) {
+    byteLength = size;
+    subBuffer = eval(subBuf);
+    var subArray = new Int8Array(subBuffer);
+    assertEq(subBuf, subBuffer.byteLength, byteLength);
+    for (var i = 0; i < size; ++i)
+      assertEq('Element ' + i, starts + i, subArray[i]);
+  }
+
+  try {
+    var buffer = new ArrayBuffer(32);
+    var array = new Int8Array(buffer);
+    for (var i = 0; i < 32; ++i)
+      array[i] = i;
+
+    test("buffer.slice(0)", 0, 32);
+    test("buffer.slice(16)", 16, 16);
+    test("buffer.slice(24)", 24, 8);
+    test("buffer.slice(32)", 32, 0);
+    test("buffer.slice(40)", 32, 0);
+    test("buffer.slice(80)", 32, 0);
+
+    test("buffer.slice(-8)", 24, 8);
+    test("buffer.slice(-16)", 16, 16);
+    test("buffer.slice(-24)", 8, 24);
+    test("buffer.slice(-32)", 0, 32);
+    test("buffer.slice(-40)", 0, 32);
+    test("buffer.slice(-80)", 0, 32);
+
+    test("buffer.slice(0, 32)", 0, 32);
+    test("buffer.slice(0, 16)", 0, 16);
+    test("buffer.slice(8, 24)", 8, 16);
+    test("buffer.slice(16, 32)", 16, 16);
+    test("buffer.slice(24, 16)", 24, 0);
+
+    test("buffer.slice(16, -8)", 16, 8);
+    test("buffer.slice(-20, 30)", 12, 18);
+
+    test("buffer.slice(-8, -20)", 24, 0);
+    test("buffer.slice(-20, -8)", 12, 12);
+    test("buffer.slice(-40, 16)", 0, 16);
+    test("buffer.slice(-40, 40)", 0, 32);
+  } catch (e) {
+    fail(e);
+  }
+}
+
 //
 // Tests for unsigned array variants
 //
@@ -825,6 +875,8 @@
 function runTests() {
   allPassed = true;
 
+  testSlice();
+
   // The "name" attribute is a concession to browsers which don't
   // implement the "name" property on function objects
   var testCases =

Modified: trunk/Source/WebCore/ChangeLog (97892 => 97893)


--- trunk/Source/WebCore/ChangeLog	2011-10-19 23:27:47 UTC (rev 97892)
+++ trunk/Source/WebCore/ChangeLog	2011-10-19 23:31:12 UTC (rev 97893)
@@ -1,3 +1,18 @@
+2011-10-19  Shinya Kawanaka  <[email protected]>
+
+        ArrayBuffer should have slice method.
+        https://bugs.webkit.org/show_bug.cgi?id=66646
+
+        Reviewed by Kenneth Russell.
+
+        * html/canvas/ArrayBuffer.cpp:
+        (WebCore::clampValue):
+        (WebCore::ArrayBuffer::slice):
+        (WebCore::ArrayBuffer::sliceImpl):
+        (WebCore::ArrayBuffer::clampIndex):
+        * html/canvas/ArrayBuffer.h: Added declaration.
+        * html/canvas/ArrayBuffer.idl: Added interface.
+
 2011-10-19  Mark Hahnenberg  <[email protected]>
 
         Add getConstructData to the MethodTable

Modified: trunk/Source/WebCore/html/canvas/ArrayBuffer.cpp (97892 => 97893)


--- trunk/Source/WebCore/html/canvas/ArrayBuffer.cpp	2011-10-19 23:27:47 UTC (rev 97892)
+++ trunk/Source/WebCore/html/canvas/ArrayBuffer.cpp	2011-10-19 23:31:12 UTC (rev 97893)
@@ -30,6 +30,16 @@
 
 namespace WebCore {
 
+static int clampValue(int x, int left, int right)
+{
+    ASSERT(left <= right);
+    if (x < left)
+        x = left;
+    if (right < x)
+        x = right;
+    return x;
+}
+
 PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize)
 {
     void* data = "" elementByteSize);
@@ -74,6 +84,30 @@
     return m_sizeInBytes;
 }
 
+PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const
+{
+    return sliceImpl(clampIndex(begin), clampIndex(end));
+}
+
+PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const
+{
+    return sliceImpl(clampIndex(begin), byteLength());
+}
+
+PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) const
+{
+    unsigned size = begin <= end ? end - begin : 0;
+    return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
+}
+
+unsigned ArrayBuffer::clampIndex(int index) const
+{
+    unsigned currentLength = byteLength();
+    if (index < 0)
+        index = currentLength + index;
+    return clampValue(index, 0, currentLength);
+}
+
 ArrayBuffer::~ArrayBuffer()
 {
     WTF::fastFree(m_data);

Modified: trunk/Source/WebCore/html/canvas/ArrayBuffer.h (97892 => 97893)


--- trunk/Source/WebCore/html/canvas/ArrayBuffer.h	2011-10-19 23:27:47 UTC (rev 97892)
+++ trunk/Source/WebCore/html/canvas/ArrayBuffer.h	2011-10-19 23:31:12 UTC (rev 97893)
@@ -41,12 +41,18 @@
     const void* data() const;
     unsigned byteLength() const;
 
+    PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
+    PassRefPtr<ArrayBuffer> slice(int begin) const;
+
     ~ArrayBuffer();
 
   private:
     ArrayBuffer(void* data, unsigned sizeInBytes);
     ArrayBuffer(unsigned numElements, unsigned elementByteSize);
     static void* tryAllocate(unsigned numElements, unsigned elementByteSize);
+    PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const;
+    unsigned clampIndex(int index) const;
+
     unsigned m_sizeInBytes;
     void* m_data;
 };

Modified: trunk/Source/WebCore/html/canvas/ArrayBuffer.idl (97892 => 97893)


--- trunk/Source/WebCore/html/canvas/ArrayBuffer.idl	2011-10-19 23:27:47 UTC (rev 97892)
+++ trunk/Source/WebCore/html/canvas/ArrayBuffer.idl	2011-10-19 23:31:12 UTC (rev 97893)
@@ -32,6 +32,7 @@
         NoStaticTables
     ] ArrayBuffer {
         readonly attribute int byteLength;
+        ArrayBuffer slice(in long begin, in [Optional] long end);
     };
 
 }
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to