]>
git.saurik.com Git - apple/javascriptcore.git/blob - runtime/ArrayBufferView.h
2 * Copyright (C) 2009, 2013 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 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 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 "TypedArrayType.h"
33 #include <wtf/PassRefPtr.h>
34 #include <wtf/RefCounted.h>
35 #include <wtf/RefPtr.h>
39 class JSArrayBufferView
;
43 class ArrayBufferView
: public RefCounted
<ArrayBufferView
> {
45 virtual TypedArrayType
getType() const = 0;
47 bool isNeutered() const
49 return !m_buffer
|| m_buffer
->isNeutered();
52 PassRefPtr
<ArrayBuffer
> buffer() const
59 void* baseAddress() const
66 unsigned byteOffset() const
73 virtual unsigned byteLength() const = 0;
75 JS_EXPORT_PRIVATE
void setNeuterable(bool flag
);
76 bool isNeuterable() const { return m_isNeuterable
; }
78 JS_EXPORT_PRIVATE
virtual ~ArrayBufferView();
80 // Helper to verify that a given sub-range of an ArrayBuffer is
82 // FIXME: This should distinguish between alignment errors and bounds errors.
83 // https://bugs.webkit.org/show_bug.cgi?id=125391
85 static bool verifySubRange(
86 PassRefPtr
<ArrayBuffer
> buffer
,
90 unsigned byteLength
= buffer
->byteLength();
91 if (sizeof(T
) > 1 && byteOffset
% sizeof(T
))
93 if (byteOffset
> byteLength
)
95 unsigned remainingElements
= (byteLength
- byteOffset
) / sizeof(T
);
96 if (numElements
> remainingElements
)
101 virtual JSArrayBufferView
* wrap(ExecState
*, JSGlobalObject
*) = 0;
104 JS_EXPORT_PRIVATE
ArrayBufferView(PassRefPtr
<ArrayBuffer
>, unsigned byteOffset
);
106 inline bool setImpl(ArrayBufferView
*, unsigned byteOffset
);
108 inline bool setRangeImpl(const char* data
, size_t dataByteLength
, unsigned byteOffset
);
110 inline bool zeroRangeImpl(unsigned byteOffset
, size_t rangeByteLength
);
112 static inline void calculateOffsetAndLength(
113 int start
, int end
, unsigned arraySize
,
114 unsigned* offset
, unsigned* length
);
116 // Input offset is in number of elements from this array's view;
117 // output offset is in number of bytes from the underlying buffer's view.
118 template <typename T
>
119 static void clampOffsetAndNumElements(
120 PassRefPtr
<ArrayBuffer
> buffer
,
121 unsigned arrayByteOffset
,
123 unsigned *numElements
)
125 unsigned maxOffset
= (UINT_MAX
- arrayByteOffset
) / sizeof(T
);
126 if (*offset
> maxOffset
) {
127 *offset
= buffer
->byteLength();
131 *offset
= arrayByteOffset
+ *offset
* sizeof(T
);
132 *offset
= std::min(buffer
->byteLength(), *offset
);
133 unsigned remainingElements
= (buffer
->byteLength() - *offset
) / sizeof(T
);
134 *numElements
= std::min(remainingElements
, *numElements
);
137 // This is the address of the ArrayBuffer's storage, plus the byte offset.
140 unsigned m_byteOffset
: 31;
141 bool m_isNeuterable
: 1;
144 friend class ArrayBuffer
;
145 RefPtr
<ArrayBuffer
> m_buffer
;
148 bool ArrayBufferView::setImpl(ArrayBufferView
* array
, unsigned byteOffset
)
150 if (byteOffset
> byteLength()
151 || byteOffset
+ array
->byteLength() > byteLength()
152 || byteOffset
+ array
->byteLength() < byteOffset
) {
153 // Out of range offset or overflow
157 char* base
= static_cast<char*>(baseAddress());
158 memmove(base
+ byteOffset
, array
->baseAddress(), array
->byteLength());
162 bool ArrayBufferView::setRangeImpl(const char* data
, size_t dataByteLength
, unsigned byteOffset
)
164 if (byteOffset
> byteLength()
165 || byteOffset
+ dataByteLength
> byteLength()
166 || byteOffset
+ dataByteLength
< byteOffset
) {
167 // Out of range offset or overflow
171 char* base
= static_cast<char*>(baseAddress());
172 memmove(base
+ byteOffset
, data
, dataByteLength
);
176 bool ArrayBufferView::zeroRangeImpl(unsigned byteOffset
, size_t rangeByteLength
)
178 if (byteOffset
> byteLength()
179 || byteOffset
+ rangeByteLength
> byteLength()
180 || byteOffset
+ rangeByteLength
< byteOffset
) {
181 // Out of range offset or overflow
185 char* base
= static_cast<char*>(baseAddress());
186 memset(base
+ byteOffset
, 0, rangeByteLength
);
190 void ArrayBufferView::calculateOffsetAndLength(
191 int start
, int end
, unsigned arraySize
, unsigned* offset
, unsigned* length
)
201 if (static_cast<unsigned>(end
) > arraySize
)
205 *offset
= static_cast<unsigned>(start
);
206 *length
= static_cast<unsigned>(end
- start
);
211 using JSC::ArrayBufferView
;
213 #endif // ArrayBufferView_h