1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "execution.h"
34 #include "macro-assembler.h"
36 #include "global-handles.h"
39 using namespace v8::internal;
44 template<typename HashMap>
45 static void TestHashMap(Handle<HashMap> table) {
46 Isolate* isolate = CcTest::i_isolate();
47 Factory* factory = isolate->factory();
49 Handle<JSObject> a = factory->NewJSArray(7);
50 Handle<JSObject> b = factory->NewJSArray(11);
51 table = HashMap::Put(table, a, b);
52 CHECK_EQ(table->NumberOfElements(), 1);
53 CHECK_EQ(table->Lookup(a), *b);
54 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
56 // Keys still have to be valid after objects were moved.
57 CcTest::heap()->CollectGarbage(NEW_SPACE);
58 CHECK_EQ(table->NumberOfElements(), 1);
59 CHECK_EQ(table->Lookup(a), *b);
60 CHECK_EQ(table->Lookup(b), CcTest::heap()->the_hole_value());
62 // Keys that are overwritten should not change number of elements.
63 table = HashMap::Put(table, a, factory->NewJSArray(13));
64 CHECK_EQ(table->NumberOfElements(), 1);
65 CHECK_NE(table->Lookup(a), *b);
67 // Keys mapped to the hole should be removed permanently.
68 table = HashMap::Put(table, a, factory->the_hole_value());
69 CHECK_EQ(table->NumberOfElements(), 0);
70 CHECK_EQ(table->Lookup(a), CcTest::heap()->the_hole_value());
72 // Keys should map back to their respective values and also should get
73 // an identity hash code generated.
74 for (int i = 0; i < 100; i++) {
75 Handle<JSReceiver> key = factory->NewJSArray(7);
76 Handle<JSObject> value = factory->NewJSArray(11);
77 table = HashMap::Put(table, key, value);
78 CHECK_EQ(table->NumberOfElements(), i + 1);
79 CHECK_NE(table->FindEntry(key), HashMap::kNotFound);
80 CHECK_EQ(table->Lookup(key), *value);
81 CHECK(key->GetIdentityHash()->IsSmi());
84 // Keys never added to the map which already have an identity hash
85 // code should not be found.
86 for (int i = 0; i < 100; i++) {
87 Handle<JSReceiver> key = factory->NewJSArray(7);
88 CHECK(JSReceiver::GetOrCreateIdentityHash(key)->IsSmi());
89 CHECK_EQ(table->FindEntry(key), HashMap::kNotFound);
90 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
91 CHECK(key->GetIdentityHash()->IsSmi());
94 // Keys that don't have an identity hash should not be found and also
95 // should not get an identity hash code generated.
96 for (int i = 0; i < 100; i++) {
97 Handle<JSReceiver> key = factory->NewJSArray(7);
98 CHECK_EQ(table->Lookup(key), CcTest::heap()->the_hole_value());
99 CHECK_EQ(key->GetIdentityHash(),
100 CcTest::heap()->undefined_value());
106 LocalContext context;
107 v8::HandleScope scope(context->GetIsolate());
108 Isolate* isolate = CcTest::i_isolate();
109 TestHashMap(ObjectHashTable::New(isolate, 23));
110 TestHashMap(isolate->factory()->NewOrderedHashMap());
114 class ObjectHashTableTest: public ObjectHashTable {
116 void insert(int entry, int key, int value) {
117 set(EntryToIndex(entry), Smi::FromInt(key));
118 set(EntryToIndex(entry) + 1, Smi::FromInt(value));
121 int lookup(int key) {
122 Handle<Object> key_obj(Smi::FromInt(key), GetIsolate());
123 return Smi::cast(Lookup(key_obj))->value();
132 TEST(HashTableRehash) {
133 LocalContext context;
134 Isolate* isolate = CcTest::i_isolate();
135 v8::HandleScope scope(context->GetIsolate());
136 // Test almost filled table.
138 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
139 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
140 int capacity = t->capacity();
141 for (int i = 0; i < capacity - 1; i++) {
142 t->insert(i, i * i, i);
144 t->Rehash(handle(Smi::FromInt(0), isolate));
145 for (int i = 0; i < capacity - 1; i++) {
146 CHECK_EQ(i, t->lookup(i * i));
149 // Test half-filled table.
151 Handle<ObjectHashTable> table = ObjectHashTable::New(isolate, 100);
152 ObjectHashTableTest* t = reinterpret_cast<ObjectHashTableTest*>(*table);
153 int capacity = t->capacity();
154 for (int i = 0; i < capacity / 2; i++) {
155 t->insert(i, i * i, i);
157 t->Rehash(handle(Smi::FromInt(0), isolate));
158 for (int i = 0; i < capacity / 2; i++) {
159 CHECK_EQ(i, t->lookup(i * i));
166 template<class HashSet>
167 static void TestHashSetCausesGC(Handle<HashSet> table) {
168 Isolate* isolate = CcTest::i_isolate();
169 Factory* factory = isolate->factory();
171 Handle<JSObject> key = factory->NewJSArray(0);
172 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
174 // Force allocation of hash table backing store for hidden properties.
175 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
176 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
177 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
179 // Simulate a full heap so that generating an identity hash code
180 // in subsequent calls will request GC.
181 SimulateFullSpace(CcTest::heap()->new_space());
182 SimulateFullSpace(CcTest::heap()->old_pointer_space());
184 // Calling Contains() should not cause GC ever.
185 int gc_count = isolate->heap()->gc_count();
186 CHECK(!table->Contains(key));
187 CHECK(gc_count == isolate->heap()->gc_count());
189 // Calling Remove() will not cause GC in this case.
190 table = HashSet::Remove(table, key);
191 CHECK(gc_count == isolate->heap()->gc_count());
193 // Calling Add() should cause GC.
194 table = HashSet::Add(table, key);
195 CHECK(gc_count < isolate->heap()->gc_count());
199 TEST(ObjectHashSetCausesGC) {
200 i::FLAG_stress_compaction = false;
201 LocalContext context;
202 v8::HandleScope scope(context->GetIsolate());
203 Isolate* isolate = CcTest::i_isolate();
204 TestHashSetCausesGC(isolate->factory()->NewOrderedHashSet());
210 template<class HashMap>
211 static void TestHashMapCausesGC(Handle<HashMap> table) {
212 Isolate* isolate = CcTest::i_isolate();
213 Factory* factory = isolate->factory();
215 Handle<JSObject> key = factory->NewJSArray(0);
216 v8::Handle<v8::Object> key_obj = v8::Utils::ToLocal(key);
218 // Force allocation of hash table backing store for hidden properties.
219 key_obj->SetHiddenValue(v8_str("key 1"), v8_str("val 1"));
220 key_obj->SetHiddenValue(v8_str("key 2"), v8_str("val 2"));
221 key_obj->SetHiddenValue(v8_str("key 3"), v8_str("val 3"));
223 // Simulate a full heap so that generating an identity hash code
224 // in subsequent calls will request GC.
225 SimulateFullSpace(CcTest::heap()->new_space());
226 SimulateFullSpace(CcTest::heap()->old_pointer_space());
228 // Calling Lookup() should not cause GC ever.
229 CHECK(table->Lookup(key)->IsTheHole());
231 // Calling Put() should request GC by returning a failure.
232 int gc_count = isolate->heap()->gc_count();
233 HashMap::Put(table, key, key);
234 CHECK(gc_count < isolate->heap()->gc_count());
238 TEST(ObjectHashTableCausesGC) {
239 i::FLAG_stress_compaction = false;
240 LocalContext context;
241 v8::HandleScope scope(context->GetIsolate());
242 Isolate* isolate = CcTest::i_isolate();
243 TestHashMapCausesGC(ObjectHashTable::New(isolate, 1));
244 TestHashMapCausesGC(isolate->factory()->NewOrderedHashMap());