2016-01-05 19:08:11 +00:00
|
|
|
// Copyright 2014 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/v8.h"
|
|
|
|
|
2017-02-10 17:22:45 +00:00
|
|
|
#include "src/ast/ast-value-factory.h"
|
2016-01-05 19:08:11 +00:00
|
|
|
#include "src/factory.h"
|
|
|
|
#include "src/handles-inl.h"
|
|
|
|
#include "src/interpreter/constant-array-builder.h"
|
|
|
|
#include "src/isolate.h"
|
2017-01-09 13:43:28 +00:00
|
|
|
#include "src/objects-inl.h"
|
2016-01-05 19:08:11 +00:00
|
|
|
#include "test/unittests/test-utils.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
namespace interpreter {
|
|
|
|
|
|
|
|
class ConstantArrayBuilderTest : public TestWithIsolateAndZone {
|
|
|
|
public:
|
|
|
|
ConstantArrayBuilderTest() {}
|
|
|
|
~ConstantArrayBuilderTest() override {}
|
|
|
|
|
2016-02-26 10:14:49 +00:00
|
|
|
static const size_t k8BitCapacity = ConstantArrayBuilder::k8BitCapacity;
|
|
|
|
static const size_t k16BitCapacity = ConstantArrayBuilder::k16BitCapacity;
|
2016-01-05 19:08:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
STATIC_CONST_MEMBER_DEFINITION const size_t
|
2016-02-26 10:14:49 +00:00
|
|
|
ConstantArrayBuilderTest::k16BitCapacity;
|
2016-01-05 19:08:11 +00:00
|
|
|
STATIC_CONST_MEMBER_DEFINITION const size_t
|
2016-02-26 10:14:49 +00:00
|
|
|
ConstantArrayBuilderTest::k8BitCapacity;
|
2016-01-05 19:08:11 +00:00
|
|
|
|
|
|
|
TEST_F(ConstantArrayBuilderTest, AllocateAllEntries) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k16BitCapacity; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
builder.Insert(ast_factory.NewNumber(i + 0.5));
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), k16BitCapacity);
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k16BitCapacity; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK_EQ(
|
|
|
|
Handle<HeapNumber>::cast(builder.At(i, isolate()).ToHandleChecked())
|
|
|
|
->value(),
|
|
|
|
i + 0.5);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 16:41:43 +00:00
|
|
|
TEST_F(ConstantArrayBuilderTest, ToFixedArray) {
|
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-08-10 16:41:43 +00:00
|
|
|
static const size_t kNumberOfElements = 37;
|
|
|
|
for (size_t i = 0; i < kNumberOfElements; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
const AstValue* value = ast_factory.NewNumber(i + 0.5);
|
|
|
|
builder.Insert(value);
|
|
|
|
ast_factory.Internalize(isolate());
|
|
|
|
CHECK(
|
|
|
|
builder.At(i, isolate()).ToHandleChecked()->SameValue(*value->value()));
|
2016-08-10 16:41:43 +00:00
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-11-11 12:12:31 +00:00
|
|
|
CHECK_EQ(constant_array->length(), static_cast<int>(kNumberOfElements));
|
2016-08-10 16:41:43 +00:00
|
|
|
for (size_t i = 0; i < kNumberOfElements; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK(constant_array->get(static_cast<int>(i))
|
|
|
|
->SameValue(*builder.At(i, isolate()).ToHandleChecked()));
|
2016-08-10 16:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConstantArrayBuilderTest, ToLargeFixedArray) {
|
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-08-10 16:41:43 +00:00
|
|
|
static const size_t kNumberOfElements = 37373;
|
|
|
|
for (size_t i = 0; i < kNumberOfElements; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
const AstValue* value = ast_factory.NewNumber(i + 0.5);
|
|
|
|
builder.Insert(value);
|
|
|
|
ast_factory.Internalize(isolate());
|
|
|
|
CHECK(
|
|
|
|
builder.At(i, isolate()).ToHandleChecked()->SameValue(*value->value()));
|
2016-08-10 16:41:43 +00:00
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-11-11 12:12:31 +00:00
|
|
|
CHECK_EQ(constant_array->length(), static_cast<int>(kNumberOfElements));
|
2016-08-10 16:41:43 +00:00
|
|
|
for (size_t i = 0; i < kNumberOfElements; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK(constant_array->get(static_cast<int>(i))
|
|
|
|
->SameValue(*builder.At(i, isolate()).ToHandleChecked()));
|
2016-08-10 16:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConstantArrayBuilderTest, ToLargeFixedArrayWithReservations) {
|
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-08-10 16:41:43 +00:00
|
|
|
static const size_t kNumberOfElements = 37373;
|
|
|
|
for (size_t i = 0; i < kNumberOfElements; i++) {
|
|
|
|
builder.CommitReservedEntry(builder.CreateReservedEntry(),
|
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-11-11 12:12:31 +00:00
|
|
|
CHECK_EQ(constant_array->length(), static_cast<int>(kNumberOfElements));
|
2016-08-10 16:41:43 +00:00
|
|
|
for (size_t i = 0; i < kNumberOfElements; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK(constant_array->get(static_cast<int>(i))
|
|
|
|
->SameValue(*builder.At(i, isolate()).ToHandleChecked()));
|
2016-08-10 16:41:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:08:11 +00:00
|
|
|
TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithIdx8Reservations) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) {
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-01-05 19:08:11 +00:00
|
|
|
for (size_t i = 0; i < reserved; i++) {
|
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(operand_size == OperandSize::kByte);
|
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < 2 * k8BitCapacity; i++) {
|
2016-08-10 16:41:43 +00:00
|
|
|
builder.CommitReservedEntry(builder.CreateReservedEntry(),
|
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-02-26 10:14:49 +00:00
|
|
|
if (i + reserved < k8BitCapacity) {
|
|
|
|
CHECK_LE(builder.size(), k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
CHECK_EQ(builder.size(), i + 1);
|
|
|
|
} else {
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_GE(builder.size(), k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
CHECK_EQ(builder.size(), i + reserved + 1);
|
|
|
|
}
|
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), 2 * k8BitCapacity + reserved);
|
2016-01-05 19:08:11 +00:00
|
|
|
|
2016-08-10 16:41:43 +00:00
|
|
|
// Commit reserved entries with duplicates and check size does not change.
|
2016-02-26 10:14:49 +00:00
|
|
|
DCHECK_EQ(reserved + 2 * k8BitCapacity, builder.size());
|
2016-01-05 19:08:11 +00:00
|
|
|
size_t duplicates_in_idx8_space =
|
2016-02-26 10:14:49 +00:00
|
|
|
std::min(reserved, k8BitCapacity - reserved);
|
2016-01-05 19:08:11 +00:00
|
|
|
for (size_t i = 0; i < duplicates_in_idx8_space; i++) {
|
|
|
|
builder.CommitReservedEntry(OperandSize::kByte,
|
2016-08-10 16:41:43 +00:00
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-02-26 10:14:49 +00:00
|
|
|
DCHECK_EQ(reserved + 2 * k8BitCapacity, builder.size());
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Now make reservations, and commit them with unique entries.
|
|
|
|
for (size_t i = 0; i < duplicates_in_idx8_space; i++) {
|
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(operand_size == OperandSize::kByte);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < duplicates_in_idx8_space; i++) {
|
2016-08-10 16:41:43 +00:00
|
|
|
Smi* value = Smi::FromInt(static_cast<int>(2 * k8BitCapacity + i));
|
|
|
|
size_t index = builder.CommitReservedEntry(OperandSize::kByte, value);
|
2016-11-10 11:25:17 +00:00
|
|
|
CHECK_EQ(index, k8BitCapacity - reserved + i);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2016-08-10 16:41:43 +00:00
|
|
|
|
2017-02-10 17:22:45 +00:00
|
|
|
// Clear any remaining uncommited reservations.
|
|
|
|
for (size_t i = 0; i < reserved - duplicates_in_idx8_space; i++) {
|
|
|
|
builder.DiscardReservedEntry(OperandSize::kByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-11-10 11:25:17 +00:00
|
|
|
CHECK_EQ(constant_array->length(),
|
|
|
|
static_cast<int>(2 * k8BitCapacity + reserved));
|
2016-08-10 16:41:43 +00:00
|
|
|
|
|
|
|
// Check all committed values match expected
|
|
|
|
for (size_t i = 0; i < k8BitCapacity - reserved; i++) {
|
|
|
|
Object* value = constant_array->get(static_cast<int>(i));
|
|
|
|
Smi* smi = Smi::FromInt(static_cast<int>(i));
|
|
|
|
CHECK(value->SameValue(smi));
|
|
|
|
}
|
|
|
|
for (size_t i = k8BitCapacity; i < 2 * k8BitCapacity + reserved; i++) {
|
|
|
|
Object* value = constant_array->get(static_cast<int>(i));
|
|
|
|
Smi* smi = Smi::FromInt(static_cast<int>(i - reserved));
|
|
|
|
CHECK(value->SameValue(smi));
|
|
|
|
}
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-21 17:08:21 +00:00
|
|
|
TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithWideReservations) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t reserved = 1; reserved < k8BitCapacity; reserved *= 3) {
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-08-10 16:41:43 +00:00
|
|
|
builder.CommitReservedEntry(builder.CreateReservedEntry(),
|
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-01-05 19:08:11 +00:00
|
|
|
CHECK_EQ(builder.size(), i + 1);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < reserved; i++) {
|
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(operand_size == OperandSize::kShort);
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < reserved; i++) {
|
|
|
|
builder.DiscardReservedEntry(OperandSize::kShort);
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
|
|
|
for (size_t i = 0; i < reserved; i++) {
|
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(operand_size == OperandSize::kShort);
|
2016-08-10 16:41:43 +00:00
|
|
|
builder.CommitReservedEntry(operand_size,
|
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = k8BitCapacity; i < k8BitCapacity + reserved; i++) {
|
2016-01-05 19:08:11 +00:00
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(operand_size == OperandSize::kShort);
|
2016-08-10 16:41:43 +00:00
|
|
|
builder.CommitReservedEntry(operand_size,
|
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-01-05 19:08:11 +00:00
|
|
|
CHECK_EQ(builder.size(), i + 1);
|
|
|
|
}
|
|
|
|
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-11-10 11:25:17 +00:00
|
|
|
CHECK_EQ(constant_array->length(),
|
|
|
|
static_cast<int>(k8BitCapacity + reserved));
|
2016-08-10 16:41:43 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity + reserved; i++) {
|
|
|
|
Object* value = constant_array->get(static_cast<int>(i));
|
|
|
|
CHECK(value->SameValue(*isolate()->factory()->NewNumberFromSize(i)));
|
|
|
|
}
|
2016-03-21 17:08:21 +00:00
|
|
|
}
|
|
|
|
}
|
2016-01-05 19:08:11 +00:00
|
|
|
|
|
|
|
TEST_F(ConstantArrayBuilderTest, GapFilledWhenLowReservationCommitted) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-01-05 19:08:11 +00:00
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(OperandSize::kByte == operand_size);
|
2016-11-14 10:48:26 +00:00
|
|
|
CHECK_EQ(builder.size(), 0u);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-08-10 16:41:43 +00:00
|
|
|
builder.CommitReservedEntry(builder.CreateReservedEntry(),
|
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), i + k8BitCapacity + 1);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-01-05 19:08:11 +00:00
|
|
|
builder.CommitReservedEntry(OperandSize::kByte,
|
2016-08-10 16:41:43 +00:00
|
|
|
Smi::FromInt(static_cast<int>(i)));
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), 2 * k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-11-14 10:48:26 +00:00
|
|
|
CHECK_EQ(constant_array->length(), static_cast<int>(2 * k8BitCapacity));
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-08-10 16:41:43 +00:00
|
|
|
Object* original = constant_array->get(static_cast<int>(k8BitCapacity + i));
|
|
|
|
Object* duplicate = constant_array->get(static_cast<int>(i));
|
|
|
|
CHECK(original->SameValue(duplicate));
|
2016-01-05 19:08:11 +00:00
|
|
|
Handle<Object> reference = isolate()->factory()->NewNumberFromSize(i);
|
|
|
|
CHECK(original->SameValue(*reference));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConstantArrayBuilderTest, GapNotFilledWhenLowReservationDiscarded) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-01-05 19:08:11 +00:00
|
|
|
OperandSize operand_size = builder.CreateReservedEntry();
|
|
|
|
CHECK(OperandSize::kByte == operand_size);
|
2016-11-14 10:48:26 +00:00
|
|
|
CHECK_EQ(builder.size(), 0u);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
const AstValue* ast_values[k8BitCapacity];
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_values[i] = ast_factory.NewNumber(i + 0.5);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
|
|
|
builder.Insert(ast_values[i]);
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), i + k8BitCapacity + 1);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2016-01-05 19:08:11 +00:00
|
|
|
builder.DiscardReservedEntry(OperandSize::kByte);
|
2017-02-10 17:22:45 +00:00
|
|
|
builder.Insert(ast_values[i]);
|
2016-02-26 10:14:49 +00:00
|
|
|
CHECK_EQ(builder.size(), 2 * k8BitCapacity);
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
ast_factory.Internalize(isolate());
|
2016-02-26 10:14:49 +00:00
|
|
|
for (size_t i = 0; i < k8BitCapacity; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
Handle<Object> reference = isolate()->factory()->NewNumber(i + 0.5);
|
|
|
|
Handle<Object> original =
|
|
|
|
builder.At(k8BitCapacity + i, isolate()).ToHandleChecked();
|
2016-01-05 19:08:11 +00:00
|
|
|
CHECK(original->SameValue(*reference));
|
2017-02-10 17:22:45 +00:00
|
|
|
MaybeHandle<Object> duplicate = builder.At(i, isolate());
|
|
|
|
CHECK(duplicate.is_null());
|
2016-01-05 19:08:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-01 12:08:53 +00:00
|
|
|
TEST_F(ConstantArrayBuilderTest, HolesWithUnusedReservations) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2016-04-01 12:08:53 +00:00
|
|
|
static int kNumberOfHoles = 128;
|
2017-02-10 17:22:45 +00:00
|
|
|
static int k8BitCapacity = ConstantArrayBuilder::k8BitCapacity;
|
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-04-01 12:08:53 +00:00
|
|
|
for (int i = 0; i < kNumberOfHoles; ++i) {
|
|
|
|
CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kByte);
|
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
// Values are placed before the reserved entries in the same slice.
|
|
|
|
for (int i = 0; i < k8BitCapacity - kNumberOfHoles; ++i) {
|
|
|
|
CHECK_EQ(builder.Insert(ast_factory.NewNumber(i + 0.5)),
|
2016-11-10 11:25:17 +00:00
|
|
|
static_cast<size_t>(i));
|
2016-04-01 12:08:53 +00:00
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
// The next value is pushed into the next slice.
|
|
|
|
CHECK_EQ(builder.Insert(ast_factory.NewNumber(k8BitCapacity + 0.5)),
|
|
|
|
k8BitCapacity);
|
2016-04-01 12:08:53 +00:00
|
|
|
|
2017-02-10 17:22:45 +00:00
|
|
|
// Discard the reserved entries.
|
|
|
|
for (int i = 0; i < kNumberOfHoles; ++i) {
|
|
|
|
builder.DiscardReservedEntry(OperandSize::kByte);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK_EQ(constant_array->length(), k8BitCapacity + 1);
|
|
|
|
for (int i = kNumberOfHoles; i < k8BitCapacity; i++) {
|
2016-04-01 12:08:53 +00:00
|
|
|
CHECK(constant_array->get(i)->SameValue(
|
|
|
|
*isolate()->factory()->the_hole_value()));
|
|
|
|
}
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK(!constant_array->get(kNumberOfHoles - 1)
|
|
|
|
->SameValue(*isolate()->factory()->the_hole_value()));
|
|
|
|
CHECK(!constant_array->get(k8BitCapacity)
|
|
|
|
->SameValue(*isolate()->factory()->the_hole_value()));
|
2016-04-01 12:08:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(ConstantArrayBuilderTest, ReservationsAtAllScales) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
|
|
|
AstValueFactory ast_factory(zone(), isolate()->ast_string_constants(),
|
|
|
|
isolate()->heap()->HashSeed());
|
2016-04-01 12:08:53 +00:00
|
|
|
for (int i = 0; i < 256; i++) {
|
|
|
|
CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kByte);
|
|
|
|
}
|
|
|
|
for (int i = 256; i < 65536; ++i) {
|
|
|
|
CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kShort);
|
|
|
|
}
|
|
|
|
for (int i = 65536; i < 131072; ++i) {
|
|
|
|
CHECK_EQ(builder.CreateReservedEntry(), OperandSize::kQuad);
|
|
|
|
}
|
2016-11-14 10:48:26 +00:00
|
|
|
CHECK_EQ(builder.CommitReservedEntry(OperandSize::kByte, Smi::FromInt(1)),
|
|
|
|
0u);
|
2016-08-10 16:41:43 +00:00
|
|
|
CHECK_EQ(builder.CommitReservedEntry(OperandSize::kShort, Smi::FromInt(2)),
|
2016-11-14 10:48:26 +00:00
|
|
|
256u);
|
2016-08-10 16:41:43 +00:00
|
|
|
CHECK_EQ(builder.CommitReservedEntry(OperandSize::kQuad, Smi::FromInt(3)),
|
2016-11-14 10:48:26 +00:00
|
|
|
65536u);
|
2017-02-10 17:22:45 +00:00
|
|
|
for (int i = 1; i < 256; i++) {
|
|
|
|
builder.DiscardReservedEntry(OperandSize::kByte);
|
|
|
|
}
|
|
|
|
for (int i = 257; i < 65536; ++i) {
|
|
|
|
builder.DiscardReservedEntry(OperandSize::kShort);
|
|
|
|
}
|
|
|
|
for (int i = 65537; i < 131072; ++i) {
|
|
|
|
builder.DiscardReservedEntry(OperandSize::kQuad);
|
|
|
|
}
|
|
|
|
|
|
|
|
ast_factory.Internalize(isolate());
|
2016-08-18 13:42:05 +00:00
|
|
|
Handle<FixedArray> constant_array = builder.ToFixedArray(isolate());
|
2016-04-01 12:08:53 +00:00
|
|
|
CHECK_EQ(constant_array->length(), 65537);
|
|
|
|
int count = 1;
|
|
|
|
for (int i = 0; i < constant_array->length(); ++i) {
|
|
|
|
Handle<Object> expected;
|
|
|
|
if (i == 0 || i == 256 || i == 65536) {
|
|
|
|
expected = isolate()->factory()->NewNumber(count++);
|
|
|
|
} else {
|
|
|
|
expected = isolate()->factory()->the_hole_value();
|
|
|
|
}
|
|
|
|
CHECK(constant_array->get(i)->SameValue(*expected));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-25 11:30:36 +00:00
|
|
|
TEST_F(ConstantArrayBuilderTest, AllocateEntriesWithFixedReservations) {
|
2016-08-05 10:09:50 +00:00
|
|
|
CanonicalHandleScope canonical(isolate());
|
2017-02-10 17:22:45 +00:00
|
|
|
ConstantArrayBuilder builder(zone());
|
2016-07-25 11:30:36 +00:00
|
|
|
for (size_t i = 0; i < k16BitCapacity; i++) {
|
|
|
|
if ((i % 2) == 0) {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK_EQ(i, builder.InsertDeferred());
|
2016-07-25 11:30:36 +00:00
|
|
|
} else {
|
2017-02-10 17:22:45 +00:00
|
|
|
builder.Insert(Smi::FromInt(static_cast<int>(i)));
|
2016-07-25 11:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK_EQ(builder.size(), k16BitCapacity);
|
|
|
|
|
|
|
|
// Check values before reserved entries are inserted.
|
|
|
|
for (size_t i = 0; i < k16BitCapacity; i++) {
|
|
|
|
if ((i % 2) == 0) {
|
2017-02-10 17:22:45 +00:00
|
|
|
// Check reserved values are null.
|
|
|
|
MaybeHandle<Object> empty = builder.At(i, isolate());
|
|
|
|
CHECK(empty.is_null());
|
2016-07-25 11:30:36 +00:00
|
|
|
} else {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK_EQ(Handle<Smi>::cast(builder.At(i, isolate()).ToHandleChecked())
|
|
|
|
->value(),
|
|
|
|
static_cast<int>(i));
|
2016-07-25 11:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert reserved entries.
|
|
|
|
for (size_t i = 0; i < k16BitCapacity; i += 2) {
|
2017-02-10 17:22:45 +00:00
|
|
|
builder.SetDeferredAt(i,
|
|
|
|
handle(Smi::FromInt(static_cast<int>(i)), isolate()));
|
2016-07-25 11:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check values after reserved entries are inserted.
|
|
|
|
for (size_t i = 0; i < k16BitCapacity; i++) {
|
2017-02-10 17:22:45 +00:00
|
|
|
CHECK_EQ(
|
|
|
|
Handle<Smi>::cast(builder.At(i, isolate()).ToHandleChecked())->value(),
|
|
|
|
static_cast<int>(i));
|
2016-07-25 11:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-05 19:08:11 +00:00
|
|
|
} // namespace interpreter
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|