Commit 4f586850 authored by Sathya Gunasekaran's avatar Sathya Gunasekaran

[dict] Add helper methods to SmallorderedNameDictionary


Bug: v8:6443, v8:7569
Change-Id: Ia7f0550500b19e93d78983db2e20d020bc0ff164
Reviewed-on: https://chromium-review.googlesource.com/c/1329700Reviewed-by: 's avatarCamillo Bruni <cbruni@chromium.org>
Cr-Commit-Position: refs/heads/master@{#57440}
parent cdfc65ab
......@@ -1613,6 +1613,7 @@ void Foreign::set_foreign_address(Address value) {
template <class Derived>
void SmallOrderedHashTable<Derived>::SetDataEntry(int entry, int relative_index,
Object* value) {
DCHECK_NE(kNotFound, entry);
Address entry_offset = GetDataEntryOffset(entry, relative_index);
RELAXED_WRITE_FIELD(this, entry_offset, value);
WRITE_BARRIER(this, static_cast<int>(entry_offset), value);
......
......@@ -65,7 +65,6 @@ inline PropertyDetails OrderedNameDictionary::DetailsAt(int entry) {
Smi::cast(get(EntryToIndex(entry) + kPropertyDetailsOffset)));
}
// Set the details for entry.
inline void OrderedNameDictionary::DetailsAtPut(int entry,
PropertyDetails value) {
DCHECK_NE(entry, kNotFound);
......@@ -74,6 +73,29 @@ inline void OrderedNameDictionary::DetailsAtPut(int entry,
this->set(EntryToIndex(entry) + kPropertyDetailsOffset, value.AsSmi());
}
inline Object* SmallOrderedNameDictionary::ValueAt(int entry) {
return this->GetDataEntry(entry, kValueIndex);
}
// Set the value for entry.
inline void SmallOrderedNameDictionary::ValueAtPut(int entry, Object* value) {
this->SetDataEntry(entry, kValueIndex, value);
}
// Returns the property details for the property at entry.
inline PropertyDetails SmallOrderedNameDictionary::DetailsAt(int entry) {
// TODO(gsathya): Optimize the cast away. And store this in the data table.
return PropertyDetails(
Smi::cast(this->GetDataEntry(entry, kPropertyDetailsIndex)));
}
// Set the details for entry.
inline void SmallOrderedNameDictionary::DetailsAtPut(int entry,
PropertyDetails value) {
// TODO(gsathya): Optimize the cast away. And store this in the data table.
this->SetDataEntry(entry, kPropertyDetailsIndex, value.AsSmi());
}
inline bool OrderedHashSet::Is(Handle<HeapObject> table) {
return table->IsOrderedHashSet();
}
......
......@@ -613,6 +613,18 @@ class SmallOrderedNameDictionary
DECL_PRINTER(SmallOrderedNameDictionary)
// Returns the value for entry.
inline Object* ValueAt(int entry);
// Set the value for entry.
inline void ValueAtPut(int entry, Object* value);
// Returns the property details for the property at entry.
inline PropertyDetails DetailsAt(int entry);
// Set the details for entry.
inline void DetailsAtPut(int entry, PropertyDetails value);
static const int kKeyIndex = 0;
static const int kValueIndex = 1;
static const int kPropertyDetailsIndex = 2;
......
......@@ -1283,7 +1283,7 @@ TEST(OrderedNameDictionaryInsertion) {
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!OrderedNameDictionary::HasKey(isolate, *dict, *key1));
PropertyDetails details = PropertyDetails::Empty();
dict = OrderedNameDictionary::Add(isolate, dict, key1, value, details);
......@@ -1314,7 +1314,7 @@ TEST(OrderedNameDictionaryFindEntry) {
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!OrderedNameDictionary::HasKey(isolate, *dict, *key1));
PropertyDetails details = PropertyDetails::Empty();
dict = OrderedNameDictionary::Add(isolate, dict, key1, value, details);
......@@ -1354,7 +1354,7 @@ TEST(OrderedNameDictionaryValueAtAndValueAtPut) {
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!OrderedNameDictionary::HasKey(isolate, *dict, *key1));
PropertyDetails details = PropertyDetails::Empty();
dict = OrderedNameDictionary::Add(isolate, dict, key1, value, details);
......@@ -1364,20 +1364,16 @@ TEST(OrderedNameDictionaryValueAtAndValueAtPut) {
CHECK(OrderedNameDictionary::HasKey(isolate, *dict, *key1));
int entry = dict->FindEntry(isolate, *key1);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
Handle<Object> found = handle(dict->ValueAt(entry), isolate);
CHECK(found->SameValue(*value));
CHECK_EQ(*found, *value);
// Change the value
Handle<String> other_value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> other_value = isolate->factory()->InternalizeUtf8String("baz");
dict->ValueAtPut(entry, *other_value);
entry = dict->FindEntry(isolate, *key1);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
found = handle(dict->ValueAt(entry), isolate);
CHECK(found->SameValue(*other_value));
CHECK_EQ(*found, *other_value);
Handle<Symbol> key2 = factory->NewSymbol();
CHECK(!OrderedNameDictionary::HasKey(isolate, *dict, *key2));
......@@ -1389,23 +1385,19 @@ TEST(OrderedNameDictionaryValueAtAndValueAtPut) {
CHECK(OrderedNameDictionary::HasKey(isolate, *dict, *key2));
entry = dict->FindEntry(isolate, *key1);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
found = handle(dict->ValueAt(entry), isolate);
CHECK(found->SameValue(*other_value));
CHECK_EQ(*found, *other_value);
entry = dict->FindEntry(isolate, *key2);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
found = handle(dict->ValueAt(entry), isolate);
CHECK(found->SameValue(*value));
CHECK_EQ(*found, *value);
// Change the value
dict->ValueAtPut(entry, *other_value);
entry = dict->FindEntry(isolate, *key1);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
found = handle(dict->ValueAt(entry), isolate);
CHECK(found->SameValue(*other_value));
CHECK_EQ(*found, *other_value);
}
TEST(OrderedNameDictionaryDetailsAtAndDetailsAtPut) {
......@@ -1420,7 +1412,7 @@ TEST(OrderedNameDictionaryDetailsAtAndDetailsAtPut) {
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!OrderedNameDictionary::HasKey(isolate, *dict, *key1));
PropertyDetails details = PropertyDetails::Empty();
dict = OrderedNameDictionary::Add(isolate, dict, key1, value, details);
......@@ -1430,8 +1422,6 @@ TEST(OrderedNameDictionaryDetailsAtAndDetailsAtPut) {
CHECK(OrderedNameDictionary::HasKey(isolate, *dict, *key1));
int entry = dict->FindEntry(isolate, *key1);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
PropertyDetails found = dict->DetailsAt(entry);
CHECK_EQ(PropertyDetails::Empty().AsSmi(), found.AsSmi());
......@@ -1453,14 +1443,11 @@ TEST(OrderedNameDictionaryDetailsAtAndDetailsAtPut) {
CHECK(OrderedNameDictionary::HasKey(isolate, *dict, *key2));
entry = dict->FindEntry(isolate, *key1);
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
found = dict->DetailsAt(entry);
CHECK_EQ(other.AsSmi(), found.AsSmi());
CHECK_NE(PropertyDetails::Empty().AsSmi(), found.AsSmi());
entry = dict->FindEntry(isolate, *key2);
CHECK_EQ(other.AsSmi(), found.AsSmi());
dict->DetailsAtPut(entry, other);
found = dict->DetailsAt(entry);
......@@ -1481,7 +1468,7 @@ TEST(SmallOrderedNameDictionaryInsertion) {
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!dict->HasKey(isolate, key1));
PropertyDetails details = PropertyDetails::Empty();
dict = SmallOrderedNameDictionary::Add(isolate, dict, key1, value, details)
......@@ -1515,7 +1502,7 @@ TEST(SmallOrderedNameDictionaryFindEntry) {
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!dict->HasKey(isolate, key1));
PropertyDetails details = PropertyDetails::Empty();
......@@ -1546,6 +1533,126 @@ TEST(SmallOrderedNameDictionaryFindEntry) {
CHECK_NE(entry, OrderedNameDictionary::kNotFound);
}
TEST(SmallOrderedNameDictionaryValueAtAndValueAtPut) {
LocalContext context;
Isolate* isolate = GetIsolateFrom(&context);
Factory* factory = isolate->factory();
HandleScope scope(isolate);
Handle<SmallOrderedNameDictionary> dict =
factory->NewSmallOrderedNameDictionary();
Verify(isolate, dict);
CHECK_EQ(2, dict->NumberOfBuckets());
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!dict->HasKey(isolate, key1));
// CHECK(dict->HasKey(isolate, key2));
PropertyDetails details = PropertyDetails::Empty();
dict = SmallOrderedNameDictionary::Add(isolate, dict, key1, value, details)
.ToHandleChecked();
Verify(isolate, dict);
CHECK_EQ(2, dict->NumberOfBuckets());
CHECK_EQ(1, dict->NumberOfElements());
CHECK(dict->HasKey(isolate, key1));
int entry = dict->FindEntry(isolate, *key1);
Handle<Object> found = handle(dict->ValueAt(entry), isolate);
CHECK_EQ(*found, *value);
// Change the value
Handle<String> other_value = isolate->factory()->InternalizeUtf8String("baz");
dict->ValueAtPut(entry, *other_value);
entry = dict->FindEntry(isolate, *key1);
found = handle(dict->ValueAt(entry), isolate);
CHECK_EQ(*found, *other_value);
Handle<Symbol> key2 = factory->NewSymbol();
CHECK(!dict->HasKey(isolate, key2));
dict = SmallOrderedNameDictionary::Add(isolate, dict, key2, value, details)
.ToHandleChecked();
Verify(isolate, dict);
CHECK_EQ(2, dict->NumberOfBuckets());
CHECK_EQ(2, dict->NumberOfElements());
CHECK(dict->HasKey(isolate, key1));
CHECK(dict->HasKey(isolate, key2));
entry = dict->FindEntry(isolate, *key1);
found = handle(dict->ValueAt(entry), isolate);
CHECK_EQ(*found, *other_value);
entry = dict->FindEntry(isolate, *key2);
found = handle(dict->ValueAt(entry), isolate);
CHECK_EQ(*found, *value);
// Change the value
dict->ValueAtPut(entry, *other_value);
entry = dict->FindEntry(isolate, *key1);
found = handle(dict->ValueAt(entry), isolate);
CHECK_EQ(*found, *other_value);
}
TEST(SmallOrderedNameDictionaryDetailsAtAndDetailsAtPut) {
LocalContext context;
Isolate* isolate = GetIsolateFrom(&context);
Factory* factory = isolate->factory();
HandleScope scope(isolate);
Handle<SmallOrderedNameDictionary> dict =
factory->NewSmallOrderedNameDictionary();
Verify(isolate, dict);
CHECK_EQ(2, dict->NumberOfBuckets());
CHECK_EQ(0, dict->NumberOfElements());
Handle<String> key1 = isolate->factory()->InternalizeUtf8String("foo");
Handle<String> value = isolate->factory()->InternalizeUtf8String("bar");
CHECK(!dict->HasKey(isolate, key1));
PropertyDetails details = PropertyDetails::Empty();
dict = SmallOrderedNameDictionary::Add(isolate, dict, key1, value, details)
.ToHandleChecked();
Verify(isolate, dict);
CHECK_EQ(2, dict->NumberOfBuckets());
CHECK_EQ(1, dict->NumberOfElements());
CHECK(dict->HasKey(isolate, key1));
int entry = dict->FindEntry(isolate, *key1);
PropertyDetails found = dict->DetailsAt(entry);
CHECK_EQ(PropertyDetails::Empty().AsSmi(), found.AsSmi());
PropertyDetails other =
PropertyDetails(kAccessor, READ_ONLY, PropertyCellType::kNoCell);
dict->DetailsAtPut(entry, other);
found = dict->DetailsAt(entry);
CHECK_NE(PropertyDetails::Empty().AsSmi(), found.AsSmi());
CHECK_EQ(other.AsSmi(), found.AsSmi());
Handle<Symbol> key2 = factory->NewSymbol();
CHECK(!dict->HasKey(isolate, key2));
dict = SmallOrderedNameDictionary::Add(isolate, dict, key2, value, details)
.ToHandleChecked();
Verify(isolate, dict);
CHECK_EQ(2, dict->NumberOfBuckets());
CHECK_EQ(2, dict->NumberOfElements());
CHECK(dict->HasKey(isolate, key1));
CHECK(dict->HasKey(isolate, key2));
entry = dict->FindEntry(isolate, *key1);
found = dict->DetailsAt(entry);
CHECK_NE(PropertyDetails::Empty().AsSmi(), found.AsSmi());
CHECK_EQ(other.AsSmi(), found.AsSmi());
entry = dict->FindEntry(isolate, *key2);
dict->DetailsAtPut(entry, other);
found = dict->DetailsAt(entry);
CHECK_NE(PropertyDetails::Empty().AsSmi(), found.AsSmi());
CHECK_EQ(other.AsSmi(), found.AsSmi());
}
} // namespace test_orderedhashtable
} // namespace internal
} // namespace v8
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment