OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/factory.h" | 7 #include "src/factory.h" |
8 #include "src/handles-inl.h" | 8 #include "src/handles-inl.h" |
9 #include "src/interpreter/constant-array-builder.h" | 9 #include "src/interpreter/constant-array-builder.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 static const size_t k8BitCapacity = ConstantArrayBuilder::k8BitCapacity; | 22 static const size_t k8BitCapacity = ConstantArrayBuilder::k8BitCapacity; |
23 static const size_t k16BitCapacity = ConstantArrayBuilder::k16BitCapacity; | 23 static const size_t k16BitCapacity = ConstantArrayBuilder::k16BitCapacity; |
24 }; | 24 }; |
25 | 25 |
26 STATIC_CONST_MEMBER_DEFINITION const size_t | 26 STATIC_CONST_MEMBER_DEFINITION const size_t |
27 ConstantArrayBuilderTest::k16BitCapacity; | 27 ConstantArrayBuilderTest::k16BitCapacity; |
28 STATIC_CONST_MEMBER_DEFINITION const size_t | 28 STATIC_CONST_MEMBER_DEFINITION const size_t |
29 ConstantArrayBuilderTest::k8BitCapacity; | 29 ConstantArrayBuilderTest::k8BitCapacity; |
30 | 30 |
31 TEST_F(ConstantArrayBuilderTest, AllocateAllEntries) { | 31 TEST_F(ConstantArrayBuilderTest, AllocateAllEntries) { |
| 32 CanonicalHandleScope canonical(isolate()); |
32 ConstantArrayBuilder builder(isolate(), zone()); | 33 ConstantArrayBuilder builder(isolate(), zone()); |
33 for (size_t i = 0; i < k16BitCapacity; i++) { | 34 for (size_t i = 0; i < k16BitCapacity; i++) { |
34 builder.Insert(handle(Smi::FromInt(static_cast<int>(i)), isolate())); | 35 builder.Insert(handle(Smi::FromInt(static_cast<int>(i)), isolate())); |
35 } | 36 } |
36 CHECK_EQ(builder.size(), k16BitCapacity); | 37 CHECK_EQ(builder.size(), k16BitCapacity); |
37 for (size_t i = 0; i < k16BitCapacity; i++) { | 38 for (size_t i = 0; i < k16BitCapacity; i++) { |
38 CHECK_EQ(Handle<Smi>::cast(builder.At(i))->value(), i); | 39 CHECK_EQ(Handle<Smi>::cast(builder.At(i))->value(), i); |
39 } | 40 } |
40 } | 41 } |
41 | 42 |
42 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx8Reservations) { | 43 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx8Reservations) { |
| 44 CanonicalHandleScope canonical(isolate()); |
43 for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) { | 45 for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) { |
44 ConstantArrayBuilder builder(isolate(), zone()); | 46 ConstantArrayBuilder builder(isolate(), zone()); |
45 for (size_t i = 0; i < reserved; i++) { | 47 for (size_t i = 0; i < reserved; i++) { |
46 OperandSize operand_size = builder.CreateReservedEntry(); | 48 OperandSize operand_size = builder.CreateReservedEntry(); |
47 CHECK(operand_size == OperandSize::kByte); | 49 CHECK(operand_size == OperandSize::kByte); |
48 } | 50 } |
49 for (size_t i = 0; i < 2 * k8BitCapacity; i++) { | 51 for (size_t i = 0; i < 2 * k8BitCapacity; i++) { |
50 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 52 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
51 builder.Insert(object); | 53 builder.Insert(object); |
52 if (i + reserved < k8BitCapacity) { | 54 if (i + reserved < k8BitCapacity) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 isolate()->factory()->NewNumberFromSize(2 * k8BitCapacity + i); | 104 isolate()->factory()->NewNumberFromSize(2 * k8BitCapacity + i); |
103 size_t index = builder.CommitReservedEntry(OperandSize::kByte, object); | 105 size_t index = builder.CommitReservedEntry(OperandSize::kByte, object); |
104 CHECK_EQ(static_cast<int>(index), k8BitCapacity - reserved + i); | 106 CHECK_EQ(static_cast<int>(index), k8BitCapacity - reserved + i); |
105 CHECK(builder.At(static_cast<int>(index))->SameValue(*object)); | 107 CHECK(builder.At(static_cast<int>(index))->SameValue(*object)); |
106 } | 108 } |
107 CHECK_EQ(builder.size(), 2 * k8BitCapacity + reserved); | 109 CHECK_EQ(builder.size(), 2 * k8BitCapacity + reserved); |
108 } | 110 } |
109 } | 111 } |
110 | 112 |
111 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithWideReservations) { | 113 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithWideReservations) { |
| 114 CanonicalHandleScope canonical(isolate()); |
112 for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) { | 115 for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) { |
113 ConstantArrayBuilder builder(isolate(), zone()); | 116 ConstantArrayBuilder builder(isolate(), zone()); |
114 for (size_t i = 0; i < k8BitCapacity; i++) { | 117 for (size_t i = 0; i < k8BitCapacity; i++) { |
115 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 118 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
116 builder.Insert(object); | 119 builder.Insert(object); |
117 CHECK(builder.At(i)->SameValue(*object)); | 120 CHECK(builder.At(i)->SameValue(*object)); |
118 CHECK_EQ(builder.size(), i + 1); | 121 CHECK_EQ(builder.size(), i + 1); |
119 } | 122 } |
120 for (size_t i = 0; i < reserved; i++) { | 123 for (size_t i = 0; i < reserved; i++) { |
121 OperandSize operand_size = builder.CreateReservedEntry(); | 124 OperandSize operand_size = builder.CreateReservedEntry(); |
(...skipping 16 matching lines...) Expand all Loading... |
138 CHECK(operand_size == OperandSize::kShort); | 141 CHECK(operand_size == OperandSize::kShort); |
139 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 142 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
140 builder.CommitReservedEntry(operand_size, object); | 143 builder.CommitReservedEntry(operand_size, object); |
141 CHECK_EQ(builder.size(), i + 1); | 144 CHECK_EQ(builder.size(), i + 1); |
142 } | 145 } |
143 } | 146 } |
144 } | 147 } |
145 | 148 |
146 | 149 |
147 TEST_F(ConstantArrayBuilderTest, ToFixedArray) { | 150 TEST_F(ConstantArrayBuilderTest, ToFixedArray) { |
| 151 CanonicalHandleScope canonical(isolate()); |
148 ConstantArrayBuilder builder(isolate(), zone()); | 152 ConstantArrayBuilder builder(isolate(), zone()); |
149 static const size_t kNumberOfElements = 37; | 153 static const size_t kNumberOfElements = 37; |
150 for (size_t i = 0; i < kNumberOfElements; i++) { | 154 for (size_t i = 0; i < kNumberOfElements; i++) { |
151 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 155 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
152 builder.Insert(object); | 156 builder.Insert(object); |
153 CHECK(builder.At(i)->SameValue(*object)); | 157 CHECK(builder.At(i)->SameValue(*object)); |
154 } | 158 } |
155 Handle<FixedArray> constant_array = builder.ToFixedArray(); | 159 Handle<FixedArray> constant_array = builder.ToFixedArray(); |
156 CHECK_EQ(constant_array->length(), kNumberOfElements); | 160 CHECK_EQ(constant_array->length(), kNumberOfElements); |
157 for (size_t i = 0; i < kNumberOfElements; i++) { | 161 for (size_t i = 0; i < kNumberOfElements; i++) { |
158 CHECK(constant_array->get(static_cast<int>(i))->SameValue(*builder.At(i))); | 162 CHECK(constant_array->get(static_cast<int>(i))->SameValue(*builder.At(i))); |
159 } | 163 } |
160 } | 164 } |
161 | 165 |
162 TEST_F(ConstantArrayBuilderTest, ToLargeFixedArray) { | 166 TEST_F(ConstantArrayBuilderTest, ToLargeFixedArray) { |
| 167 CanonicalHandleScope canonical(isolate()); |
163 ConstantArrayBuilder builder(isolate(), zone()); | 168 ConstantArrayBuilder builder(isolate(), zone()); |
164 static const size_t kNumberOfElements = 37373; | 169 static const size_t kNumberOfElements = 37373; |
165 for (size_t i = 0; i < kNumberOfElements; i++) { | 170 for (size_t i = 0; i < kNumberOfElements; i++) { |
166 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 171 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
167 builder.Insert(object); | 172 builder.Insert(object); |
168 CHECK(builder.At(i)->SameValue(*object)); | 173 CHECK(builder.At(i)->SameValue(*object)); |
169 } | 174 } |
170 Handle<FixedArray> constant_array = builder.ToFixedArray(); | 175 Handle<FixedArray> constant_array = builder.ToFixedArray(); |
171 CHECK_EQ(constant_array->length(), kNumberOfElements); | 176 CHECK_EQ(constant_array->length(), kNumberOfElements); |
172 for (size_t i = 0; i < kNumberOfElements; i++) { | 177 for (size_t i = 0; i < kNumberOfElements; i++) { |
173 CHECK(constant_array->get(static_cast<int>(i))->SameValue(*builder.At(i))); | 178 CHECK(constant_array->get(static_cast<int>(i))->SameValue(*builder.At(i))); |
174 } | 179 } |
175 } | 180 } |
176 | 181 |
177 TEST_F(ConstantArrayBuilderTest, GapFilledWhenLowReservationCommitted) { | 182 TEST_F(ConstantArrayBuilderTest, GapFilledWhenLowReservationCommitted) { |
| 183 CanonicalHandleScope canonical(isolate()); |
178 ConstantArrayBuilder builder(isolate(), zone()); | 184 ConstantArrayBuilder builder(isolate(), zone()); |
179 for (size_t i = 0; i < k8BitCapacity; i++) { | 185 for (size_t i = 0; i < k8BitCapacity; i++) { |
180 OperandSize operand_size = builder.CreateReservedEntry(); | 186 OperandSize operand_size = builder.CreateReservedEntry(); |
181 CHECK(OperandSize::kByte == operand_size); | 187 CHECK(OperandSize::kByte == operand_size); |
182 CHECK_EQ(builder.size(), 0); | 188 CHECK_EQ(builder.size(), 0); |
183 } | 189 } |
184 for (size_t i = 0; i < k8BitCapacity; i++) { | 190 for (size_t i = 0; i < k8BitCapacity; i++) { |
185 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 191 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
186 builder.Insert(object); | 192 builder.Insert(object); |
187 CHECK_EQ(builder.size(), i + k8BitCapacity + 1); | 193 CHECK_EQ(builder.size(), i + k8BitCapacity + 1); |
188 } | 194 } |
189 for (size_t i = 0; i < k8BitCapacity; i++) { | 195 for (size_t i = 0; i < k8BitCapacity; i++) { |
190 builder.CommitReservedEntry(OperandSize::kByte, | 196 builder.CommitReservedEntry(OperandSize::kByte, |
191 builder.At(i + k8BitCapacity)); | 197 builder.At(i + k8BitCapacity)); |
192 CHECK_EQ(builder.size(), 2 * k8BitCapacity); | 198 CHECK_EQ(builder.size(), 2 * k8BitCapacity); |
193 } | 199 } |
194 for (size_t i = 0; i < k8BitCapacity; i++) { | 200 for (size_t i = 0; i < k8BitCapacity; i++) { |
195 Handle<Object> original = builder.At(k8BitCapacity + i); | 201 Handle<Object> original = builder.At(k8BitCapacity + i); |
196 Handle<Object> duplicate = builder.At(i); | 202 Handle<Object> duplicate = builder.At(i); |
197 CHECK(original->SameValue(*duplicate)); | 203 CHECK(original->SameValue(*duplicate)); |
198 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); | 204 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); |
199 CHECK(original->SameValue(*reference)); | 205 CHECK(original->SameValue(*reference)); |
200 } | 206 } |
201 } | 207 } |
202 | 208 |
203 TEST_F(ConstantArrayBuilderTest, GapNotFilledWhenLowReservationDiscarded) { | 209 TEST_F(ConstantArrayBuilderTest, GapNotFilledWhenLowReservationDiscarded) { |
| 210 CanonicalHandleScope canonical(isolate()); |
204 ConstantArrayBuilder builder(isolate(), zone()); | 211 ConstantArrayBuilder builder(isolate(), zone()); |
205 for (size_t i = 0; i < k8BitCapacity; i++) { | 212 for (size_t i = 0; i < k8BitCapacity; i++) { |
206 OperandSize operand_size = builder.CreateReservedEntry(); | 213 OperandSize operand_size = builder.CreateReservedEntry(); |
207 CHECK(OperandSize::kByte == operand_size); | 214 CHECK(OperandSize::kByte == operand_size); |
208 CHECK_EQ(builder.size(), 0); | 215 CHECK_EQ(builder.size(), 0); |
209 } | 216 } |
210 for (size_t i = 0; i < k8BitCapacity; i++) { | 217 for (size_t i = 0; i < k8BitCapacity; i++) { |
211 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); | 218 Handle<Object> object = isolate()->factory()->NewNumberFromSize(i); |
212 builder.Insert(object); | 219 builder.Insert(object); |
213 CHECK_EQ(builder.size(), i + k8BitCapacity + 1); | 220 CHECK_EQ(builder.size(), i + k8BitCapacity + 1); |
214 } | 221 } |
215 for (size_t i = 0; i < k8BitCapacity; i++) { | 222 for (size_t i = 0; i < k8BitCapacity; i++) { |
216 builder.DiscardReservedEntry(OperandSize::kByte); | 223 builder.DiscardReservedEntry(OperandSize::kByte); |
217 builder.Insert(builder.At(i + k8BitCapacity)); | 224 builder.Insert(builder.At(i + k8BitCapacity)); |
218 CHECK_EQ(builder.size(), 2 * k8BitCapacity); | 225 CHECK_EQ(builder.size(), 2 * k8BitCapacity); |
219 } | 226 } |
220 for (size_t i = 0; i < k8BitCapacity; i++) { | 227 for (size_t i = 0; i < k8BitCapacity; i++) { |
221 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); | 228 Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i); |
222 Handle<Object> original = builder.At(k8BitCapacity + i); | 229 Handle<Object> original = builder.At(k8BitCapacity + i); |
223 CHECK(original->SameValue(*reference)); | 230 CHECK(original->SameValue(*reference)); |
224 Handle<Object> duplicate = builder.At(i); | 231 Handle<Object> duplicate = builder.At(i); |
225 CHECK(duplicate->SameValue(*isolate()->factory()->the_hole_value())); | 232 CHECK(duplicate->SameValue(*isolate()->factory()->the_hole_value())); |
226 } | 233 } |
227 } | 234 } |
228 | 235 |
229 TEST_F(ConstantArrayBuilderTest, HolesWithUnusedReservations) { | 236 TEST_F(ConstantArrayBuilderTest, HolesWithUnusedReservations) { |
| 237 CanonicalHandleScope canonical(isolate()); |
230 static int kNumberOfHoles = 128; | 238 static int kNumberOfHoles = 128; |
231 ConstantArrayBuilder builder(isolate(), zone()); | 239 ConstantArrayBuilder builder(isolate(), zone()); |
232 for (int i = 0; i < kNumberOfHoles; ++i) { | 240 for (int i = 0; i < kNumberOfHoles; ++i) { |
233 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kByte); | 241 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kByte); |
234 } | 242 } |
235 for (int i = 0; i < 128; ++i) { | 243 for (int i = 0; i < 128; ++i) { |
236 CHECK_EQ(builder.Insert(isolate()->factory()->NewNumber(i)), i); | 244 CHECK_EQ(builder.Insert(isolate()->factory()->NewNumber(i)), i); |
237 } | 245 } |
238 CHECK_EQ(builder.Insert(isolate()->factory()->NewNumber(256)), 256); | 246 CHECK_EQ(builder.Insert(isolate()->factory()->NewNumber(256)), 256); |
239 | 247 |
240 Handle<FixedArray> constant_array = builder.ToFixedArray(); | 248 Handle<FixedArray> constant_array = builder.ToFixedArray(); |
241 CHECK_EQ(constant_array->length(), 257); | 249 CHECK_EQ(constant_array->length(), 257); |
242 for (int i = 128; i < 256; i++) { | 250 for (int i = 128; i < 256; i++) { |
243 CHECK(constant_array->get(i)->SameValue( | 251 CHECK(constant_array->get(i)->SameValue( |
244 *isolate()->factory()->the_hole_value())); | 252 *isolate()->factory()->the_hole_value())); |
245 } | 253 } |
246 CHECK(!constant_array->get(127)->SameValue( | 254 CHECK(!constant_array->get(127)->SameValue( |
247 *isolate()->factory()->the_hole_value())); | 255 *isolate()->factory()->the_hole_value())); |
248 CHECK(!constant_array->get(256)->SameValue( | 256 CHECK(!constant_array->get(256)->SameValue( |
249 *isolate()->factory()->the_hole_value())); | 257 *isolate()->factory()->the_hole_value())); |
250 } | 258 } |
251 | 259 |
252 TEST_F(ConstantArrayBuilderTest, ReservationsAtAllScales) { | 260 TEST_F(ConstantArrayBuilderTest, ReservationsAtAllScales) { |
| 261 CanonicalHandleScope canonical(isolate()); |
253 ConstantArrayBuilder builder(isolate(), zone()); | 262 ConstantArrayBuilder builder(isolate(), zone()); |
254 for (int i = 0; i < 256; i++) { | 263 for (int i = 0; i < 256; i++) { |
255 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kByte); | 264 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kByte); |
256 } | 265 } |
257 for (int i = 256; i < 65536; ++i) { | 266 for (int i = 256; i < 65536; ++i) { |
258 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kShort); | 267 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kShort); |
259 } | 268 } |
260 for (int i = 65536; i < 131072; ++i) { | 269 for (int i = 65536; i < 131072; ++i) { |
261 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kQuad); | 270 CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kQuad); |
262 } | 271 } |
(...skipping 14 matching lines...) Expand all Loading... |
277 if (i == 0 || i == 256 || i == 65536) { | 286 if (i == 0 || i == 256 || i == 65536) { |
278 expected = isolate()->factory()->NewNumber(count++); | 287 expected = isolate()->factory()->NewNumber(count++); |
279 } else { | 288 } else { |
280 expected = isolate()->factory()->the_hole_value(); | 289 expected = isolate()->factory()->the_hole_value(); |
281 } | 290 } |
282 CHECK(constant_array->get(i)->SameValue(*expected)); | 291 CHECK(constant_array->get(i)->SameValue(*expected)); |
283 } | 292 } |
284 } | 293 } |
285 | 294 |
286 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithFixedReservations) { | 295 TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithFixedReservations) { |
| 296 CanonicalHandleScope canonical(isolate()); |
287 ConstantArrayBuilder builder(isolate(), zone()); | 297 ConstantArrayBuilder builder(isolate(), zone()); |
288 for (size_t i = 0; i < k16BitCapacity; i++) { | 298 for (size_t i = 0; i < k16BitCapacity; i++) { |
289 if ((i % 2) == 0) { | 299 if ((i % 2) == 0) { |
290 CHECK_EQ(i, builder.AllocateEntry()); | 300 CHECK_EQ(i, builder.AllocateEntry()); |
291 } else { | 301 } else { |
292 builder.Insert(handle(Smi::FromInt(static_cast<int>(i)), isolate())); | 302 builder.Insert(handle(Smi::FromInt(static_cast<int>(i)), isolate())); |
293 } | 303 } |
294 } | 304 } |
295 CHECK_EQ(builder.size(), k16BitCapacity); | 305 CHECK_EQ(builder.size(), k16BitCapacity); |
296 | 306 |
(...skipping 16 matching lines...) Expand all Loading... |
313 | 323 |
314 // Check values after reserved entries are inserted. | 324 // Check values after reserved entries are inserted. |
315 for (size_t i = 0; i < k16BitCapacity; i++) { | 325 for (size_t i = 0; i < k16BitCapacity; i++) { |
316 CHECK_EQ(Handle<Smi>::cast(builder.At(i))->value(), i); | 326 CHECK_EQ(Handle<Smi>::cast(builder.At(i))->value(), i); |
317 } | 327 } |
318 } | 328 } |
319 | 329 |
320 } // namespace interpreter | 330 } // namespace interpreter |
321 } // namespace internal | 331 } // namespace internal |
322 } // namespace v8 | 332 } // namespace v8 |
OLD | NEW |