2011-07-28 17:21:22 +00:00
|
|
|
// Copyright 2011 the V8 project authors. All rights reserved.
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
#include "v8.h"
|
|
|
|
|
|
|
|
#include "api.h"
|
|
|
|
#include "debug.h"
|
|
|
|
#include "execution.h"
|
|
|
|
#include "factory.h"
|
|
|
|
#include "macro-assembler.h"
|
|
|
|
#include "objects.h"
|
|
|
|
#include "global-handles.h"
|
|
|
|
#include "cctest.h"
|
|
|
|
|
|
|
|
using namespace v8::internal;
|
|
|
|
|
2011-10-26 12:23:40 +00:00
|
|
|
|
2011-07-28 17:21:22 +00:00
|
|
|
TEST(ObjectHashTable) {
|
|
|
|
LocalContext context;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(context->GetIsolate());
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<ObjectHashTable> table = factory->NewObjectHashTable(23);
|
|
|
|
Handle<JSObject> a = factory->NewJSArray(7);
|
|
|
|
Handle<JSObject> b = factory->NewJSArray(11);
|
2013-11-05 11:47:11 +00:00
|
|
|
table = ObjectHashTable::Put(table, a, b);
|
2011-07-28 17:21:22 +00:00
|
|
|
CHECK_EQ(table->NumberOfElements(), 1);
|
|
|
|
CHECK_EQ(table->Lookup(*a), *b);
|
2013-09-19 09:46:15 +00:00
|
|
|
CHECK_EQ(table->Lookup(*b), CcTest::heap()->the_hole_value());
|
2011-07-28 17:21:22 +00:00
|
|
|
|
|
|
|
// Keys still have to be valid after objects were moved.
|
2013-09-19 09:46:15 +00:00
|
|
|
CcTest::heap()->CollectGarbage(NEW_SPACE);
|
2011-07-28 17:21:22 +00:00
|
|
|
CHECK_EQ(table->NumberOfElements(), 1);
|
|
|
|
CHECK_EQ(table->Lookup(*a), *b);
|
2013-09-19 09:46:15 +00:00
|
|
|
CHECK_EQ(table->Lookup(*b), CcTest::heap()->the_hole_value());
|
2011-07-28 17:21:22 +00:00
|
|
|
|
|
|
|
// Keys that are overwritten should not change number of elements.
|
2013-11-05 11:47:11 +00:00
|
|
|
table = ObjectHashTable::Put(table, a, factory->NewJSArray(13));
|
2011-07-28 17:21:22 +00:00
|
|
|
CHECK_EQ(table->NumberOfElements(), 1);
|
|
|
|
CHECK_NE(table->Lookup(*a), *b);
|
|
|
|
|
2012-06-25 13:33:48 +00:00
|
|
|
// Keys mapped to the hole should be removed permanently.
|
2013-11-05 11:47:11 +00:00
|
|
|
table = ObjectHashTable::Put(table, a, factory->the_hole_value());
|
2011-07-28 17:21:22 +00:00
|
|
|
CHECK_EQ(table->NumberOfElements(), 0);
|
|
|
|
CHECK_EQ(table->NumberOfDeletedElements(), 1);
|
2013-09-19 09:46:15 +00:00
|
|
|
CHECK_EQ(table->Lookup(*a), CcTest::heap()->the_hole_value());
|
2011-07-28 17:21:22 +00:00
|
|
|
|
2011-10-26 12:23:40 +00:00
|
|
|
// Keys should map back to their respective values and also should get
|
|
|
|
// an identity hash code generated.
|
2011-07-28 17:21:22 +00:00
|
|
|
for (int i = 0; i < 100; i++) {
|
2013-08-30 13:42:16 +00:00
|
|
|
Handle<JSReceiver> key = factory->NewJSArray(7);
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<JSObject> value = factory->NewJSArray(11);
|
2013-11-05 11:47:11 +00:00
|
|
|
table = ObjectHashTable::Put(table, key, value);
|
2011-07-28 17:21:22 +00:00
|
|
|
CHECK_EQ(table->NumberOfElements(), i + 1);
|
|
|
|
CHECK_NE(table->FindEntry(*key), ObjectHashTable::kNotFound);
|
|
|
|
CHECK_EQ(table->Lookup(*key), *value);
|
2013-11-05 11:47:11 +00:00
|
|
|
CHECK(key->GetIdentityHash()->IsSmi());
|
2011-10-26 12:23:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Keys never added to the map which already have an identity hash
|
|
|
|
// code should not be found.
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2013-08-30 13:42:16 +00:00
|
|
|
Handle<JSReceiver> key = factory->NewJSArray(7);
|
2013-11-05 11:47:11 +00:00
|
|
|
CHECK(JSReceiver::GetOrCreateIdentityHash(key)->IsSmi());
|
2011-10-26 12:23:40 +00:00
|
|
|
CHECK_EQ(table->FindEntry(*key), ObjectHashTable::kNotFound);
|
2013-09-19 09:46:15 +00:00
|
|
|
CHECK_EQ(table->Lookup(*key), CcTest::heap()->the_hole_value());
|
2013-11-05 11:47:11 +00:00
|
|
|
CHECK(key->GetIdentityHash()->IsSmi());
|
2011-07-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
|
2011-10-26 12:23:40 +00:00
|
|
|
// Keys that don't have an identity hash should not be found and also
|
|
|
|
// should not get an identity hash code generated.
|
|
|
|
for (int i = 0; i < 100; i++) {
|
2013-08-30 13:42:16 +00:00
|
|
|
Handle<JSReceiver> key = factory->NewJSArray(7);
|
2013-09-19 09:46:15 +00:00
|
|
|
CHECK_EQ(table->Lookup(*key), CcTest::heap()->the_hole_value());
|
2013-11-05 11:47:11 +00:00
|
|
|
CHECK_EQ(key->GetIdentityHash(),
|
2013-09-19 09:46:15 +00:00
|
|
|
CcTest::heap()->undefined_value());
|
2011-07-28 17:21:22 +00:00
|
|
|
}
|
|
|
|
}
|
2011-10-26 12:23:40 +00:00
|
|
|
|
|
|
|
|
2013-09-12 11:03:27 +00:00
|
|
|
class ObjectHashTableTest: public ObjectHashTable {
|
|
|
|
public:
|
|
|
|
void insert(int entry, int key, int value) {
|
|
|
|
set(EntryToIndex(entry), Smi::FromInt(key));
|
|
|
|
set(EntryToIndex(entry) + 1, Smi::FromInt(value));
|
|
|
|
}
|
|
|
|
|
|
|
|
int lookup(int key) {
|
|
|
|
return Smi::cast(Lookup(Smi::FromInt(key)))->value();
|
|
|
|
}
|
|
|
|
|
|
|
|
int capacity() {
|
|
|
|
return Capacity();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
TEST(HashTableRehash) {
|
|
|
|
LocalContext context;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-09-12 11:03:27 +00:00
|
|
|
Factory* factory = isolate->factory();
|
|
|
|
v8::HandleScope scope(context->GetIsolate());
|
|
|
|
// Test almost filled table.
|
|
|
|
{
|
|
|
|
Handle<ObjectHashTable> table = factory->NewObjectHashTable(100);
|
|
|
|
ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
|
|
|
|
int capacity = t->capacity();
|
|
|
|
for (int i = 0; i < capacity - 1; i++) {
|
|
|
|
t->insert(i, i * i, i);
|
|
|
|
}
|
|
|
|
t->Rehash(Smi::FromInt(0));
|
|
|
|
for (int i = 0; i < capacity - 1; i++) {
|
|
|
|
CHECK_EQ(i, t->lookup(i * i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Test half-filled table.
|
|
|
|
{
|
|
|
|
Handle<ObjectHashTable> table = factory->NewObjectHashTable(100);
|
|
|
|
ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
|
|
|
|
int capacity = t->capacity();
|
|
|
|
for (int i = 0; i < capacity / 2; i++) {
|
|
|
|
t->insert(i, i * i, i);
|
|
|
|
}
|
|
|
|
t->Rehash(Smi::FromInt(0));
|
|
|
|
for (int i = 0; i < capacity / 2; i++) {
|
|
|
|
CHECK_EQ(i, t->lookup(i * i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-10-26 12:23:40 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
TEST(ObjectHashSetCausesGC) {
|
2013-06-19 08:58:09 +00:00
|
|
|
i::FLAG_stress_compaction = false;
|
2011-10-26 12:23:40 +00:00
|
|
|
LocalContext context;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(context->GetIsolate());
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<ObjectHashSet> table = factory->NewObjectHashSet(1);
|
|
|
|
Handle<JSObject> key = factory->NewJSArray(0);
|
2012-07-30 13:09:09 +00:00
|
|
|
v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
|
|
|
|
|
|
|
|
// Force allocation of hash table backing store for hidden properties.
|
|
|
|
key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
|
|
|
|
key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
|
|
|
|
key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
|
2011-10-26 12:23:40 +00:00
|
|
|
|
|
|
|
// Simulate a full heap so that generating an identity hash code
|
|
|
|
// in subsequent calls will request GC.
|
2013-09-19 09:46:15 +00:00
|
|
|
SimulateFullSpace(CcTest::heap()->new_space());
|
|
|
|
SimulateFullSpace(CcTest::heap()->old_pointer_space());
|
2011-10-26 12:23:40 +00:00
|
|
|
|
|
|
|
// Calling Contains() should not cause GC ever.
|
2013-11-05 11:47:11 +00:00
|
|
|
int gc_count = isolate->heap()->gc_count();
|
2011-10-26 12:23:40 +00:00
|
|
|
CHECK(!table->Contains(*key));
|
2013-11-05 11:47:11 +00:00
|
|
|
CHECK(gc_count == isolate->heap()->gc_count());
|
2011-10-26 12:23:40 +00:00
|
|
|
|
2013-11-05 11:47:11 +00:00
|
|
|
// Calling Remove() will not cause GC in this case.
|
|
|
|
table = ObjectHashSet::Remove(table, key);
|
|
|
|
CHECK(gc_count == isolate->heap()->gc_count());
|
2011-10-26 12:23:40 +00:00
|
|
|
|
2013-11-05 11:47:11 +00:00
|
|
|
// Calling Add() should cause GC.
|
|
|
|
table = ObjectHashSet::Add(table, key);
|
|
|
|
CHECK(gc_count < isolate->heap()->gc_count());
|
2011-10-26 12:23:40 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
TEST(ObjectHashTableCausesGC) {
|
2013-06-19 08:58:09 +00:00
|
|
|
i::FLAG_stress_compaction = false;
|
2011-10-26 12:23:40 +00:00
|
|
|
LocalContext context;
|
2013-09-19 09:17:13 +00:00
|
|
|
Isolate* isolate = CcTest::i_isolate();
|
2013-06-04 10:30:05 +00:00
|
|
|
Factory* factory = isolate->factory();
|
2013-03-15 12:06:53 +00:00
|
|
|
v8::HandleScope scope(context->GetIsolate());
|
2013-06-04 10:30:05 +00:00
|
|
|
Handle<ObjectHashTable> table = factory->NewObjectHashTable(1);
|
|
|
|
Handle<JSObject> key = factory->NewJSArray(0);
|
2012-07-30 13:09:09 +00:00
|
|
|
v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
|
|
|
|
|
|
|
|
// Force allocation of hash table backing store for hidden properties.
|
|
|
|
key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
|
|
|
|
key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
|
|
|
|
key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
|
2011-10-26 12:23:40 +00:00
|
|
|
|
|
|
|
// Simulate a full heap so that generating an identity hash code
|
|
|
|
// in subsequent calls will request GC.
|
2013-09-19 09:46:15 +00:00
|
|
|
SimulateFullSpace(CcTest::heap()->new_space());
|
|
|
|
SimulateFullSpace(CcTest::heap()->old_pointer_space());
|
2011-10-26 12:23:40 +00:00
|
|
|
|
|
|
|
// Calling Lookup() should not cause GC ever.
|
2012-06-25 13:57:52 +00:00
|
|
|
CHECK(table->Lookup(*key)->IsTheHole());
|
2011-10-26 12:23:40 +00:00
|
|
|
|
|
|
|
// Calling Put() should request GC by returning a failure.
|
2013-11-05 11:47:11 +00:00
|
|
|
int gc_count = isolate->heap()->gc_count();
|
|
|
|
ObjectHashTable::Put(table, key, key);
|
|
|
|
CHECK(gc_count < isolate->heap()->gc_count());
|
2011-10-26 12:23:40 +00:00
|
|
|
}
|
|
|
|
#endif
|