Changeset 97893 in webkit


Ignore:
Timestamp:
Oct 19, 2011 4:31:12 PM (13 years ago)
Author:
commit-queue@webkit.org
Message:

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

Patch by Shinya Kawanaka <shinyak@google.com> 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.
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r97890 r97893  
     12011-10-19  Shinya Kawanaka  <shinyak@google.com>
     2
     3        ArrayBuffer should have slice method.
     4        https://bugs.webkit.org/show_bug.cgi?id=66646
     5
     6        Reviewed by Kenneth Russell.
     7
     8        * fast/canvas/webgl/array-unit-tests.html: Aded.
     9
    1102011-10-19  Dirk Pranke  <dpranke@chromium.org>
    211
  • trunk/LayoutTests/fast/canvas/webgl/array-unit-tests.html

    r97881 r97893  
    5555  } else {
    5656    debug("TEST FAILED");
     57  }
     58}
     59
     60var byteLength;
     61var subBuffer;
     62function testSlice() {
     63  function test(subBuf, starts, size) {
     64    byteLength = size;
     65    subBuffer = eval(subBuf);
     66    var subArray = new Int8Array(subBuffer);
     67    assertEq(subBuf, subBuffer.byteLength, byteLength);
     68    for (var i = 0; i < size; ++i)
     69      assertEq('Element ' + i, starts + i, subArray[i]);
     70  }
     71
     72  try {
     73    var buffer = new ArrayBuffer(32);
     74    var array = new Int8Array(buffer);
     75    for (var i = 0; i < 32; ++i)
     76      array[i] = i;
     77
     78    test("buffer.slice(0)", 0, 32);
     79    test("buffer.slice(16)", 16, 16);
     80    test("buffer.slice(24)", 24, 8);
     81    test("buffer.slice(32)", 32, 0);
     82    test("buffer.slice(40)", 32, 0);
     83    test("buffer.slice(80)", 32, 0);
     84
     85    test("buffer.slice(-8)", 24, 8);
     86    test("buffer.slice(-16)", 16, 16);
     87    test("buffer.slice(-24)", 8, 24);
     88    test("buffer.slice(-32)", 0, 32);
     89    test("buffer.slice(-40)", 0, 32);
     90    test("buffer.slice(-80)", 0, 32);
     91
     92    test("buffer.slice(0, 32)", 0, 32);
     93    test("buffer.slice(0, 16)", 0, 16);
     94    test("buffer.slice(8, 24)", 8, 16);
     95    test("buffer.slice(16, 32)", 16, 16);
     96    test("buffer.slice(24, 16)", 24, 0);
     97
     98    test("buffer.slice(16, -8)", 16, 8);
     99    test("buffer.slice(-20, 30)", 12, 18);
     100
     101    test("buffer.slice(-8, -20)", 24, 0);
     102    test("buffer.slice(-20, -8)", 12, 12);
     103    test("buffer.slice(-40, 16)", 0, 16);
     104    test("buffer.slice(-40, 40)", 0, 32);
     105  } catch (e) {
     106    fail(e);
    57107  }
    58108}
     
    825875function runTests() {
    826876  allPassed = true;
     877
     878  testSlice();
    827879
    828880  // The "name" attribute is a concession to browsers which don't
  • trunk/Source/WebCore/ChangeLog

    r97892 r97893  
     12011-10-19  Shinya Kawanaka  <shinyak@google.com>
     2
     3        ArrayBuffer should have slice method.
     4        https://bugs.webkit.org/show_bug.cgi?id=66646
     5
     6        Reviewed by Kenneth Russell.
     7
     8        * html/canvas/ArrayBuffer.cpp:
     9        (WebCore::clampValue):
     10        (WebCore::ArrayBuffer::slice):
     11        (WebCore::ArrayBuffer::sliceImpl):
     12        (WebCore::ArrayBuffer::clampIndex):
     13        * html/canvas/ArrayBuffer.h: Added declaration.
     14        * html/canvas/ArrayBuffer.idl: Added interface.
     15
    1162011-10-19  Mark Hahnenberg  <mhahnenberg@apple.com>
    217
  • trunk/Source/WebCore/html/canvas/ArrayBuffer.cpp

    r91300 r97893  
    3030
    3131namespace WebCore {
     32
     33static int clampValue(int x, int left, int right)
     34{
     35    ASSERT(left <= right);
     36    if (x < left)
     37        x = left;
     38    if (right < x)
     39        x = right;
     40    return x;
     41}
    3242
    3343PassRefPtr<ArrayBuffer> ArrayBuffer::create(unsigned numElements, unsigned elementByteSize)
     
    7585}
    7686
     87PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin, int end) const
     88{
     89    return sliceImpl(clampIndex(begin), clampIndex(end));
     90}
     91
     92PassRefPtr<ArrayBuffer> ArrayBuffer::slice(int begin) const
     93{
     94    return sliceImpl(clampIndex(begin), byteLength());
     95}
     96
     97PassRefPtr<ArrayBuffer> ArrayBuffer::sliceImpl(unsigned begin, unsigned end) const
     98{
     99    unsigned size = begin <= end ? end - begin : 0;
     100    return ArrayBuffer::create(static_cast<const char*>(data()) + begin, size);
     101}
     102
     103unsigned ArrayBuffer::clampIndex(int index) const
     104{
     105    unsigned currentLength = byteLength();
     106    if (index < 0)
     107        index = currentLength + index;
     108    return clampValue(index, 0, currentLength);
     109}
     110
    77111ArrayBuffer::~ArrayBuffer()
    78112{
  • trunk/Source/WebCore/html/canvas/ArrayBuffer.h

    r91300 r97893  
    4242    unsigned byteLength() const;
    4343
     44    PassRefPtr<ArrayBuffer> slice(int begin, int end) const;
     45    PassRefPtr<ArrayBuffer> slice(int begin) const;
     46
    4447    ~ArrayBuffer();
    4548
     
    4851    ArrayBuffer(unsigned numElements, unsigned elementByteSize);
    4952    static void* tryAllocate(unsigned numElements, unsigned elementByteSize);
     53    PassRefPtr<ArrayBuffer> sliceImpl(unsigned begin, unsigned end) const;
     54    unsigned clampIndex(int index) const;
     55
    5056    unsigned m_sizeInBytes;
    5157    void* m_data;
  • trunk/Source/WebCore/html/canvas/ArrayBuffer.idl

    r96788 r97893  
    3333    ] ArrayBuffer {
    3434        readonly attribute int byteLength;
     35        ArrayBuffer slice(in long begin, in [Optional] long end);
    3536    };
    3637
Note: See TracChangeset for help on using the changeset viewer.