]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSArrayBufferView.h
JavaScriptCore-7601.1.46.3.tar.gz
[apple/javascriptcore.git] / runtime / JSArrayBufferView.h
CommitLineData
81345200
A
1/*
2 * Copyright (C) 2013 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
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.
24 */
25
26#ifndef JSArrayBufferView_h
27#define JSArrayBufferView_h
28
29#include "JSObject.h"
30
31namespace JSC {
32
33// This class serves two purposes:
34//
35// 1) It provides those parts of JSGenericTypedArrayView that don't depend
36// on template parameters.
37//
38// 2) It represents the DOM/WebCore-visible "JSArrayBufferView" type, which
39// C++ code uses when it wants to pass around a view of an array buffer
40// without concern for the actual type of the view.
41//
42// These two roles are quite different. (1) is just a matter of optimizing
43// compile and link times by having as much code and data as possible not
44// be subject to template specialization. (2) is *almost* a matter of
45// semantics; indeed at the very least it is a matter of obeying a contract
46// that we have with WebCore right now.
47//
48// One convenient thing that saves us from too much crazy is that
49// ArrayBufferView is not instantiable.
50
51// Typed array views have different modes depending on how big they are and
52// whether the user has done anything that requires a separate backing
53// buffer or the DOM-specified neutering capabilities.
54enum TypedArrayMode {
55 // Small and fast typed array. B is unused, V points to a vector
56 // allocated in copied space, and M = FastTypedArray. V's liveness is
57 // determined entirely by the view's liveness.
58 FastTypedArray,
59
60 // A large typed array that still attempts not to waste too much
61 // memory. B is initialized to point to a slot that could hold a
62 // buffer pointer, V points to a vector allocated using fastCalloc(),
63 // and M = OversizeTypedArray. V's liveness is determined entirely by
64 // the view's liveness, and the view will add a finalizer to delete V.
65 OversizeTypedArray,
66
67 // A typed array that was used in some crazy way. B's IndexingHeader
68 // is hijacked to contain a reference to the native array buffer. The
69 // native typed array view points back to the JS view. V points to a
70 // vector allocated using who-knows-what, and M = WastefulTypedArray.
71 // The view does not own the vector.
72 WastefulTypedArray,
73
74 // A data view. B is unused, V points to a vector allocated using who-
75 // knows-what, and M = DataViewMode. The view does not own the vector.
76 // There is an extra field (in JSDataView) that points to the
77 // ArrayBuffer.
78 DataViewMode
79};
80
81inline bool hasArrayBuffer(TypedArrayMode mode)
82{
83 return mode >= WastefulTypedArray;
84}
85
86// When WebCore uses a JSArrayBufferView, it expects to be able to get the native
87// ArrayBuffer and little else. This requires slowing down and wasting memory,
88// and then accessing things via the Butterfly. When JS uses a JSArrayBufferView
89// it is always via the usual methods in the MethodTable, so this class's
90// implementation of those has no need to even exist - we could at any time sink
91// code into JSGenericTypedArrayView if it was convenient.
92
93class JSArrayBufferView : public JSNonFinalObject {
94public:
95 typedef JSNonFinalObject Base;
ed1e77d3 96 static const unsigned StructureFlags = Base::StructureFlags | OverridesGetPropertyNames | OverridesGetOwnPropertySlot;
81345200
A
97
98 static const unsigned fastSizeLimit = 1000;
99
100 static size_t sizeOf(uint32_t length, uint32_t elementSize)
101 {
102 return (length * elementSize + sizeof(EncodedJSValue) - 1)
103 & ~(sizeof(EncodedJSValue) - 1);
104 }
105
106 static size_t allocationSize(size_t inlineCapacity)
107 {
108 ASSERT_UNUSED(inlineCapacity, !inlineCapacity);
109 return sizeof(JSArrayBufferView);
110 }
111
112protected:
113 class ConstructionContext {
114 WTF_MAKE_NONCOPYABLE(ConstructionContext);
115
116 public:
117 enum InitializationMode { ZeroFill, DontInitialize };
118
119 JS_EXPORT_PRIVATE ConstructionContext(VM&, Structure*, uint32_t length, uint32_t elementSize, InitializationMode = ZeroFill);
120
121 JS_EXPORT_PRIVATE ConstructionContext(
122 VM&, Structure*, PassRefPtr<ArrayBuffer>,
123 unsigned byteOffset, unsigned length);
124
125 enum DataViewTag { DataView };
126 ConstructionContext(
127 Structure*, PassRefPtr<ArrayBuffer>,
128 unsigned byteOffset, unsigned length, DataViewTag);
129
130 bool operator!() const { return !m_structure; }
131
132 Structure* structure() const { return m_structure; }
133 void* vector() const { return m_vector; }
134 uint32_t length() const { return m_length; }
135 TypedArrayMode mode() const { return m_mode; }
136 Butterfly* butterfly() const { return m_butterfly; }
137
138 private:
139 Structure* m_structure;
140 void* m_vector;
141 uint32_t m_length;
142 TypedArrayMode m_mode;
143 Butterfly* m_butterfly;
144 };
145
146 JS_EXPORT_PRIVATE JSArrayBufferView(VM&, ConstructionContext&);
147 JS_EXPORT_PRIVATE void finishCreation(VM&);
148
149 static bool getOwnPropertySlot(JSObject*, ExecState*, PropertyName, PropertySlot&);
150 static void put(JSCell*, ExecState*, PropertyName, JSValue, PutPropertySlot&);
151 static bool defineOwnProperty(JSObject*, ExecState*, PropertyName, const PropertyDescriptor&, bool shouldThrow);
152 static bool deleteProperty(JSCell*, ExecState*, PropertyName);
153
154 static void getOwnNonIndexPropertyNames(JSObject*, ExecState*, PropertyNameArray&, EnumerationMode);
155
156public:
157 TypedArrayMode mode() const { return m_mode; }
158 bool hasArrayBuffer() const { return JSC::hasArrayBuffer(mode()); }
159
160 ArrayBuffer* buffer();
161 PassRefPtr<ArrayBufferView> impl();
162 void neuter();
163
164 void* vector() { return m_vector; }
165 unsigned byteOffset();
166 unsigned length() const { return m_length; }
167
168 DECLARE_EXPORT_INFO;
169
170 static ptrdiff_t offsetOfVector() { return OBJECT_OFFSETOF(JSArrayBufferView, m_vector); }
171 static ptrdiff_t offsetOfLength() { return OBJECT_OFFSETOF(JSArrayBufferView, m_length); }
172 static ptrdiff_t offsetOfMode() { return OBJECT_OFFSETOF(JSArrayBufferView, m_mode); }
173
174private:
175 static void finalize(JSCell*);
176
177protected:
81345200
A
178 ArrayBuffer* existingBufferInButterfly();
179
180 void* m_vector;
181 uint32_t m_length;
182 TypedArrayMode m_mode;
183};
184
185} // namespace JSC
186
187#endif // JSArrayBufferView_h
188