2 * Copyright (C) 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #ifndef ArrayBufferView_h
27 #define ArrayBufferView_h
29 #include "ArrayBuffer.h"
30 #include "ExceptionCode.h"
34 #include <wtf/PassRefPtr.h>
35 #include <wtf/RefCounted.h>
36 #include <wtf/RefPtr.h>
40 class ArrayBufferView : public RefCounted<ArrayBufferView> {
42 virtual bool isByteArray() const { return false; }
43 virtual bool isUnsignedByteArray() const { return false; }
44 virtual bool isShortArray() const { return false; }
45 virtual bool isUnsignedShortArray() const { return false; }
46 virtual bool isIntArray() const { return false; }
47 virtual bool isUnsignedIntArray() const { return false; }
48 virtual bool isFloatArray() const { return false; }
49 virtual bool isDoubleArray() const { return false; }
50 virtual bool isDataView() const { return false; }
52 PassRefPtr<ArrayBuffer> buffer() const
57 void* baseAddress() const
62 unsigned byteOffset() const
67 virtual unsigned byteLength() const = 0;
69 virtual ~ArrayBufferView();
72 ArrayBufferView(PassRefPtr<ArrayBuffer> buffer, unsigned byteOffset);
74 void setImpl(ArrayBufferView* array, unsigned byteOffset, ExceptionCode& ec);
76 void setRangeImpl(const char* data, size_t dataByteLength, unsigned byteOffset, ExceptionCode& ec);
78 void zeroRangeImpl(unsigned byteOffset, size_t rangeByteLength, ExceptionCode& ec);
80 static void calculateOffsetAndLength(int start, int end, unsigned arraySize,
81 unsigned* offset, unsigned* length);
83 // Helper to verify that a given sub-range of an ArrayBuffer is
86 static bool verifySubRange(PassRefPtr<ArrayBuffer> buffer,
92 if (sizeof(T) > 1 && byteOffset % sizeof(T))
94 if (byteOffset > buffer->byteLength())
96 unsigned remainingElements = (buffer->byteLength() - byteOffset) / sizeof(T);
97 if (numElements > remainingElements)
102 // Input offset is in number of elements from this array's view;
103 // output offset is in number of bytes from the underlying buffer's view.
104 template <typename T>
105 static void clampOffsetAndNumElements(PassRefPtr<ArrayBuffer> buffer,
106 unsigned arrayByteOffset,
108 unsigned *numElements)
110 unsigned maxOffset = (UINT_MAX - arrayByteOffset) / sizeof(T);
111 if (*offset > maxOffset) {
112 *offset = buffer->byteLength();
116 *offset = arrayByteOffset + *offset * sizeof(T);
117 *offset = std::min(buffer->byteLength(), *offset);
118 unsigned remainingElements = (buffer->byteLength() - *offset) / sizeof(T);
119 *numElements = std::min(remainingElements, *numElements);
122 // This is the address of the ArrayBuffer's storage, plus the byte offset.
125 unsigned m_byteOffset;
128 RefPtr<ArrayBuffer> m_buffer;
131 } // namespace WebCore
133 #endif // ArrayBufferView_h