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/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;
};