1/*
2 * Copyright (C) 2012-2018 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#pragma once
27
28#include "IndexingHeader.h"
29#include "IndexingType.h"
30#include "PropertyStorage.h"
31#include <wtf/Gigacage.h>
32#include <wtf/Noncopyable.h>
33
34namespace JSC {
35
36class VM;
37class CopyVisitor;
38class GCDeferralContext;
39struct ArrayStorage;
40
41template <typename T>
42struct ContiguousData {
43 ContiguousData() = default;
44 ContiguousData(T* data, size_t length)
45 : m_data(data)
46#if !ASSERT_DISABLED
47 , m_length(length)
48#endif
49 {
50 UNUSED_PARAM(length);
51 }
52
53 struct Data {
54 Data(T& location, IndexingType indexingMode)
55 : m_data(location)
56#if !ASSERT_DISABLED
57 , m_isWritable(!isCopyOnWrite(indexingMode))
58#endif
59 {
60 UNUSED_PARAM(indexingMode);
61 }
62
63 explicit operator bool() const { return !!m_data.get(); }
64
65 const T& operator=(const T& value)
66 {
67 ASSERT(m_isWritable);
68 m_data = value;
69 return value;
70 }
71
72 operator const T&() const { return m_data; }
73
74 // WriteBarrier forwarded methods.
75
76 void set(VM& vm, const JSCell* owner, const JSValue& value)
77 {
78 ASSERT(m_isWritable);
79 m_data.set(vm, owner, value);
80 }
81
82 void setWithoutWriteBarrier(const JSValue& value)
83 {
84 ASSERT(m_isWritable);
85 m_data.setWithoutWriteBarrier(value);
86 }
87
88 void setStartingValue(JSValue value)
89 {
90 m_data.setStartingValue(value);
91 }
92
93 void clear()
94 {
95 ASSERT(m_isWritable);
96 m_data.clear();
97 }
98
99 JSValue get() const
100 {
101 return m_data.get();
102 }
103
104
105 T& m_data;
106#if !ASSERT_DISABLED
107 bool m_isWritable;
108#endif
109 };
110
111 const Data at(const JSCell* owner, size_t index) const;
112 Data at(const JSCell* owner, size_t index);
113
114 T& atUnsafe(size_t index) { ASSERT(index < m_length); return m_data[index]; }
115
116 T* data() const { return m_data; }
117#if !ASSERT_DISABLED
118 size_t length() const { return m_length; }
119#endif
120
121private:
122 T* m_data { nullptr };
123#if !ASSERT_DISABLED
124 size_t m_length { 0 };
125#endif
126};
127
128using ContiguousDoubles = ContiguousData<double>;
129using ContiguousJSValues = ContiguousData<WriteBarrier<Unknown>>;
130using ConstContiguousDoubles = ContiguousData<const double>;
131using ConstContiguousJSValues = ContiguousData<const WriteBarrier<Unknown>>;
132
133class Butterfly {
134 WTF_MAKE_NONCOPYABLE(Butterfly);
135private:
136 Butterfly() { } // Not instantiable.
137public:
138
139 static size_t totalSize(size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes)
140 {
141 ASSERT(indexingPayloadSizeInBytes ? hasIndexingHeader : true);
142 ASSERT(sizeof(EncodedJSValue) == sizeof(IndexingHeader));
143 return (preCapacity + propertyCapacity) * sizeof(EncodedJSValue) + (hasIndexingHeader ? sizeof(IndexingHeader) : 0) + indexingPayloadSizeInBytes;
144 }
145
146 static Butterfly* fromBase(void* base, size_t preCapacity, size_t propertyCapacity)
147 {
148 return reinterpret_cast<Butterfly*>(static_cast<EncodedJSValue*>(base) + preCapacity + propertyCapacity + 1);
149 }
150
151 ALWAYS_INLINE static unsigned availableContiguousVectorLength(size_t propertyCapacity, unsigned vectorLength);
152 static unsigned availableContiguousVectorLength(Structure*, unsigned vectorLength);
153
154 ALWAYS_INLINE static unsigned optimalContiguousVectorLength(size_t propertyCapacity, unsigned vectorLength);
155 static unsigned optimalContiguousVectorLength(Structure*, unsigned vectorLength);
156
157 // This method is here not just because it's handy, but to remind you that
158 // the whole point of butterflies is to do evil pointer arithmetic.
159 static Butterfly* fromPointer(char* ptr)
160 {
161 return reinterpret_cast<Butterfly*>(ptr);
162 }
163
164 char* pointer() { return reinterpret_cast<char*>(this); }
165
166 static ptrdiff_t offsetOfIndexingHeader() { return IndexingHeader::offsetOfIndexingHeader(); }
167 static ptrdiff_t offsetOfArrayBuffer() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfArrayBuffer(); }
168 static ptrdiff_t offsetOfPublicLength() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfPublicLength(); }
169 static ptrdiff_t offsetOfVectorLength() { return offsetOfIndexingHeader() + IndexingHeader::offsetOfVectorLength(); }
170
171 static Butterfly* tryCreateUninitialized(VM&, JSObject* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes, GCDeferralContext* = nullptr);
172 static Butterfly* createUninitialized(VM&, JSObject* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, size_t indexingPayloadSizeInBytes);
173
174 static Butterfly* tryCreate(VM& vm, JSObject*, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader& indexingHeader, size_t indexingPayloadSizeInBytes);
175 static Butterfly* create(VM&, JSObject* intendedOwner, size_t preCapacity, size_t propertyCapacity, bool hasIndexingHeader, const IndexingHeader&, size_t indexingPayloadSizeInBytes);
176 static Butterfly* create(VM&, JSObject* intendedOwner, Structure*);
177
178 IndexingHeader* indexingHeader() { return IndexingHeader::from(this); }
179 const IndexingHeader* indexingHeader() const { return IndexingHeader::from(this); }
180 PropertyStorage propertyStorage() { return indexingHeader()->propertyStorage(); }
181 ConstPropertyStorage propertyStorage() const { return indexingHeader()->propertyStorage(); }
182
183 uint32_t publicLength() const { return indexingHeader()->publicLength(); }
184 uint32_t vectorLength() const { return indexingHeader()->vectorLength(); }
185 void setPublicLength(uint32_t value) { indexingHeader()->setPublicLength(value); }
186 void setVectorLength(uint32_t value) { indexingHeader()->setVectorLength(value); }
187
188 template<typename T>
189 T* indexingPayload() { return reinterpret_cast_ptr<T*>(this); }
190 ArrayStorage* arrayStorage() { return indexingPayload<ArrayStorage>(); }
191 ContiguousJSValues contiguousInt32() { return ContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); }
192 ContiguousDoubles contiguousDouble() { return ContiguousDoubles(indexingPayload<double>(), vectorLength()); }
193 ContiguousJSValues contiguous() { return ContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); }
194
195 template<typename T>
196 const T* indexingPayload() const { return reinterpret_cast_ptr<const T*>(this); }
197 const ArrayStorage* arrayStorage() const { return indexingPayload<ArrayStorage>(); }
198 ConstContiguousJSValues contiguousInt32() const { return ConstContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); }
199 ConstContiguousDoubles contiguousDouble() const { return ConstContiguousDoubles(indexingPayload<double>(), vectorLength()); }
200 ConstContiguousJSValues contiguous() const { return ConstContiguousJSValues(indexingPayload<WriteBarrier<Unknown>>(), vectorLength()); }
201
202 static Butterfly* fromContiguous(WriteBarrier<Unknown>* contiguous)
203 {
204 return reinterpret_cast<Butterfly*>(contiguous);
205 }
206 static Butterfly* fromContiguous(double* contiguous)
207 {
208 return reinterpret_cast<Butterfly*>(contiguous);
209 }
210
211 static ptrdiff_t offsetOfPropertyStorage() { return -static_cast<ptrdiff_t>(sizeof(IndexingHeader)); }
212 static int indexOfPropertyStorage()
213 {
214 ASSERT(sizeof(IndexingHeader) == sizeof(EncodedJSValue));
215 return -1;
216 }
217
218 void* base(size_t preCapacity, size_t propertyCapacity) { return propertyStorage() - propertyCapacity - preCapacity; }
219 void* base(Structure*);
220
221 static Butterfly* createOrGrowArrayRight(
222 Butterfly*, VM&, JSObject* intendedOwner, Structure* oldStructure,
223 size_t propertyCapacity, bool hadIndexingHeader,
224 size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes);
225
226 // The butterfly reallocation methods perform the reallocation itself but do not change any
227 // of the meta-data to reflect that the reallocation occurred. Note that this set of
228 // methods is not exhaustive and is not intended to encapsulate all possible allocation
229 // modes of butterflies - there are code paths that allocate butterflies by calling
230 // directly into Heap::tryAllocateStorage.
231 static Butterfly* createOrGrowPropertyStorage(Butterfly*, VM&, JSObject* intendedOwner, Structure*, size_t oldPropertyCapacity, size_t newPropertyCapacity);
232 Butterfly* growArrayRight(VM&, JSObject* intendedOwner, Structure* oldStructure, size_t propertyCapacity, bool hadIndexingHeader, size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); // Assumes that preCapacity is zero, and asserts as much.
233 Butterfly* growArrayRight(VM&, JSObject* intendedOwner, Structure*, size_t newIndexingPayloadSizeInBytes);
234
235 Butterfly* reallocArrayRightIfPossible(VM&, GCDeferralContext&, JSObject* intendedOwner, Structure* oldStructure, size_t propertyCapacity, bool hadIndexingHeader, size_t oldIndexingPayloadSizeInBytes, size_t newIndexingPayloadSizeInBytes); // Assumes that preCapacity is zero, and asserts as much.
236
237 Butterfly* resizeArray(VM&, JSObject* intendedOwner, size_t propertyCapacity, bool oldHasIndexingHeader, size_t oldIndexingPayloadSizeInBytes, size_t newPreCapacity, bool newHasIndexingHeader, size_t newIndexingPayloadSizeInBytes);
238 Butterfly* resizeArray(VM&, JSObject* intendedOwner, Structure*, size_t newPreCapacity, size_t newIndexingPayloadSizeInBytes); // Assumes that you're not changing whether or not the object has an indexing header.
239 Butterfly* unshift(Structure*, size_t numberOfSlots);
240 Butterfly* shift(Structure*, size_t numberOfSlots);
241};
242
243} // namespace JSC
244